【问题标题】:Confused on this Discord.py Rewrite + Reaction Light Code - Need Explanation对这个 Discord.py Rewrite + Reaction Light 代码感到困惑 - 需要解释
【发布时间】:2020-06-17 12:12:53
【问题描述】:

因此,虽然我们都知道整个复制和粘贴其他人的代码,而且它神奇地工作,但在弄清楚这段代码的实际工作原理和功能时,它会失去一些上下文和理解。

我正在使用Discord.py Rewrite 和一段名为Reaction Light 的代码来创建a bot,它允许在我的Discord Server 中自分配角色。该机器人 100% 正常运行并按预期工作。现在,我已经从他们的代码中改变了一些东西,所以我的方法位于不同的位置并从不同的区域调用。

这就是我感到困惑的地方:

我有一个名为isadmin() 的方法,只要需要完成检查以确定发出命令的用户是否是管理员,就会调用此方法。管理员角色在我使用 dotenv 模块检索的 .env 文件中定义。很直接的东西。 (我稍后会重写它的那一部分,并希望将所有管理员角色 id 放入一个文件中并从那里获取它们。)但是我对这个方法的第三个参数究竟做了什么感到困惑。 msg=False

每当我为此编写 cogs 时,我都会直接调用此方法,而无需将 'msg' 参数传递给它,如下所示:

admin.py

# admin.py
# Simple ping pong command
@commands.command()
    async def ping(self, ctx):
        if helpers.isadmin(ctx):
            print("Running Command from admin.py")
            await ctx.send('Pong!')

现在在我的on_message() 侦听器方法中,它传递msg 参数,然后执行一些与 ping 命令无关但与机器人自分配角色部分的功能相关的代码。

message.py

# message.py
@commands.Cog.listener()
    async def on_message(self, message):
        if helpers.isadmin(message, msg=True):
            # Execute some code here for the self-assigning roles

据我所知,此工作流程的工作方式是这样,我将使用 ping 命令作为示例,该命令由 r.ping 命令调用。

  1. on_message() 侦听服务器中发送的所有消息
  2. 用户在频道中发出 Ping 命令
  3. on_message() 听到消息并检查用户是否是管理员,但也传递了msg 参数
  4. admin.py 调用 ping 命令,然后检查(再次?)用户是否是管理员,如果他/她是,则执行该命令。

所以,我试图弄清楚何时或何时不使用这个 msg 参数,如果它已经在检查用户是否是侦听器中的管理员,我是否必须再次检查 在实际的命令中?是不是有点多余?

这是helpers.py 文件中的isadmin() 方法

# helpers.py
def isadmin(self, ctx, msg=False):
        # Checks if command author has one of .env admin role IDs
        try:
            check = (
                [role.id for role in ctx.author.roles]
                if msg
                else [role.id for role in ctx.message.author.roles]
            )
            if self.admin_a in check or self.admin_b in check or self.admin_c in check:
                return True
            return False
        except AttributeError:
            # Error raised from 'fake' users, such as webhooks
            return False

【问题讨论】:

    标签: python bots chatbot discord.py discord.py-rewrite


    【解决方案1】:

    老实说,我不确定为什么会出现这种情况。如果您有权访问ctx.author,则您可以访问ctx.message,因为ctx.author 只是该消息的作者,似乎是多余的。但是我强烈建议您为此使用checks。例如我有:

    def is_owner():
        def predicate(ctx):
            return ctx.author.id in ctx.bot.config()["owners"]
        return commands.check(predicate)
    

    我把它用作装饰器

    # utils/checks/checks.py
    from utils.checks import checks
    
    @checks.is_owner()
    @commands.group(hidden=True, case_insensitive=True, description="Load a module")
    async def load(self, ctx):
        if not ctx.invoked_subcommand:
            return await ctx.send_help(ctx.command)
    

    例如你可以有

    def is_admin():
        def predicate(ctx):
            role_id = 123123123123123 # replace this with os.getenv("wherever your admin role is")
            return role_id in [x.id for x in ctx.author.roles]
        return commands.check(predicate)
    

    在我看来,它更简洁、更易于使用/理解。

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多