diff --git a/__init__.py b/__init__.py index e69de29..3656724 100644 --- a/__init__.py +++ b/__init__.py @@ -0,0 +1 @@ +from .hyperfocus import * diff --git a/data/hyperfocus.sql b/data/hyperfocus.sql index 26624d1..8d548cd 100644 --- a/data/hyperfocus.sql +++ b/data/hyperfocus.sql @@ -6,7 +6,7 @@ VALUES ('HYPERFOCUS', 0, 1, 'Initial Creation'); CREATE TABLE hyperfocused( profileid INTEGER PRIMARY KEY REFERENCES user_profiles(profileid) ON DELETE CASCADE ON UPDATE CASCADE, started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), - ends_at TIMSTAMPTZ NOT NULL, + ends_at TIMESTAMPTZ NOT NULL, started_in INTEGER REFERENCES communities(communityid) ON DELETE SET NULL ON UPDATE CASCADE ); diff --git a/hyperfocus/twitch/component.py b/hyperfocus/twitch/component.py index 4402eee..ded5349 100644 --- a/hyperfocus/twitch/component.py +++ b/hyperfocus/twitch/component.py @@ -34,6 +34,7 @@ class FocusComponent(cmds.Component): self.data = bot.dbconn.load_registry(HyperfocusData()) self.channels: dict[str, FocusChannel] = {} + self._last_deleted: dict[int, datetime] = {} # ----- API ----- async def component_load(self): @@ -85,8 +86,8 @@ class FocusComponent(cmds.Component): """ Get the Hyperfocuser if the user is hyperfocused. """ - row = Hyperfocuser.fetch(profileid) - if row and row.ends_at < utc_now(): + row = await Hyperfocuser.fetch(profileid) + if row and row.ends_at > utc_now(): return row async def focus_delete_message(self, message: twitchio.ChatMessage): @@ -95,20 +96,24 @@ class FocusComponent(cmds.Component): # None id could cause chat to be wiped assert message.id is not None - badge_sets = {badge.set_id for badge in message.badges} - if "moderator" in badge_sets or "broadcaster" in badge_sets: - # We need to use the focus channel - - assert message.broadcaster.name is not None - chan = await self.get_focus_channel( - str(message.broadcaster.id), message.broadcaster.name - ) - await chan.delete_msg(str(message.id)) - else: - await message.broadcaster.delete_chat_messages( - moderator=self.bot.bot_id, - message_id=message.id, - ) + # badge_sets = {badge.set_id for badge in message.badges} + # if "moderator" in badge_sets or "broadcaster" in badge_sets: + # # We need to use the focus channel + # + # assert message.broadcaster.name is not None + # chan = await self.get_focus_channel( + # str(message.broadcaster.id), message.broadcaster.name + # ) + # await chan.delete_msg(str(message.id)) + # else: + # await message.broadcaster.delete_chat_messages( + # moderator=self.bot.bot_id, + # message_id=message.id, + # ) + await message.broadcaster.delete_chat_messages( + moderator=message.broadcaster, + message_id=message.id, + ) def check_hyperfocus_message(self, message: twitchio.ChatMessage): """ @@ -141,25 +146,32 @@ class FocusComponent(cmds.Component): # If they are, check the message content for deletion if hyperfocused and not self.check_hyperfocus_message(payload): # If we need to delete, run delete and send message + notify = ( # + (last := self._last_deleted.get(profile.profileid)) + and (utc_now() - last).total_seconds() > 30 + ) try: await self.focus_delete_message(payload) - await payload.broadcaster.send_message( - f"@{payload.chatter.name} Stay focused! " - "(You are in !hyperfocus, use !unfocus to come back if you need to!)", - sender=self.bot.bot_id, - ) + if notify: + await payload.broadcaster.send_message( + f"@{payload.chatter.name} Stay focused! " + "(You are in !hyperfocus, use !unfocus to come back if you need to!)", + sender=self.bot.bot_id, + ) except Exception: logger.warning(f"Failed to delete a hyperfocus message: {payload!r}") - await payload.broadcaster.send_message( - f"@{payload.chatter.name} Stay focused! ", - sender=self.bot.bot_id, - ) + if notify: + await payload.broadcaster.send_message( + f"@{payload.chatter.name} Stay focused! ", + sender=self.bot.bot_id, + ) + self._last_deleted[profile.profileid] = utc_now() # ------ Commands ----- @cmds.command( name="hyperfocus", aliases=["hyperf", "hyper", "hypercrocus", "hyperofcus"] ) - async def hyperfocus_cmd(self, ctx, *, duration: str | None): + async def hyperfocus_cmd(self, ctx, *, duration: str | None = None): now = utc_now() # First parse duration @@ -202,7 +214,7 @@ class FocusComponent(cmds.Component): # TODO: Update channel await ctx.reply( f"{ctx.chatter.name} has gone into HYPERFOCUS! " - "They will be in emote and command only mode for the next {minutes} minutes! " + f"They will be in emote and command only mode for the next {minutes} minutes! " "Use !unfocus to come back if you need to, best of luck! ☘️🍀☘️ " ) diff --git a/hyperfocus/twitch/focuschannel.py b/hyperfocus/twitch/focuschannel.py index 56c0219..7287bd5 100644 --- a/hyperfocus/twitch/focuschannel.py +++ b/hyperfocus/twitch/focuschannel.py @@ -284,6 +284,7 @@ class FocusChannel: asyncio.create_task(self.connect()) async def send_msg(self, content: str): + logger.debug(f"Sending: {content}") await self._socket.send_str(content) async def delete_msg(self, msgid: str):