首页 > 解决方案 > 我试图创建一个加入角色功能,但它不起作用

问题描述

所以它基本上是这样的:我正在尝试创建一个可以从特定角色添加成员的函数,并且该函数将查看该成员是否在该角色中或者该角色是否存在以及最大成员这个角色是五个,但代码似乎不起作用

    @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

标签: discord.pypython-3.10

解决方案


您的代码中只有一个逻辑错误。

您正在检查的内容是否在author这里in没有role任何意义。

反过来检查它:

if role in ctx.author.roles: 
 # Do what you want to do

推荐阅读