gpt4 book ai didi

python - Discord 机器人检查用户是否是管理员

转载 作者:行者123 更新时间:2023-12-01 01:46:45 24 4
gpt4 key购买 nike

嗨,我是使用不和谐的 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.author.server_permissions.administrator不起作用。

将其更改为message.author.guild_permissions.administrator

或者尝试message.author.top_role.permissions.administrator,这将返回一个 bool 值。

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

关于python - Discord 机器人检查用户是否是管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51240878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com