-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c5a732
commit fd0ecbb
Showing
9 changed files
with
187 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import asyncio | ||
|
||
import discord | ||
from discord.ext import tasks | ||
|
||
from .settings import BOT_TICKER_INTERVAL_MINUTES | ||
|
||
|
||
class TickerBot(discord.Client): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs, intents=discord.Intents.default()) | ||
|
||
async def setup_hook(self) -> None: | ||
# start the task to run in the background | ||
self.ticker.start() | ||
|
||
async def update_bot_member_nick(self, guild, nick: str): | ||
bot_member = await guild.fetch_member(self.user.id) | ||
if bot_member is None: | ||
return | ||
await bot_member.edit(nick=nick) | ||
|
||
async def update_nick_for_all_servers(self, nick: str): | ||
await asyncio.gather( | ||
*map(lambda guild: self.update_bot_member_nick(guild, nick), self.guilds) | ||
) | ||
|
||
async def update_presence(self, presence_text: str): | ||
# https://discordpy.readthedocs.io/en/latest/api.html#discord.ActivityType | ||
await self.change_presence( | ||
activity=discord.Activity( | ||
name=presence_text, type=discord.ActivityType.watching | ||
) | ||
) | ||
|
||
@tasks.loop(seconds=BOT_TICKER_INTERVAL_MINUTES * 60) | ||
async def ticker(self): | ||
raise NotImplementedError | ||
|
||
@ticker.before_loop | ||
async def before_my_task(self): | ||
# wait until the bot logs in | ||
await self.wait_until_ready() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from discord.ext import tasks | ||
|
||
from .settings import BOT_TICKER_INTERVAL_MINUTES | ||
from .data import LiquidityPool | ||
from .helpers import LOGGER | ||
from .ticker import TickerBot | ||
|
||
|
||
class TVLBot(TickerBot): | ||
def __init__(self, *args, target_network: str, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.target_network = target_network | ||
|
||
async def on_ready(self): | ||
LOGGER.debug(f"Logged in as {self.user} (ID: {self.user.id})") | ||
LOGGER.debug("------") | ||
await self.update_presence(f"TVL {self.target_network}") | ||
|
||
@tasks.loop(seconds=BOT_TICKER_INTERVAL_MINUTES * 60) | ||
async def ticker(self): | ||
try: | ||
pools = await LiquidityPool.get_pools() | ||
tvl = await LiquidityPool.tvl(pools) | ||
await self.update_nick_for_all_servers(f"{round(tvl/1000000, 2)}M") | ||
except Exception as ex: | ||
LOGGER.error(f"Ticker failed with {ex}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters