python - 在服务器上添加角色
问题描述
我打算,如果你给机器人发送一个特定的 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'
解决方案
我认为你定义role
错误。您的代码说不message.author.guild
存在,所以您应该尝试message.guild
.
像这样:
role = message.guild.get_role(861543456908640298)
除此之外,您没有定义member
. 只需将其重命名await message.author.add_roles(role)
为await member.add_roles(role)
来源
推荐阅读
- spring - HTTP 状态 500 - 请求处理失败;嵌套异常是 org.springframework.web.client.HttpClientErrorException: 403 null type
- java - 为什么 setOnChangeListener 不能正常工作?
- javascript - 如何通过Javascript解析没有密钥的多维JSON数据?
- scala - 更改给定 scala 函数中的类型
- windows - Chrome 扩展程序中的本机消息不起作用
- php - 在 docker 中使用 magento/bin setup:di:compile 耗尽 PHP 内存大小
- typescript - ionic 4 存储注入错误:没有提供程序
- axios - 使用 Nuxt.js 进行开发和生产的不同 baseURL
- android - Pipelines:mavenLocal 位于 Pipelines 的什么位置?
- sql - 如何解决在 localhost 中正常运行的服务器中的查询问题?