Remove extraneous IRC client

This commit is contained in:
2025-11-01 06:45:36 +10:00
parent 6eabbc0d73
commit baeea1c41b
2 changed files with 7 additions and 351 deletions

View File

@@ -1,6 +1,7 @@
from string import punctuation
import datetime as dt
from datetime import datetime, timedelta
from math import ceil
import time
import twitchio
@@ -15,7 +16,7 @@ from utils.lib import parse_dur, strfdelta, utc_now
from . import logger
from ..data import HyperfocusData, Hyperfocuser
from .focuschannel import FocusChannel
# Default requested scopes for joining a channel
CHANNEL_SCOPES = Scopes(
@@ -33,55 +34,12 @@ class FocusComponent(cmds.Component):
self.bot = bot
self.data = bot.dbconn.load_registry(HyperfocusData())
self.channels: dict[str, FocusChannel] = {}
self._last_deleted: dict[int, datetime] = {}
# ----- API -----
async def component_load(self):
await self.data.init()
async def get_focus_channel(self, channelid: str, channel: str):
"""
Get the logged in FocusChannel for the given channel, if possible.
This is an expensive operation when we need to login to a new channel,
so don't call this unless we are sure we can log in or need it.
"""
if not (ch := self.channels.get(channelid)) or not ch.is_alive:
# Create and login to a new focuschannel, if possible.
token = await self.get_token_for(channelid)
session = self.bot._http._session
ch = FocusChannel(channel, token, session)
try:
await ch.connect()
except Exception:
logger.exception(
f"Hyperfocus channel connect failure for '{channel}' ('{channelid}')"
)
raise
self.channels[channelid] = ch
return ch
async def get_token_for(self, channelid: str):
"""
Get the hyperfocus-capable token for the given channel.
"""
# TODO: Token invalidation method if it fails.
# Also should be in a transaction
# Might technically need to refresh token as well
scope_rows = await self.bot.data.user_auth_scopes.select_where(userid=channelid)
scopes = Scopes([row["scope"] for row in scope_rows])
has_required = all(scope in scopes for scope in CHANNEL_SCOPES)
if not has_required:
# TODO: Better auth exception
raise ValueError("Channel '%s' does not have required scopes." % channelid)
auth_row = await UserAuth.fetch(channelid)
assert auth_row is not None
return auth_row.token
async def get_hyperfocus(self, profileid: int) -> Hyperfocuser | None:
"""
Get the Hyperfocuser if the user is hyperfocused.
@@ -91,25 +49,11 @@ class FocusComponent(cmds.Component):
return row
async def focus_delete_message(self, message: twitchio.ChatMessage):
"""Delete the given message. Uses the API if possible, otherwise opens an IRC channel."""
"""Delete the given message."""
# This should be impossible, but just in case.
# 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,
# )
await message.broadcaster.delete_chat_messages(
moderator=message.broadcaster,
message_id=message.id,
@@ -159,7 +103,9 @@ class FocusComponent(cmds.Component):
sender=self.bot.bot_id,
)
except Exception:
logger.warning(f"Failed to delete a hyperfocus message: {payload!r}")
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! ",
@@ -212,6 +158,7 @@ class FocusComponent(cmds.Component):
)
# TODO: Update channel
minutes = ceil(dur / 60)
await ctx.reply(
f"{ctx.chatter.name} has gone into HYPERFOCUS! "
f"They will be in emote and command only mode for the next {minutes} minutes! "