0

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

    @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
4

2 回答 2

1

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

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

反过来检查它:

if role in ctx.author.roles: 
 # Do what you want to do
于 2021-10-10T21:51:49.937 回答
0

你的逻辑和代码都是错误的。您已经在参数上获得了角色,但仍将其传递给,get_role()并且您还在检查作者是否在角色对象中,而不是该角色的成员。Bot 对象没有add_roles()功能。

    @commands.command()
    async def join_team(self, ctx, role: discord.Role):

        if role in ctx.author.roles:
            await ctx.send("you're already in that role !")

        elif len(role.members) == 5:
            await ctx.send("this specific role is full")

        else:
            await ctx.author.add_roles(role)
于 2021-10-11T16:07:11.877 回答