【问题标题】:Make a discord bot with Python用 Python 制作一个不和谐的机器人
【发布时间】:2021-03-27 15:42:03
【问题描述】:
@client.event
async def on_message(message):
    if message.content.startswith('bot, '):
        if message.content[5:].startswith(('clear')):
            if message.author.guild_permissions.administrator:
                amount=int(message.content[10:])
                await message.channel.purge(limit=amount)
            else:
                await message.channel.send('not enough energy.')

除了管理员, 我想让某些角色执行命令。 我该怎么办?

角色名称 = 不是管理员 id = 825183755484266529

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

您可以获得message.author.roles,这是listRoles。然后,您可以迭代此列表并检查是否有任何角色具有“管理员”角色 ID。示例:

ADMIN_ROLE_ID = 1234567890
def can_perform_commands(member):
    for role in member.roles:
        if role.id == ADMIN_ROLE_ID:
            return True
    return False

然后你可以像这样使用这个函数:

if can_perform_commands(message.author):
    # do whatever the command does
else:
    message.channel.send("I'm sorry, but you don't have permission to use this command.")

【讨论】:

  • 对不起,我不明白
  • message.author.roles 是一个列表。它包含消息作者的角色。第一段代码定义了一个函数,它查看这个列表并检查任何角色 ID 是否与管理员角色的 ID 相同。如果有一个 - 这意味着用户具有管理员角色 - 它返回 True。在其他情况下,它返回 False。第二个代码调用此函数并将消息作者传递给它。如果它返回 True,它会继续执行命令正常执行的操作。否则,它会发送一条关于用户没有足够权限的消息。
猜你喜欢
  • 2021-05-17
  • 1970-01-01
  • 2022-07-12
  • 2021-10-30
  • 2021-05-27
  • 2021-07-15
  • 2023-04-04
  • 2022-01-26
  • 2021-05-24
相关资源
最近更新 更多