Search code examples
pythonfor-loopdiscord.pyembed

Discord.py | When working with embeds, how does one use a for loop to generate a variable's worth of fields?


I'm currently working on a discord.py dice roller command that allows one to input an argument for a number of dice to roll. As this command outputs to an embed, I would like each dice roll to output to it's own embed field using a for loop that executes the same number of times as the variable that contains the argument's amount of dice.

I've attempted quite a few types of for loops, but I think I'm doing something fundamentally wrong. Unfortunately, Discord.py is not returning any errors when I run this for loop so I can't even begin to wrap my head around why...

diceNum = 5
diceRange = range(diceNum)

for dice in diceRange:
    print(f'test: {dice}')
    embed.add_field(name=f"{dice}",
                    value=f"{diceName} is a(n)...\n {diceResult}**!",
                    inline=True)

My assumption is that one cannot add an embed field without doing some special method of sorts, and as stated previously when I run this code it simply outputs nothing. No error, and cuts the command short too. Any suggestions would be greatly appreciated, SO!

Edit: Here's the embed assignment!

embed = discord.Embed(
    title="Test Dice Roller",
    description=f"You've rolled {diceNum} {diceName} dice, Good luck!\n\n",
    color=diceColor,
)

Solution

  • Your method is correct, check the color

    here is my little test with a change in the for loop statement

    @bot.command()
    async def dice(ctx):
    
        diceNum = 5
        embed = discord.Embed(
            title="Test Dice Roller",
            description=f"You've rolled {diceNum}",
        )
    
        for dice in range(diceNum):
            embed.add_field(name=dice, value='something')
    
        await ctx.send(embed=embed)
    

    enter image description here