[英]Why doesn't my code detect the member in the guild even with a valid ID?
For reference, memID is initialized to message.author.id作为参考,memID 被初始化为 message.author.id
if message.content.startswith(prefix+'jail'):
serv = client.get_guild(message.guild.id)
mod = serv.get_member(memID)
if 'Moderator' in mod.roles:
await message.channel.send("Signal")`
I even debugged to print memID and it printed out my ID, however this function does not detect me as a member even with the valid ID, instead initializing to NoneType.我什至调试打印 memID 并打印出我的 ID,但是这个 function 即使具有有效 ID 也没有将我检测为成员,而是初始化为 NoneType。 Help.
帮助。
Error Log:错误日志:
File "/home/runner/NKBot/venv/lib/python3.8/site-packages/discord/client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "main.py", line 81, in on_message
if 'Moderator' in mod.roles:
AttributeError: 'NoneType' object has no attribute 'roles'
Firstly, the members intent
was not enabled, so your discord.Member
instance couldn't be found.首先,
members intent
未启用,因此找不到您的discord.Member
实例。 See the docs for how to enable them: https://discordpy.readthedocs.io/en/stable/intents.html请参阅文档以了解如何启用它们: https://discordpy.readthedocs.io/en/stable/intents.html
Next, you're checking if a string ( "Moderator"
) is in mod.roles
.接下来,您要检查字符串 (
"Moderator"
) 是否在mod.roles
中。 This is always False
, because Member.roles
is a list of discord.Role
instances, not strings.这始终为
False
,因为Member.roles
是discord.Role
实例的列表,而不是字符串。 You can also find this in the docs: https://discordpy.readthedocs.io/en/stable/api.html?highlight=member%20roles#discord.Member.roles您还可以在文档中找到它: https://discordpy.readthedocs.io/en/stable/api.html?highlight=member%20roles#discord.Member.roles
You can find the role by name using a loop, or more easily using the built-in utils.get()
method.您可以使用循环按名称查找角色,或者使用内置的
utils.get()
方法更轻松。 The docs page has plenty of examples that explain how it works.文档页面有很多示例来解释它是如何工作的。
Lastly, instead of manually parsing message content consider just using the built-in Commands
framework that does all this for you.最后,与其手动解析消息内容,不如考虑使用内置的
Commands
框架来为您完成所有这些工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.