discord.py - 我试图创建一个加入角色功能,但它不起作用
问题描述
所以它基本上是这样的:我正在尝试创建一个可以从特定角色添加成员的函数,并且该函数将查看该成员是否在该角色中或者该角色是否存在以及最大成员这个角色是五个,但代码似乎不起作用
@commands.command()
async def join_team(self,ctx,rule:discord.Role):
role = ctx.guild.get_role(rule) # check this specific role
if ctx.author in role:
await ctx.send("you're already in that role !")
elif len(role.members) == 5:
await ctx.send("this specific role is full")
else:
await self.client.add_roles(ctx.author, role)
我得到的错误:
Ignoring exception in command join_team:
Traceback (most recent call last):
File "E:\PYTHON\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\Name\Desktop\scripts\python's\discordbot\cogs\cog1.py", line 29, in join_team
if ctx.author in role:
TypeError: argument of type 'NoneType' is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\PYTHON\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "E:\PYTHON\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "E:\PYTHON\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'NoneType' is not iterable
解决方案
您的代码中只有一个逻辑错误。
您正在检查的内容是否在author
这里in
没有role
任何意义。
反过来检查它:
if role in ctx.author.roles:
# Do what you want to do
推荐阅读
- pg-promise - 如何处理 ParameterizedQuery 的错误?
- rasa-nlu - 如果单词中有下划线,如何修复 Rasa Nlu Confidence 给 0?
- splunk - Splunk 图表函数在尝试舍入输入时显示零值
- c - For 循环运行太频繁
- twitter-bootstrap - 在 Bootstrap 4 中,Glyphicons 不起作用?
- r - 使用 data.table 写入和加载 JSON 字符串
- jenkins - Jenkins 共享库中的 Groovy 范围界定
- perl - Perl 行为差异关闭由 open() 产生的子进程与 IPC::Open3
- linux - 为什么 go 应用程序作为多个进程运行?
- db2 - 如果第一个为空,则选择语句返回一个值或另一个值