<bdo id='YY2FM'></bdo><ul id='YY2FM'></ul>

      <i id='YY2FM'><tr id='YY2FM'><dt id='YY2FM'><q id='YY2FM'><span id='YY2FM'><b id='YY2FM'><form id='YY2FM'><ins id='YY2FM'></ins><ul id='YY2FM'></ul><sub id='YY2FM'></sub></form><legend id='YY2FM'></legend><bdo id='YY2FM'><pre id='YY2FM'><center id='YY2FM'></center></pre></bdo></b><th id='YY2FM'></th></span></q></dt></tr></i><div id='YY2FM'><tfoot id='YY2FM'></tfoot><dl id='YY2FM'><fieldset id='YY2FM'></fieldset></dl></div>
    1. <tfoot id='YY2FM'></tfoot>
    2. <small id='YY2FM'></small><noframes id='YY2FM'>

    3. <legend id='YY2FM'><style id='YY2FM'><dir id='YY2FM'><q id='YY2FM'></q></dir></style></legend>
    4. 我怎样才能有两个相互对抗的命令装饰器?

      时间:2023-10-11
      <i id='yL7FZ'><tr id='yL7FZ'><dt id='yL7FZ'><q id='yL7FZ'><span id='yL7FZ'><b id='yL7FZ'><form id='yL7FZ'><ins id='yL7FZ'></ins><ul id='yL7FZ'></ul><sub id='yL7FZ'></sub></form><legend id='yL7FZ'></legend><bdo id='yL7FZ'><pre id='yL7FZ'><center id='yL7FZ'></center></pre></bdo></b><th id='yL7FZ'></th></span></q></dt></tr></i><div id='yL7FZ'><tfoot id='yL7FZ'></tfoot><dl id='yL7FZ'><fieldset id='yL7FZ'></fieldset></dl></div>

            <tbody id='yL7FZ'></tbody>
        1. <legend id='yL7FZ'><style id='yL7FZ'><dir id='yL7FZ'><q id='yL7FZ'></q></dir></style></legend>
          • <small id='yL7FZ'></small><noframes id='yL7FZ'>

            • <tfoot id='yL7FZ'></tfoot>

                <bdo id='yL7FZ'></bdo><ul id='yL7FZ'></ul>
                本文介绍了我怎样才能有两个相互对抗的命令装饰器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个命令可以删除用户输入的指定数量的消息.我希望只有我和具有管理员角色的人才能访问此命令.我以前用 if 语句实现过这个,它工作得很好.但是,现在我正在尝试使用命令装饰器来做同样的事情,它只允许管理员使用命令 - 而不是我.这是我正在使用的代码:

                I have a command that deletes a specified number of messages entered in by the user. I want this command to be accessible only to me and those with the administrator role. I had implemented this before with if-statements, and it was working perfectly fine. However, now I'm trying to use command decorators to do the same, and it only lets administrators use the command - not me. Here's the code I'm working with:

                @bot.command(description="clears entered amount of messages")
                @commands.is_owner() # checks if user is owner
                @commands.has_permissions(administrator=True) # checks if user is admin
                async def delete(ctx, amount : int):
                    await ctx.channel.purge(limit = amount + 1)
                

                我认为 @commands.has_permissions(administrator=True) 阻止了我使用该命令,因为我不是其中一台服务器的管理员.我试过切换他们的订单,is_owner() 检查低于 has_permissions() 检查;尽管如此,它仍然不允许我使用该命令.如何使用装饰器克服这个问题?

                The @commands.has_permissions(administrator=True), I think, is blocking me from using the command, since I'm not an administrator in one of the servers. I've tried switching their orders, with the is_owner() check being below the has_permissions() check; nonetheless, it still doesn't allow me to use the command. How can I overcome this using decorators?

                推荐答案

                您可以创建一个自定义装饰器来检查您是机器人的所有者还是管理员

                You can create a custom decorator that will check if you're either the owner of the bot, or you are an admin

                def owner_or_admin():
                    def predicate(ctx):
                        owner = ctx.author.id == bot.owner_id # Comparing the author of the message with the owner of the bot
                        perms = ctx.author.guild_permissions.administrator # Checking for admin perms
                        return owner or perms
                    return commands.check(predicate)
                
                
                @bot.command(description="clears entered amount of messages")
                @owner_or_admin()
                async def delete(ctx, amount : int):
                    await ctx.channel.purge(limit = amount + 1)
                

                我刚刚发现 commands.check_any - 检查是否有任何通过的检查将通过,即使用逻辑 OR.

                @bot.command()
                @commands.check_any(commands.is_owner(), commands.has_permissions(administrator=True))
                async def delete(ctx, amount: int):
                    await ctx.channel.purge(limit=amount + 1)
                

                这篇关于我怎样才能有两个相互对抗的命令装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何响应 discord.py 中的错误用户命令 下一篇:根据 UTC 时间制作 discord.py(重写版本)计时器

                相关文章

                最新文章

                  <small id='NJRMv'></small><noframes id='NJRMv'>

                    • <bdo id='NJRMv'></bdo><ul id='NJRMv'></ul>

                    <tfoot id='NJRMv'></tfoot>
                    <i id='NJRMv'><tr id='NJRMv'><dt id='NJRMv'><q id='NJRMv'><span id='NJRMv'><b id='NJRMv'><form id='NJRMv'><ins id='NJRMv'></ins><ul id='NJRMv'></ul><sub id='NJRMv'></sub></form><legend id='NJRMv'></legend><bdo id='NJRMv'><pre id='NJRMv'><center id='NJRMv'></center></pre></bdo></b><th id='NJRMv'></th></span></q></dt></tr></i><div id='NJRMv'><tfoot id='NJRMv'></tfoot><dl id='NJRMv'><fieldset id='NJRMv'></fieldset></dl></div>
                    <legend id='NJRMv'><style id='NJRMv'><dir id='NJRMv'><q id='NJRMv'></q></dir></style></legend>