【问题标题】:Trying to check for a role in a member's acquired roles list [duplicate]尝试检查成员获得的角色列表中的角色[重复]
【发布时间】:2021-05-22 15:52:05
【问题描述】:

所以我试图让代码检查成员角色列表中的角色。但这个简单的任务结果比我想象的要难。这是我的代码:

@client.event
async def on_message(message):
  if message.author.bot:
    return
  else:
    if message.content == "":
      return
    else:
      role = discord.utils.get(message.guild.roles, name='Bullied')
      member = message.guild.get_member(message.author.id)
      if role in member.roles:
        await message.channel.send(f'Hey there {message.author.name}')
      else:
        return

我收到了这个错误:

AttributeError: 'NoneType' object has no attribute 'roles'

【问题讨论】:

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


【解决方案1】:

为什么你从 member 对象获取 id,然后从 id 获取相同的 member 对象。只需这样做:

member = message.author

此外,您的代码中有一些不必要的嵌套。这个:

@client.event
async def on_message(message):
    if message.author.bot or message.content == "":
        return
    role = discord.utils.get(message.guild.roles, name="Bullied")
    member = message.author
    if role in member.roles:
        await message.channel.send(f"Hey there {member.name}")
    return

看起来干净多了。

【讨论】:

  • 谢谢!我会接受你的改变,但这并不是我真正想要的。我的问题已经解决了:)
猜你喜欢
  • 2019-10-19
  • 2013-03-31
  • 2022-10-23
  • 2010-09-17
  • 1970-01-01
  • 2011-12-11
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多