【问题标题】:Discord bot check if user is adminDiscord bot 检查用户是否为管理员
【发布时间】:2018-12-16 20:41:26
【问题描述】:

您好,我是不和谐的 python 编码新手,我试图创建一个命令来告诉用户他们是否是管理员,但是很好......它一点也不工作

    @client.command(name="whoami",description="who are you?")
async def whoami():
    if message.author == client.user:
        return
    if context.message.author.mention == discord.Permissions.administrator:
        msg = "You're an admin {0.author.mention}".format(message)  
        await client.send_message(message.channel, msg)
    else:
        msg = "You're an average joe {0.author.mention}".format(message)  
        await client.send_message(message.channel, msg)

然后当我尝试输入 whoami 时得到这个

Ignoring exception in command whoami
Traceback (most recent call last):
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "<stdin>", line 3, in whoami
NameError: name 'message' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'message' is not defined

【问题讨论】:

  • 什么不起作用?有例外吗?
  • message 来自哪里?
  • 刚刚添加了我尝试激活命令时吐出的内容
  • 什么意思?对不起,如果很明显,我很新
  • 尝试将 context 添加到您的第一个 if 语句 if context.message.author == client.user:

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


【解决方案1】:

如果message.author.server_permissions.administrator 不起作用。

改成message.author.guild_permissions.administrator

或者试试message.author.top_role.permissions.administrator,这会返回一个布尔值。

有一件事是,通常服务器所有者将管理员设置为服务器最高角色,所以这在大多数情况下都会起作用。但如果他们不这样做,第三个 sol 就不起作用了。

【讨论】:

    【解决方案2】:

    您可以使用has_permissions check 查看用户是否具有administrator 权限。

    然后我们可以处理检查失败时抛出的错误,以便发送失败消息。

    from discord.ext.commands import Bot, has_permissions, CheckFailure
    
    client = Bot("!")
    
    @client.command(pass_context=True)
    @has_permissions(administrator=True)
    async def whoami(ctx):
        msg = "You're an admin {}".format(ctx.message.author.mention)  
        await ctx.send(msg)
    
    @whoami.error
    async def whoami_error(ctx, error):
        if isinstance(error, CheckFailure):
            msg = "You're an average joe {}".format(ctx.message.author.mention)  
            await ctx.send(msg)
    

    【讨论】:

    • 在错误处理程序中,参数似乎被交换了。必须说: async def whoami_error(ctx, error):
    • @PepeOchoa 这是一个旧答案,位置已被交换为 1.0 中的重大更改我已经更新了代码
    【解决方案3】:

    改变

    @client.command(name="whoami",description="who are you?")
    async def whoami():
    

    @client.command(pass_context=True)
    async def whoami(ctx):
    

    然后你可以使用ctx来获取各种东西,比如写它的用户、消息内容等等

    要查看User 是否为管理员,请执行
    if ctx.message.author.server_permissions.administrator:,如果用户是管理员,则应返回True

    您的代码应如下所示:

    import discord
    import asyncio
    from discord.ext.commands import Bot
    
    client = Bot(description="My Cool Bot", command_prefix="!", pm_help = False, )
    @client.event
    async def on_ready():
      print("Bot is ready!")
      return await client.change_presence(game=discord.Game(name='My bot'))
    
    @client.command(pass_context = True)
    async def whoami(ctx):
        if ctx.message.author.server_permissions.administrator:
            msg = "You're an admin {0.author.mention}".format(ctx.message)  
            await client.send_message(ctx.message.channel, msg)
        else:
            msg = "You're an average joe {0.author.mention}".format(ctx.message)  
            await client.send_message(ctx.message.channel, msg)
    client.run('Your_Bot_Token')
    

    【讨论】:

    • 这个答案应该更新:AttributeError: 'Member' object has no attribute 'server_permissions'
    • 它的 'guild_permissions' 现在
    猜你喜欢
    • 2019-06-01
    • 2018-02-02
    • 2011-04-05
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多