python - 检查用户是否具有特定角色

标签 python python-3.x discord discord.py

我有代码,您可以在其中输入 -giverole <user> <rolename>例如-giverole @Soup Board of Executives .
我现在需要的是一种方法来检查输入命令的用户是否具有特定角色。

我有可以为某人分配角色的代码:

@client.command(pass_context=True)
async def giverole(ctx, member: discord.Member, *, role: discord.Role):
    await client.add_roles(member, role)
    await client.say("The role '" + str(role) + "' has been given to " + member.mention + " .")

应该可以await client.say()如果用户有正确的等级。如果不这样做,则会引发错误消息。

最佳答案

您可以使用discord.Member.roles做类似的事情

from discord.utils import get

@client.command(pass_context=True)
async def giverole(ctx, member: discord.Member, *, role: discord.Role):
  check_role = get(ctx.message.server.roles, name='Board of Executives')
  if check_role not in member.roles:
    await client.say(f"You don't have the role '{str(role)}'")
  else:
    await client.add_roles(member, role)
    await client.say(f"The role '{str(role)}' has been given to {member.mention}.")

关于python - 检查用户是否具有特定角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54324466/

相关文章:

Python 3 中忽略的 Python 单元测试预期失败

python - 如何删除列表中的第一项和最后一项?

Python argparse 到游戏

python-3.x - 制作一个包含 (i,i*i) 的字典,其中 i 是从 1 到 n

python - 如何使用 python discord api 中的 add_roles 方法

python - 你能在 Discord 机器人上获得 "About me"功能吗? (不和谐.py)

python - 无 类型不可迭代 (RecurrentTabularExplainer)

python - 在我们连接之前,浏览器似乎已经退出。输出为 : mkdir: cannot create directory

python-3.x - 用另一个数据框的行部分替换数据框

c# - 如何将 DiscordSharpPlus 中的 DiscordColor 设置为随机 RGB 颜色?