(不和.py)(Python)尝试添加角色时出现非类型对象错误

2024-05-16 19:36:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试有一个机器人为我的不和能够添加角色。它可以创建角色,但当我试图让它将角色赋予某人时,它就失败了。这是我的代码:

    await client.send_message(message.channel, 'Type in the name of the role you would like to have. This cannot be a role that already exists.')

    roleMessage = await client.wait_for_message(author = message.author)
    roleName = str(roleMessage.content)

    await client.create_role(client.get_server('insertserveridhere'), name = roleName, colour = discord.Colour.orange(), hoist = True, mentionable = True)
    userRole = discord.utils.get(message.server.roles, name = roleName)
    await client.add_roles(message.author, userRole)

我试过查看其他给人们角色的代码示例,它们看起来都很相似。我不知道是什么问题。每当我运行它,它会说:

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

它在添加角色的那一行写着。在

编辑:userRole似乎是add_roles行上的问题,但我不知道原因。在

编辑2:由于某些原因,将服务器id更改为另一个id,然后再更改它。在

编辑三:不知为什么,当我在同一台服务器上做了第二次它又坏了。我一点都不明白。在

编辑4:我让机器人在创建和添加角色之间说点什么,让它正常工作。我的理论是,当它试图添加角色时,它没有时间去注册它的存在。在


Tags: the代码nameclientid角色编辑message