我开始制造一个机器人,该机器人为服务器中的所有用户提供角色。我决定从用户在服务器中输入特定消息时赋予角色开始。这是我正在使用的代码:
if message.content == 'test':
member = message.author
role = discord.utils.get(member.guild.roles, name="test")
await discord.Member.add_roles(member, role)
但是我遇到了一个错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "E:\Program File\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "E:\folder for all folders\python\program\bot.py", line 73, in on_message
await discord.Member.add_roles(member, role)
File "E:\Program File\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'
我对在这里做什么感到迷茫。
答案 0 :(得分:4)
这是正确的代码-
if message.content == 'test':
member = message.author
role = discord.utils.get(message.guild.roles, name="test")
await member.add_roles(role)
注意-要求服务器具有名为test
的角色。
如果可行,请考虑使用accepting this as an answer。谢谢!
答案 1 :(得分:0)
我不太擅长Python,但是当您进入文档时,它会告诉您add_roles具有3个参数角色,原因和原子。您不必放置成员。 https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.add_roles