0

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.

3
  • Change random.sample(list(maps,4) to random.sample(list(maps,4))? Commented Jan 17, 2021 at 20:07
  • Changed it to choice = random.sample(list(maps),4) discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Command' object is not iterable ` Commented Jan 17, 2021 at 20:16
  • When I try random.sample(list(maps,4)) it returns discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: list() takes at most 1 argument (2 given) Commented Jan 17, 2021 at 20:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.