From 730fff098418faab398494c58bc72e9bde6d051a Mon Sep 17 00:00:00 2001 From: Kieran Prasch Date: Fri, 13 Oct 2023 02:53:11 +0200 Subject: [PATCH] PiqueBot -> PiqueCog; change throttles --- pique/constants/_throttle.py | 3 +++ pique/constants/_tweaks.py | 1 - pique/discord/bot.py | 42 ++++++++++++++++++++---------------- pique/scanner/scanner.py | 6 ++++-- pique/services.py | 4 ++-- pique/subscriptions.py | 4 ++-- 6 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 pique/constants/_throttle.py delete mode 100644 pique/constants/_tweaks.py diff --git a/pique/constants/_throttle.py b/pique/constants/_throttle.py new file mode 100644 index 0000000..5d5a848 --- /dev/null +++ b/pique/constants/_throttle.py @@ -0,0 +1,3 @@ +PUBLISHER = 1 +SCANNER = 0.5 +BATCH = 1 diff --git a/pique/constants/_tweaks.py b/pique/constants/_tweaks.py deleted file mode 100644 index c8511dc..0000000 --- a/pique/constants/_tweaks.py +++ /dev/null @@ -1 +0,0 @@ -THROTTLE = 0.1 diff --git a/pique/discord/bot.py b/pique/discord/bot.py index 98582f7..1019624 100644 --- a/pique/discord/bot.py +++ b/pique/discord/bot.py @@ -9,7 +9,7 @@ from pique.subscriptions import DiscordSubscriber, SubscriptionManager -class PiqueBot(commands.Cog): +class PiqueCog(commands.Cog): def __init__( self, name: str, @@ -30,6 +30,13 @@ def __init__( self.start_time = datetime.datetime.now() LOGGER.debug(f"Initialized {self.name}") + @property + def uptime(self): + if self.start_time is None: + return "Bot is not active" + else: + return datetime.datetime.now() - self.start_time + async def start(self): await self.bot.add_cog(self) LOGGER.debug(f"Starting Bot...") @@ -43,35 +50,34 @@ async def on_ready(self): except Exception as e: LOGGER.error(f"Error in on_ready: {e}") + @commands.command() + async def status(self, ctx): + LOGGER.debug(f"Status requested by {ctx.author.display_name}") + try: + embed = await make_status_embed(ctx=ctx, w3c=self) + await ctx.send(embed=embed) + except Exception as e: + LOGGER.error(f"Error in status: {e}") + def _connect_subscriber_channels(self): for subscriber in self.subscription_manager.subscribers: + + # skip non-discord subscribers if not isinstance(subscriber, DiscordSubscriber): continue + # connect the channel channel = self.bot.get_channel(subscriber.channel_id) if not channel: LOGGER.error(f"Could not find channel with ID {subscriber.channel_id}") continue + # set the channel LOGGER.info(f"Found channel {channel.name} with ID {channel.id}") subscriber.channel = channel + # subscribe to events for event in self.scanner.events: - # subscribe to all events + # subscribe to all events by default + # TODO: allow for more granular subscription self.subscription_manager.subscribe(event=event, subscriber=subscriber) - - @property - def uptime(self): - if self.start_time is None: - return "Bot is not active" - else: - return datetime.datetime.now() - self.start_time - - @commands.command() - async def status(self, ctx): - LOGGER.debug(f"Status requested by {ctx.author.display_name}") - try: - embed = await make_status_embed(ctx=ctx, w3c=self) - await ctx.send(embed=embed) - except Exception as e: - LOGGER.error(f"Error in status: {e}") diff --git a/pique/scanner/scanner.py b/pique/scanner/scanner.py index fbc453e..9551f21 100644 --- a/pique/scanner/scanner.py +++ b/pique/scanner/scanner.py @@ -2,7 +2,7 @@ from asyncio import Queue from typing import List -from pique.constants._tweaks import THROTTLE +from pique.constants import _throttle from pique.log import LOGGER from pique.scanner.events import EventContainer, Event @@ -86,11 +86,13 @@ async def check_web3_events(self): f"Finished fetching events from {start_block} to {start_block + self.batch_size}" ) start_block += self.batch_size + await asyncio.sleep(_throttle.BATCH) + LOGGER.debug( f"Finished scanning {event_container.name}|{event_container.address[:8]}" ) - await asyncio.sleep(THROTTLE) + await asyncio.sleep(_throttle.SCANNER) except Exception as e: LOGGER.error(f"Error in check_web3_events: {e}") diff --git a/pique/services.py b/pique/services.py index 7c7fc85..02b819d 100644 --- a/pique/services.py +++ b/pique/services.py @@ -1,7 +1,7 @@ from asyncio import Queue from pique.config import PiqueConfig -from pique.discord.bot import PiqueBot +from pique.discord.bot import PiqueCog from pique.scanner.scanner import EventScanner from pique.subscriptions import SubscriptionManager @@ -16,7 +16,7 @@ async def _run_internal_services(config: PiqueConfig): async def run_discord_bot(config): scanner, manager = await _run_internal_services(config) - bot = PiqueBot( + bot = PiqueCog( name=config.name, command_prefix=config.discord.command_prefix, token=config.discord.token, diff --git a/pique/subscriptions.py b/pique/subscriptions.py index c02bf74..b46c4e3 100644 --- a/pique/subscriptions.py +++ b/pique/subscriptions.py @@ -7,7 +7,7 @@ from discord import TextChannel from pique.config import PiqueConfig -from pique.constants._tweaks import THROTTLE +from pique.constants import _throttle from pique.discord.embeds import create_event_embed from pique.log import LOGGER @@ -89,7 +89,7 @@ async def process_queue(self): LOGGER.debug(f"Processing event #{event.id[:8]}") await self.notify(event) self.event_queue.task_done() - await asyncio.sleep(THROTTLE) + await asyncio.sleep(_throttle.PUBLISHER) def start(self): asyncio.create_task(self.process_queue())