discord.py - 'command' is not iterable?

JabbaTheHut4874

I'm new to python and making discord bots so sorry if this is a noob question but I'm stuck.

Been trying to figure it out for hours.

I'm writing a simple bot that will loop through a list of 28 objects and randomly choose 4 of them. Then send those 4 choices into chat so people can vote for their choice.

Last night I was using

@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()

This works fine. But then I found out about @bot.command instead of @client.event so I'm trying to switch to that. However, when I try to run the command it returns

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()

What makes @bot.command so different than @client.event that I can't iterate through the choices?

I didnt use to have random.sample(list(maps,4) but when I try to run it with just random.sample(maps,4) I get a different error.

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

So I changed it to random.sample(list(maps,4) if that matters.

Axiumin_

The problem is that your function name and list name are both maps. So, when you run choice = random.sample(list(maps,4), it thinks you're referring to the function maps, not the list. In order to fix this, you'll either want to

  1. Change the name of the function (also changes the name of the command). You can do this by just changing async def maps(ctx): to async def newCommandName(ctx): (change newCommandName with the new name you want for the function).

  2. Change the name of the maps list. I don't know where this definition is, but I'll assume it's something like this maps = []. Instead of that, you'll want to change the name to something like mapsList and then change all references to the list to use mapsList instead.

Also, as a side note, choice = random.sample(list(maps,4) should be choice = random.sample(list(maps),4) instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive