【问题标题】:discord.py-rewrite music - Cannot use checkdiscord.py-rewrite 音乐 - 不能使用检查
【发布时间】:2019-05-30 23:19:41
【问题描述】:

所以,我有一个 Discord BOT,其音乐齿轮如下所示:

def setup(bot):

    class Music:

        __slots__ = ("players", )

        def __init__(self):
            self.players = {}

        def get_player(self, ctx):
            # Retrieve the guild player.

        def check_player(self):

            def predicate(ctx):
                # PREVIOUS CHECK
                player = self.get_player(ctx)
                # CHECK PLAYER STATE

            return commands.check(predicate)

        @commands.command()
        @check_player(self)
        async def a_command(self, ctx):
            pass

    bot.add_cog(Music())

但我不能使用check_player() 检查a_command() 命令,因为self 未定义。有人知道我该如何解决这个问题吗?

【问题讨论】:

  • 您需要将您的检查定义为一个只接受ctx 的函数。它不能是您的 cog 方法或依赖于self。 (因为检查的回调只注册一次,对于类的所有实例)。支票检查是什么?
  • get_player 是如何工作的?此行为可能无法进入check,它们主要用于您可以针对调用上下文进行的快速验证。

标签: python-3.x discord discord.py discord.py-rewrite


【解决方案1】:

@PatrickHaugh

def get_player(self, ctx):
    """Retrieve the guild player, or generate one."""
    try:
        player = self.players[ctx.guild.id]
    except KeyError:
        player = MusicPlayer(ctx, bot)
        self.players[ctx.guild.id] = player
    except:
        return None

    return player

这就是get_player() 检查的工作原理。

【讨论】:

  • 以后你应该在你的问题中编辑这样的东西。也就是说,这段代码绝对应该从命令本身调用,因为它的意义超出了基本检查。
猜你喜欢
  • 1970-01-01
  • 2021-05-05
  • 2021-02-06
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 2021-04-29
  • 2021-04-09
  • 2019-11-06
相关资源
最近更新 更多