【问题标题】:Cant give roles using python.py无法使用 python.py 赋予角色
【发布时间】:2017-08-22 03:14:28
【问题描述】:

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

@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'

【问题讨论】:

  • 我们将需要看到比这更多的代码。您应该提供一个Minimal, Complete, and Verifiable 示例。
  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。
  • 我猜 add_roles 在它的第二个参数中需要一个具有 id 属性(数据字段)的某种类型的对象。检查 add_roles 的文档或配置文件。

标签: python discord


【解决方案1】:

client.add_roles 需要一个角色对象。它不能接受纯字符串

要获得Role,您应该查看message.server.roles。请记住,message.server 可能是 None(在私人消息的情况下)。您不应该尝试创建自己的 Role 实例

【讨论】:

  • 抱歉,我查看了文档,但仍然没有弄清楚如何获取 Role 对象,我大多只是愚蠢。所以请你帮帮我
【解决方案2】:
# 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 号。还要确保将输入传递给正确的人或您尝试分配的任何人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2017-07-07
    • 2022-08-22
    • 2019-10-19
    • 2021-10-11
    相关资源
    最近更新 更多