【问题标题】:How can I make my bot check if the author has administrator and send a message if they don't?如何让我的机器人检查作者是否有管理员,如果没有,则发送消息?
【发布时间】:2020-06-26 13:40:24
【问题描述】:

我创建了一个我希望只有管理员可以访问的命令。如果用户有管理员,则运行特定代码。如果没有,我希望机器人发送一条消息,如“抱歉,你不能使用它”。有没有办法做到这一点?

【问题讨论】:

    标签: discord discord.py discord.py-rewrite


    【解决方案1】:
        @commands.command()
        @commands.has_permissions(administrator=True)
        async def command(self, ctx):
              #code here
    
    

    只会使命令command admin。 至于警告用户尝试运行他们没有权限的命令,您可以使用错误处理程序/命令特定的错误处理程序。 比如

        @commands.Cog.listener()
        async def on_command_error(self, ctx, error):
            if isinstance(error, commands.errors.MissingPermissions):
                msg.title = "Missing Permission ⛔"
                msg.description = error.args[0]
                return await ctx.send(embed=msg)
    

    对于特定于 cog 的错误,使用 commandname.error() 并捕获如上所示的错误。

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 2019-02-01
      • 1970-01-01
      • 2021-01-24
      • 2019-09-19
      • 2019-11-17
      • 2021-03-19
      • 2018-09-14
      • 1970-01-01
      相关资源
      最近更新 更多