对此感到困惑 Discord.py Rewrite + Reaction Light Code - 需要解释

Confused on this Discord.py Rewrite + Reaction Light Code - Need Explanation

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

我正在使用 Discord.py Rewrite, and a chunk of code called Reaction Light in order to create a bot that allows for self-assigning roles in my 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

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

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

所以,我想弄清楚什么时候或什么时候不使用这个 msg 参数,如果它已经在检查用户是否是监听器中的管理员,我是否必须检查 again 在实际命令本身?是不是有点多余?

这是 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

老实说,我不确定为什么会这样。如果您可以访问 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)

在我看来,use/understand 更简洁、更容易。