【问题标题】:Check user for a role检查用户的角色
【发布时间】:2022-10-16 06:32:44
【问题描述】:

我正在尝试检查用户是否具有特定角色,如果他们具有该角色,他们可以使用该命令,但无论我是否具有该角色,我都会触发 else 。

代码

@bot.slash_command(name="test", description = "testing cmd")
async def test(ctx, member: discord.Member):
    role = "1023451893575450665"
    if get(member.roles, id=role):
        await ctx.send("your application has been accepted")
    else:
         await ctx.send("your application has been denied")

【问题讨论】:

  • role 是角色名称还是角色 ID?

标签: discord discord.py bots roles pycord


【解决方案1】:

如果您知道角色 ID,请使用 Member.get_role 方法。角色 id 应保留为整数,而不是字符串。

if member.get_role(role):
    await ctx.send("your application has been accepted")

如果您知道角色名称,则需要检查member.roles 属性,该属性是Roles 的列表

if len(member_role for member_role in member.roles if member_role.name == role]):
    await ctx.send("your application has been accepted")

【讨论】:

  • 这将适用于我正在测试的用户,但我希望该命令仅适用于具有特定角色的人,例如管理员角色?
猜你喜欢
  • 2020-08-04
  • 2011-11-05
  • 1970-01-01
  • 2016-08-28
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多