feat: Add websocket channel

This commit is contained in:
2025-11-01 10:58:26 +10:00
parent 737871d696
commit 3885710689
2 changed files with 146 additions and 13 deletions

View File

@@ -11,11 +11,13 @@ from twitchio.ext import commands as cmds
from botdata import UserAuth
from meta import Bot
from meta.logger import log_wrap
from meta.sockets import register_channel
from utils.lib import parse_dur, strfdelta, utc_now
from . import logger
from ..data import HyperfocusData, Hyperfocuser
from ..channel import FocusChannel
# Default requested scopes for joining a channel
@@ -33,6 +35,9 @@ class FocusComponent(cmds.Component):
def __init__(self, bot: Bot):
self.bot = bot
self.data = bot.dbconn.load_registry(HyperfocusData())
self.channel = FocusChannel(self.bot.profiles.profiles, self.data)
register_channel(self.channel.name, self.channel)
self._last_deleted: dict[int, datetime] = {}
@@ -96,22 +101,41 @@ class FocusComponent(cmds.Component):
)
try:
await self.focus_delete_message(payload)
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,
)
deleted = True
except Exception:
logger.warning(
f"Failed to delete a hyperfocus message: {payload!r}", exc_info=True
)
if notify:
await payload.broadcaster.send_message(
f"@{payload.chatter.name} Stay focused! ",
sender=self.bot.bot_id,
deleted = False
if notify:
self._last_deleted[profile.profileid] = utc_now()
try:
if deleted:
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,
)
else:
await payload.broadcaster.send_message(
f"@{payload.chatter.name} Stay focused! ",
sender=self.bot.bot_id,
)
except twitchio.exceptions.HTTPException:
logger.warning(
f"Failed to notify user of hyperfocus deletion: {payload!r}",
exc_info=True,
)
self._last_deleted[profile.profileid] = utc_now()
if hyperfocused:
# Send an update to the channel
# TODO: Nicer and more efficient caching of which channel has which users
# TODO: Possible race condition with the commands. Should use locks.
comm = await self.bot.profiles.fetch_community(
payload.broadcaster, touch=True
)
await self.channel.send_hyperfocus_patch(comm.communityid, hyperfocused)
# ------ Commands -----
@cmds.command(
@@ -150,14 +174,15 @@ class FocusComponent(cmds.Component):
comm = await self.bot.profiles.fetch_community(ctx.broadcaster, touch=True)
await Hyperfocuser.table.delete_where(profileid=pid)
await Hyperfocuser.create(
focuser = await Hyperfocuser.create(
profileid=pid,
started_at=now,
ends_at=end_at,
started_in=comm.communityid,
)
# TODO: Update channel
await self.channel.send_hyperfocus_patch(comm.communityid, focuser)
minutes = ceil(dur / 60)
await ctx.reply(
f"{ctx.chatter.name} has gone into HYPERFOCUS! "
@@ -171,6 +196,7 @@ class FocusComponent(cmds.Component):
row = await Hyperfocuser.fetch(profile.profileid)
if row:
await row.delete()
await self.channel.send_hyperfocus_del(profile.profileid)
await ctx.reply(
"Welcome back from focus, hope it went well!"