首页 > 解决方案 > 在服务器上添加角色

问题描述

我打算,如果你给机器人发送一个特定的 DM,它就会在特定的服务器上赋予一个角色。

这是我尝试过的代码:

@bot.event
async def on_message(message):
  if message.author.bot:
        return
  elif message.content.startswith("++neunundvierzig++"):
        role = message.author.guild.get_role(861543456908640298)
        guild = bot.get_guild(816021180435529758)
        await message.channel.send("Erfolgreich Zerifiziert!")
        await member.add_roles(role)

它在日志中说:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\aaa12\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "f:\HTML CSS JS Py Projekte\Python\bot_check\bot.py", line 36, in on_message
    role = message.author.guild.get_role(861543456908640298)
AttributeError: 'User' object has no attribute 'guild'

标签: pythondiscord.py

解决方案


我认为你定义role错误。您的代码说不message.author.guild存在,所以您应该尝试message.guild.

像这样:

role = message.guild.get_role(861543456908640298)

除此之外,您没有定义member. 只需将其重命名await message.author.add_roles(role)await member.add_roles(role)


来源


推荐阅读