Initial merger with Twitch interface.

This commit is contained in:
2024-08-27 17:41:33 +10:00
parent 7e6dcb006f
commit d10fd2fc1d
28 changed files with 1309 additions and 40 deletions

View File

@@ -13,6 +13,7 @@ from meta.sharding import THIS_SHARD
from meta.monitor import ComponentMonitor, ComponentStatus, StatusLevel
from utils.lib import utc_now
from utils.ratelimits import limit_concurrency
from meta.sockets import Channel, register_channel
from wards import low_management_ward
@@ -39,6 +40,37 @@ _param_options = {
}
class TimerChannel(Channel):
name = 'Timer'
def __init__(self, cog: 'TimerCog', **kwargs):
super().__init__(**kwargs)
self.cog = cog
async def on_connection(self, websocket, event):
await super().on_connection(websocket, event)
timer = self.cog.get_channel_timer(1261999440160624734)
if timer is not None:
await self.send_set(
timer.data.last_started,
timer.data.focus_length,
timer.data.break_length,
websocket=websocket,
)
async def send_set(self, start_at, focus_length, break_length, goal=12, websocket=None):
await self.send_event({
'type': "DO",
'method': 'setTimer',
'args': {
'start_at': start_at.isoformat(),
'focus_length': focus_length,
'break_length': break_length,
'block_goal': goal,
}
}, websocket=websocket)
class TimerCog(LionCog):
def __init__(self, bot: LionBot):
self.bot = bot
@@ -46,6 +78,9 @@ class TimerCog(LionCog):
self.settings = TimerSettings()
self.monitor = ComponentMonitor('TimerCog', self._monitor)
self.channel = TimerChannel(self)
register_channel(self.channel.name, self.channel)
self.timer_options = TimerOptions()
self.ready = False
@@ -1012,3 +1047,31 @@ class TimerCog(LionCog):
ui = TimerConfigUI(self.bot, ctx.guild.id, ctx.channel.id)
await ui.run(ctx.interaction)
await ui.wait()
# ----- Hacky Stream commands -----
@cmds.hybrid_group('streamtimer', with_app_command=True)
async def streamtimer_group(self, ctx: LionContext):
...
@streamtimer_group.command(
name="update"
)
@low_management_ward
async def streamtimer_update_cmd(self, ctx: LionContext,
new_start: Optional[str] = None,
new_goal: int = 12):
timer = self.get_channel_timer(1261999440160624734)
if timer is None:
return
if new_start:
timezone = ctx.lmember.timezone
start_at = await self.bot.get_cog('Reminders').parse_time_static(new_start, timezone)
await timer.data.update(last_started=start_at)
await self.channel.send_set(
timer.data.last_started,
timer.data.focus_length,
timer.data.break_length,
goal=new_goal,
)
await ctx.reply("Stream Timer Updated")

View File

@@ -195,9 +195,7 @@ class Timer:
Uses voice channel member cache as source-of-truth.
"""
if (chan := self.channel):
members = [
member for member in chan.members if not member.bot and 1148167212901859328 in [role.id for role in member.roles]
]
members = [m for m in chan.members if not m.bot]
else:
members = []
return members
@@ -480,6 +478,7 @@ class Timer:
if self.guild.voice_client:
await self.guild.voice_client.disconnect(force=True)
alert_file = focus_alert_path if stage.focused else break_alert_path
try:
voice_client = await asyncio.wait_for(
self.channel.connect(timeout=30, reconnect=False),
@@ -613,7 +612,11 @@ class Timer:
if render:
try:
card = await get_timer_card(self.bot, self, stage)
await card.render()
data = await card.render()
import io
with io.BytesIO(data) as buffer:
with open(f"pomodoro_{self.data.channelid}.png", "wb") as f:
f.write(buffer.getbuffer())
rawargs['file'] = card.as_file(f"pomodoro_{self.data.channelid}.png")
except RenderingException:
pass
@@ -841,8 +844,8 @@ class Timer:
to_next_stage = (current.end - utc_now()).total_seconds()
# TODO: Consider request rate and load
if to_next_stage > 5 * 60 - drift:
time_to_sleep = 5 * 60
if to_next_stage > 1 * 60 - drift:
time_to_sleep = 1 * 60
else:
time_to_sleep = to_next_stage