不能使用python.py给角色

时间:2017-03-28 21:00:46

标签: python discord

我正在尝试给人角色。我确保我的机器人可以添加和删除角色。这是我的代码:

@client.event
async def on_message(message):
    await client.add_roles(message.author, "role id")

错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Jack Eagles\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:/Users/Jack Eagles/PycharmProjects/Discord Bot/Main.py", line 71, in on_message
    await client.add_roles(message.author, role)
  File "C:\Users\Jack Eagles\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 2907, in add_roles
    new_roles = utils._unique(role.id for role in itertools.chain(member.roles, roles))
  File "C:\Users\Jack Eagles\AppData\Roaming\Python\Python36\site-packages\discord\utils.py", line 232, in _unique
    return [x for x in iterable if not (x in seen or adder(x))]
  File "C:\Users\Jack Eagles\AppData\Roaming\Python\Python36\site-packages\discord\utils.py", line 232, in <listcomp>
    return [x for x in iterable if not (x in seen or adder(x))]
  File "C:\Users\Jack Eagles\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 2907, in <genexpr>
    new_roles = utils._unique(role.id for role in itertools.chain(member.roles, roles))
AttributeError: 'str' object has no attribute 'id'

2 个答案:

答案 0 :(得分:1)

client.add_roles需要一个Role对象。它不能接受普通字符串

要获得Role,您应该查看message.server.roles。请注意,message.server可能是None(如果是私信)。您不应该尝试创建自己的Role

实例

答案 1 :(得分:0)

# When a member joins, the bot DM's them a message
@client.event
async def on_member_join(member):
await client.send_message(member, 'Welcome to The Hangout {}, Enjoy your 
stay! Also, Check out the Icy Rules Homie!! :cookie:'.format(member.name))

# Assigns the joining member with a role
role = discord.utils.get(member.server.roles, id="")
await client.add_roles(member, role)

确保将您尝试提供给该人的角色的ID号。另外,请确保将输入传递给合适的人或您尝试分配的人。