【问题标题】:Members role command成员角色命令
【发布时间】:2018-07-27 16:01:06
【问题描述】:

所以我一直试图让这个命令 $100ivShiny 给我的成员实际排名 100ivShiny。但我似乎找不到解决方案。

if message.content.startswith('$100ivShiny'):
      role = discord.utils.get(message.server.roles, name="100ivShiny")
      await client.add_role(user, role)
      await client.send_message(channel, "Role added")

这是我遇到的错误

  File "mod.py", line 29, in on_message
    await client.add_role(user, role)
AttributeError: '<class 'discord.client.Client'>' object has no attribute 'add_role'

【问题讨论】:

  • 这个错误会告诉你到底出了什么问题。 client 没有 add_role 方法。阅读 Discord 的 API 以了解调用的方法。

标签: python discord discord.py


【解决方案1】:

协程被命名为Client.add_roles,而不是Client.add_role。它会像

一样使用
if message.content.startswith('$100ivShiny'):
      role = discord.utils.get(message.server.roles, name="100ivShiny")
      await client.add_roles(user, [role])
      await client.send_message(channel, "Role added")

假设messageuserchannel 定义正确。

如果您要编写大量这样的命令,则应考虑使用 discord.ext.commands 扩展名,这意味着您不必将所有命令都保留在 on_message 事件中。

【讨论】:

    猜你喜欢
    • 2019-11-24
    • 2021-09-05
    • 2022-12-08
    • 2012-02-19
    • 1970-01-01
    • 2018-11-03
    • 2020-12-19
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多