【问题标题】:discord.py - 'command' is not iterable?discord.py - “命令”不可迭代?
【发布时间】:2021-01-19 02:28:55
【问题描述】:

我是 python 新手,如果这是一个菜鸟问题,我很抱歉制作不和谐的机器人,但我被困住了。

花了好几个小时才弄明白。

我正在编写一个简单的机器人,它将遍历 28 个对象的列表并随机选择其中的 4 个。然后将这 4 个选项发送到聊天中,以便人们为他们的选择投票。

昨晚我在使用

@client.event
async def on_message(message):
        if message.author == client.user:
                return

        if message.content.startswith('!maps'):
                await message.delete()
                channel = client.get_channel(800086126173225010)
                await channel.send('**New poll! Vote below now for tomorrow\'s map!**')
                choice = random.sample(maps,4)

                for x in range(0, 4):

                        mapemp.append(emoji[x]+" - "+choice[x])

                msg = await channel.send('\n\n'.join(mapemp))

                for x in range(0,4):
                
                        await msg.add_reaction(emoji[x]) 
                mapemp.clear()

这很好用。但后来我发现了@bot.command 而不是@client.event 所以我试图切换到那个。但是,当我尝试运行它返回的命令时

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Command' object is not iterable

@bot.command(pass_context=True)
async def maps(ctx):
    await ctx.message.delete()
    channel = bot.get_channel(800086126173225010)
    await channel.send('**New poll! Vote below now for tomorrow\'s map!**')
    choice = random.sample(list(maps,4)

    for x in range(0, 4):

            mapemp.append(emoji[x]+" - "+choice[x])

    msg = await channel.send('\n\n'.join(mapemp))

    for x in range(0,4):
    
            await msg.add_reaction(emoji[x])
    mapemp.clear()

是什么让@bot.command 与@client.event 如此不同以至于我无法重复选择?

我以前没有random.sample(list(maps,4),但是当我尝试只使用random.sample(maps,4) 运行它时,我得到了一个不同的错误。

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Population must be a sequence or set. For dicts, use list(d).

所以我把它改成了random.sample(list(maps,4),如果这很重要的话。

【问题讨论】:

  • 把random.sample(list(maps,4)改成random.sample(list(maps,4))?
  • 改成choice = random.sample(list(maps),4)discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Command' object is not iterable`
  • 当我尝试 random.sample(list(maps,4)) 它返回 discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: list() takes at most 1 argument (2 given)

标签: python python-3.x discord discord.py discord.py-rewrite


【解决方案1】:

问题是你的函数名和列表名都是maps。因此,当您运行choice = random.sample(list(maps,4) 时,它认为您指的是函数maps,而不是列表。为了解决这个问题,你要么想要

  1. 更改函数的名称(也更改命令的名称)。您只需将 async def maps(ctx): 更改为 async def newCommandName(ctx): 即可完成此操作(将 newCommandName 更改为您想要的函数名称)。

  2. 更改maps 列表的名称。我不知道这个定义在哪里,但我假设它类似于maps = []。取而代之的是,您需要将名称更改为 mapsList 之类的名称,然后将所有对列表的引用更改为使用 mapsList。

另外,作为旁注,choice = random.sample(list(maps,4) 应该改为 choice = random.sample(list(maps),4)。

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 2021-04-14
    • 2021-11-11
    • 2021-03-16
    • 2021-11-14
    • 2021-03-26
    • 2023-02-10
    • 2021-08-28
    • 2021-01-22
    相关资源
    最近更新 更多