Discord机器人如何检查用户是否为管理员

6

你好,我是一个新手,正在使用discord进行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

2
什么出了问题?有异常吗? - Sasha
message 是从哪里来的? - rak007
刚刚添加了一个命令,当我尝试激活该命令时,它会输出什么内容。 - Finlay Metcalfe
你是什么意思?抱歉,我很新手,如果显而易见的话。 - Finlay Metcalfe
尝试在您的第一个if语句中添加_context_ if context.message.author == client.user: - Tristo
我在 message.author 前面加了 context.,但问题没有解决。 - Finlay Metcalfe
3个回答

6
如果 message.author.server_permissions.administrator 无效,请改为 message.author.guild_permissions.administrator
或者尝试 message.author.top_role.permissions.administrator,它将返回一个布尔值。
需要注意的是,通常情况下服务器所有者会将管理员权限设置给服务器最高角色,因此大多数情况下第二个方法都可以使用。但如果他们没有这样做,第三种方法就无效了。

5
你可以使用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): - Pepe Ochoa
1
@PepeOchoa 这是一个旧答案,位置在1.0中作为破坏性更改而交换。我已经更新了代码。 - Patrick Haugh

4

更改

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

to

@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')

5
此答案需要更新: AttributeError: 'Member'对象没有'server_permissions'属性。 - Safak Ozdek
3
它现在是 'guild_permissions'。 - Client

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接