Skip to content

Commit

Permalink
PiqueBot -> PiqueCog; change throttles
Browse files Browse the repository at this point in the history
  • Loading branch information
KPrasch committed Oct 13, 2023
1 parent e041943 commit 730fff0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
3 changes: 3 additions & 0 deletions pique/constants/_throttle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PUBLISHER = 1
SCANNER = 0.5
BATCH = 1
1 change: 0 additions & 1 deletion pique/constants/_tweaks.py

This file was deleted.

42 changes: 24 additions & 18 deletions pique/discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pique.subscriptions import DiscordSubscriber, SubscriptionManager


class PiqueBot(commands.Cog):
class PiqueCog(commands.Cog):
def __init__(
self,
name: str,
Expand All @@ -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...")
Expand All @@ -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}")
6 changes: 4 additions & 2 deletions pique/scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}")

Expand Down
4 changes: 2 additions & 2 deletions pique/services.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pique/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 730fff0

Please sign in to comment.