I have written a discord bot using discord.py to randomly select 3 lords from a list. Since embeds can only display one image, I've decided to use custom emojis instead. It works, but the problem is that the sometimes emoji is not added.
Correct output:
Incorrect output:
I am pretty sure it is connected to problems with the async nature of the discord.py library and my code, but I don't exactly know how to fix it.
How do I make sure that the emotes are added before I try to fetch them and add them to the embed?
Also, any suggestions for a solution that wouldn't use custom emotes and would achieve something similar to the correct output, are welcome.
Here's the relevant part of the code and what it does:
- Pick random lords from a list
- For each lord:
- create emote (add_emote method)
- get respective custom emote
- add custom emote to an embed as a field
- Send the embed to the channel
- Delete all custom emotes
@bot.command()
async def draft(ctx, counter):
embed = discord.Embed(
title='Lords'
)
if int(counter) > 3 or int(counter) < 1:
rand_lords = random.sample(lords, counter)
for rand_lord in rand_lords:
await add_emote(ctx, rand_lord)
emoji = get(ctx.message.guild.emojis, name=rand_lord['asset'])
embed.add_field(name=rand_lord["lord"], value=emoji, inline=False)
await ctx.send(embed=embed)
await remove_emote(ctx)
add_emote method:
async def add_emote(ctx, lord):
with open('assets/' + lord['asset'] + '.webp', 'rb') as fd:
await ctx.guild.create_custom_emoji(name=lord['asset'], image=fd.read())
lords.json:
[
{
"race": "Bretonnia",
"faction": "Couronne",
"lord": "Louen Leoncoeur",
"asset": "louen_leoncoeur"
},
{
"race": "Bretonnia",
"faction": "Bordeleaux Errant",
"lord": "Alberic de Bordeleaux",
"asset": "alberic_de_bordeleaux"
},
{
"race": "Bretonnia",
"faction": "Carcassonne",
"lord": "The Fay Enchantress",
"asset": "the_fay_enchantress"
}
]

