Python Discord bot检查用户是否为管理员

Python Discord bot检查用户是否为管理员,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,您好,我对使用discord编写python代码还不熟悉,我已经尝试过制作一个命令,告诉用户他们是否是管理员,但是。。。它一点也不起作用 @client.command(name="whoami",description="who are you?") async def whoami(): if message.author == client.user: return if context.message.author.mention == disco

您好,我对使用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
忽略命令whoami中的异常
回溯(最近一次呼叫最后一次):
文件“/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py”,第50行,包装
ret=来自coro的收益(*args,**kwargs)
文件“”,第3行,在whoami中
NameError:未定义名称“消息”
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“/home/python/.local/lib/python3.6/site packages/discord/ext/commands/bot.py”,第846行,进程内命令
从command.invoke(ctx)中获得收益
文件“/home/python/.local/lib/python3.6/site packages/discord/ext/commands/core.py”,第374行,在invoke中
注入的产量(*ctx.args,**ctx.kwargs)
文件“/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py”,第54行,包装为
将CommandInvokeError(e)从e中提升
discord.ext.commands.errors.CommandInvokeError:命令引发异常:NameError:未定义名称“message”
更改

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

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

要查看
用户是否是管理员,请执行
如果ctx.message.author.server\u权限。管理员:
如果用户是管理员,则应返回
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')
您可以使用查看用户是否具有该权限

然后,我们可以处理该检查失败将引发的错误,以便发送失败消息

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 client.send_message(ctx.message.channel, msg)

@whoami.error
async def whoami_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "You're an average joe {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)

如果
message.author.server\u permissions.administrator
不起作用

将其更改为
message.author.guild\u permissions.administrator

或者尝试
message.author.top\u role.permissions.administrator
,这将返回bool


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

什么不起作用?有例外吗?
消息
来自哪里?只是添加了我尝试激活命令时发出的信息。你是什么意思?抱歉,我是一个非常新的人,如果它明显地将上下文添加到您的第一个if语句
if context.message.author==client.user:
应该更新此答案:AttributeError:'Member'对象没有属性'server\u permissions'它的'guild\u permissions'
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 client.send_message(ctx.message.channel, msg)

@whoami.error
async def whoami_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "You're an average joe {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)