discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:类型为“User”的参数不可iterable

2024-04-25 18:02:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个监狱命令,当你说一个!jail(ping)(以分钟为单位的整数)将为ping成员提供一个角色,该角色只允许访问一个通道。 一切都在discord服务器端完成

守则:

import time
from discord.ext import commands

DISCORD_TOKEN = "hahasikeyouwontgetm"

client = commands.Bot(command_prefix="a!")


@client.event
async def on_ready():
    guild_count = 0

    for guild in client.guilds:
        print(f"- {guild.id} (name: {guild.name})")

        guild_count = guild_count + 1

    print("AnythingBot is in " + str(guild_count) + " guilds.")


@client.command()
async def order(ctx, arg):
    if arg == "burger":
        embed = discord.Embed(
            title="here burger",
            color=ctx.author.color
        )
        embed.set_image(url='https://media.discordapp.net/attachments/839166049755856906/839166099336593428/burger.jpg'
                            '?width=670&height=670'
                        )
        await ctx.channel.send(embed=embed)


@client.command()
async def math(ctx, arg, arg2, arg3):
    mathed = 0
    answer = 0
    if arg2 == "+":
        answer = float(arg) + float(arg3)
        mathed = 1
    elif arg2 == "-":
        answer = float(arg) - float(arg3)
        mathed = 1
    elif arg2 == "/":
        answer = float(arg) / float(arg3)
        mathed = 1
    elif arg2 == "*":
        answer = float(arg) * float(arg3)
        mathed = 1
    embed = discord.Embed(
        title="The answer is: " + str(answer),
        color=ctx.author.color
    )
    if mathed == 1:
        await ctx.channel.send(embed=embed)


@client.command()
async def jail(ctx, arg, arg2):
    jailtime = int(arg2) * 60
    arg = arg.replace("<@!", "")
    arg = arg.replace(">", "")
    prisoner = await client.fetch_user(user_id=arg)
    isadmin = False
    admin = discord.utils.get(ctx.guild.roles, name='Admin')
    default = discord.utils.get(ctx.guild.roles, name='Member')
    jailed = discord.utils.get(ctx.guild.roles, name='Jailed')

    embedj = discord.Embed(
        title="Jailed " + prisoner.display_name + " for " + str(jailtime) + "m.",
        color=ctx.author.color
    )

    embedn = discord.Embed(
        title="You must be a staff member to use this command.",
        color=ctx.author.color,
        author="loser"
    )

    embedu = discord.Embed(
        title=prisoner.display_name + " has been unjailed.",
        color=ctx.author.color
    )

    if admin in ctx.message.author.roles:
        if admin in prisoner:
            isadmin = True

        await ctx.channel.send(embed=embedj)
        await client.add_roles(jailed)

        time.sleep(jailtime)

        await ctx.channel.send(embed=embedu)

        if isadmin:
            await client.add_roles(prisoner, admin)
        await client.add_roles(prisoner, default)
    else:
        await ctx.channel.send(embed=embedn)


client.run(DISCORD_TOKEN)

错误:

Traceback (most recent call last):
  File "C:\Users\domas\Downloads\DiscordBot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/domas/Downloads/DiscordBot/main.py", line 87, in jail
    if admin in prisoner:
TypeError: argument of type 'User' is not iterable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\domas\Downloads\DiscordBot\venv\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\domas\Downloads\DiscordBot\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\domas\Downloads\DiscordBot\venv\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 'User' is not iterable

第二个是:

其他一切似乎都起了作用,我感到困惑。 我怎样才能解决这个问题


Tags: nameinclientifargembedawaitfloat
2条回答

要修复您的代码,我有一些建议:

  • 我建议改用commands.MemberConverter来简化这一过程
  • 您给出的错误意味着prisoner无法迭代。您需要获取其.roles来迭代管理角色
  • 您不能通过client.add_roles向用户添加角色。你需要用discord.Member.add_roles()来代替
  • 不要使用time.sleep,而是使用await asyncio.sleep。异步程序的更好实践。它还可能阻止bot使用其他命令。但请确保导入asyncio

以下是jail命令的实现(我希望您可以使用这些建议来实现其余命令)

@client.command()
async def jail(ctx, prisoner:commands.MemberConverter, arg2:int):
    jailtime = arg2 * 60
    isadmin = False
    admin = discord.utils.get(ctx.guild.roles, name='Admin')
    default = discord.utils.get(ctx.guild.roles, name='Member')
    jailed = discord.utils.get(ctx.guild.roles, name='Jailed')

    embedj = discord.Embed(
        title="Jailed " + prisoner.display_name + " for " + str(jailtime) + "m.",
        color=ctx.author.color
    )

    embedn = discord.Embed(
        title="You must be a staff member to use this command.",
        color=ctx.author.color,
        author="loser"
    )

    embedu = discord.Embed(
        title=prisoner.display_name + " has been unjailed.",
        color=ctx.author.color
    )

    if admin in ctx.message.author.roles:
        if admin in prisoner.roles:
            isadmin = True

        await ctx.channel.send(embed=embedj)
        await prisoner.add_roles(jailed) # I wasn't sure exactly what you wanted here, so I just added the jailed role to the prisoner.

        await asyncio.sleep(jailtime)

        await ctx.channel.send(embed=embedu)

        if isadmin:
            await prisoner.add_roles(admin)
        await prisoner.add_roles(default)
    else:
        await ctx.channel.send(embed=embedn)

如果prisoner只是一个discord.User对象,则不能使用in。一份清单就行了

相关问题 更多 >