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

新手上路,请多包涵

嗨,我是使用 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

原文由 Finlay Metcalfe 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 755
2 个回答

改变

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

原文由 Tristo 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

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

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

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

原文由 Kouheng 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题