From 688282b6ac4e4840d5b0f98a6b1bca4773629353 Mon Sep 17 00:00:00 2001 From: Interitio Date: Sun, 31 May 2026 07:49:48 +1000 Subject: [PATCH] fix(twitch): Fix followage and whisper ctxvars. --- twitch/component.py | 2 ++ twitch/context.py | 14 +++++++------- twitch/ctxvars.py | 11 ++++++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/twitch/component.py b/twitch/component.py index 03ee5cf..b46f8fe 100644 --- a/twitch/component.py +++ b/twitch/component.py @@ -16,6 +16,8 @@ from .client import TwitchCmdClient # TODO: Make an alphacroc or croccyalpha client for testing # TODO: Fix the tracker not actually guarding the commands properly +# TODO: Fix suffix of result not printing +# TODO When command produces None don't stringify to 'None' class CustomCmdComponent(cmds.Component): diff --git a/twitch/context.py b/twitch/context.py index 35e531b..bbbbe5d 100644 --- a/twitch/context.py +++ b/twitch/context.py @@ -25,21 +25,21 @@ class CustomCmdUser: return self.user.display_name or self.user.name or self.user.id async def _exec_attr_whisper(self, ctx, message: str): - await self.user.send_whisper(to_user=self.user, message=message) + await ctx.bot.user.send_whisper(to_user=self.user, message=message) - async def _exec_attr_followage(self, ctx, channel: twitchio.PartialUser): + async def _exec_attr_followage(self, ctx, channel: "CustomCmdUser"): followed_at = None - query = await channel.fetch_followers(user=self.user) + query = await channel.user.fetch_followers( + user=self.user, token_for=ctx.bot.user + ) async for follower in query.followers: followed_at = follower.followed_at if followed_at: durstr = strfdelta(utc_now() - followed_at) - resp = ( - f"{self.user.mention} has been following {channel.mention} for {durstr}" - ) + return durstr else: - return f"{self.user.mention} is not following {channel.mention}" + return None @classmethod def from_user(cls, user: twitchio.PartialUser): diff --git a/twitch/ctxvars.py b/twitch/ctxvars.py index 8bf93f1..f1cf0f9 100644 --- a/twitch/ctxvars.py +++ b/twitch/ctxvars.py @@ -4,23 +4,28 @@ from typing import Optional stuff = {} + def register_var(name: str | None = None): def wrapper(coro): varname = name or coro.__name__ stuff[varname] = coro - return coro + return coro + return wrapper -@register_var('random') +@register_var("random") async def random_func(ctx, a: int, b: Optional[int] = None): + a = int(a) + b = int(b) if b is not None else None if b is not None: low, high = a, b else: low, high = 0, a return random.randint(low, high) -@register_var('user') + +@register_var("user") async def user_func(ctx, userstr: int | str): """ Attempt to turn the given userstr into a user.