【问题标题】:TwitchIO send cool down messageTwitchIO 发送冷却消息
【发布时间】:2022-07-11 04:04:57
【问题描述】:

基本上我将 TwitchIO 用于 twitch BOT。我设置了一个时间限制以防止用户发送垃圾邮件。当用户尝试发送垃圾邮件时,会出现一个错误,指示他们必须等待 30 秒。但是,我想使用 ctx.Send(...) 将此消息发送到 twitch 聊天室。

    @commands.command()
    @commands.cooldown(1,30,commands.Bucket.user)
    async def test(self, ctx: commands.Context):
        await ctx.send(f'user message from {ctx.author.name}!')

在 twitch 聊天中,我使用 !test 命令 message 'user message from {ctx.author.name}!问题是我无法重新运行命令来发送错误消息,因为它在装饰器中。以下是冷却装饰器的组织方式:

def cooldown(rate, per, bucket=Bucket.default):
    def decorator(func: FN) -> FN:
        if isinstance(func, Command):
            func._cooldowns.append(Cooldown(rate, per, bucket))
        else:
            func.__cooldowns__ = [Cooldown(rate, per, bucket)]
        return func

    return decorator

    def update_bucket(self, ctx):
        now = time.time()

        self._tokens = self.get_tokens(now)

        if self._tokens == 0:
            self._window = now

        if self._tokens == self._rate:
            retry = self._per - (now - self._window)
            raise CommandOnCooldown(command=ctx.command, retry_after=retry)


        self._tokens += 1

        if self._tokens == self._rate:
            self._window = now

class CommandOnCooldown(TwitchCommandError):
    def __init__(self, command, retry_after):
        self.command = command
        self.retry_after = retry_after
        super().__init__(f"Command <{command.name}> is on cooldown. Try again in ({retry_after:.2f})s")

你有什么想法吗?

【问题讨论】:

    标签: python bots irc twitch


    【解决方案1】:

    只要有错误,twitch io就会调用“event_command_error”

    您可以使用以下代码覆盖它:

        async def event_command_error(self, ctx, error: Exception) -> None:
        print(error)
    

    当然,这段代码只是打印错误,但是有了可用的 ctx,你应该可以做任何你想做的事情。愉快的黑客攻击。

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 2021-04-14
      • 2018-07-07
      • 1970-01-01
      • 2021-08-16
      • 2021-07-24
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多