简体   繁体   English

为什么即使 ID 有效,我的代码也检测不到公会中的成员?

[英]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.rolesdiscord.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.

相关问题 Discord.py:从不与机器人共享公会的成员的用户名中获取用户 ID - Discord.py: Obtaining user ID from username of member who doesn't share a guild with the bot Discord.py client.get_guild(ID) 返回 NoneType 即使它是公会的成员 - Discord.py client.get_guild(ID) returns NoneType even though it is a member of a guild Python 3.8.2 | 为什么我的输入不接受我的答案,即使它是有效的? (功能) - Python 3.8.2 | Why does my input doesn't accept my answer even if it's a valid one? (function) 为什么doctest没有检测到我的测试? - Why doesn't doctest detect my tests? discord.Guild.id 只会返回<member 'id' of 'Guild' objects> - discord.Guild.id will only return <member 'id' of 'Guild' objects> 知道为什么hackerrank 不接受我的代码,即使它准确打印了要求的内容 - Any idea why hackerrank doesn't accept my code even if it prints exactly what is asked on_guild_join 数据未保存在我的数据库中 - on_guild_join data doesn't save on my database 为什么在 memory 中移动列表,列表的 id 没有变化? - Why doesn't the id of a list change even if the list is moved in memory? 为什么我的脚本不接受有效日期作为用户输入? - Why doesn't my script accept a valid date as user input? 为什么此代码未检测到输入中的空格? - Why does this code doesn't detect space in the input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM