【问题标题】:How can I use Discord.py to add a role to a specific user once they've levelled up?我如何使用 Discord.py 在特定用户升级后为其添加角色?
【发布时间】:2023-02-19 21:43:25
【问题描述】:

当有人升级时,我正在尝试为我的不和谐服务器上的用户添加一个角色,但我不断收到错误消息

“ AttributeError: 'NoneType' 对象没有属性 'roles'”

.这是相关代码:

intents = discord.Intents.all()
client = discord.Client(intents=intents)

async def assignRoles(message, level):
  server = bot.get_guild(<ID GOES HERE>)
  role = discord.utils.get(server.roles, name = "LEVEL 5-10")
  try:
    await message.author.id.add_roles(role)
  except Exception as e:
    print(e)
  else:
    print("hooray")

我尝试了很多在网上找到的不同方法,但都没有成功。提前感谢您的帮助!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    看起来 bot.get_guild 没有返回。您实际上是在向其中传递 ID 吗?

    server = bot.get_guild(YOUR_SERVER_ID)
    

    如果说仍然返回None,这意味着它不在缓存中,所以你必须fetch it

    server = await bot.fetch_guild(YOUR_SERVER_ID)
    

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多