【问题标题】:discord.py testing for roles not working. always returns falsediscord.py 测试角色不起作用。总是返回 false
【发布时间】:2019-10-25 01:27:32
【问题描述】:

我想测试一个用户是否具有两个特定角色之一,但由于某种原因,我编写的代码总是返回 false。

我还尝试了另一种方法,它总是返回 true。在 discord.py discord 服务器上也没有得到答案

#first approach. always returns false
def isMod(user):
    for role in user.roles:
        if role.id==587614886771687445:
            return True
        elif role.id==587615608279924756:
            return True
    return False



#second approach. always returns true
def isMod(user):
    if discord.utils.get(user.roles, id=587614886771687445) is None and discord.utils.get(user.roles, id=587615608279924756):
        return False
    else:
        return True

如果用户具有给定 id 的一个或两个角色,它应该返回 true;如果没有,则返回 false。

【问题讨论】:

  • user 是您期望的用户吗?
  • 您使用的是什么版本的 discord.py?您可以print(discord.__version__)查看。
  • 我使用的是 0.16.12

标签: python bots discord roles discord.py


【解决方案1】:

我得到了可以在我的系统上运行的代码。您可以在此处尝试一些方法来解决此问题。

  1. 如何获取用户名?尝试使用以下内容打印出用户名:
@client.event
async def on_message(message):
      if message.content.startswith('$'):
            user = message.author
            print(user)

使用 $ 键入任何内容以调用此消息。如果用户被正确打印,下一步尝试打印角色,

print(user.roles)

这应该返回一个带有“Role id”和“Name”的可迭代对象。验证角色 ID 是否正确传递。

如果仍有问题,请在 hastebin 或其他地方分享您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多