user13878676
user13878676

Reputation:

How to copy an embed in discord.py?

I want the bot fetch a message(embed) and send it to channel where command is invoked. Following code works fine for normal text message:

@bot.command()
async def fetch(ctx):
    channel = bot.get_channel(736984092368830468)
    msg = await channel.fetch_message(752779298439430164)
    await ctx.send(msg.content)

For sending embeds I tried:

@bot.command()
async def fetch(ctx):
    channel = bot.get_channel(736984092368830468)
    msg = await channel.fetch_message(752779298439430164)
    await ctx.send(msg.embeds.copy())

It sends this instead of sending embed:

enter image description here

How do I make the bot copy and send embed?

Upvotes: 1

Views: 5839

Answers (2)

Nicholas Obert
Nicholas Obert

Reputation: 1608

The problem is that you are trying to send a list of discord.Embed objects as a string in your await ctx.send(msg.embeds.copy())
The ctx.send() method has a parameter called embed to send embeds in a message.

await ctx.send(embed=msg.embeds[0])

Should do the trick. This way you are sending an actual discord.Embed object, instead of a list of discord.Embeds

You should not need the .copy() method in

await ctx.send(embed=msg.embeds[0].copy())

although you can use it

The only downside of using the index operator [0] is that you can only access one embed contained in your message. The discord API does not provide a way to send multiple embeds in a single message. (see this question).

A workaround could be iterating over every embed in the msg.embeds list and send a message for every embed.

for embed in msg.embeds:
    await ctx.send(embed=embed)

This unfortunately results in multiple messages being sent by the bot, but you don't really notice.

Upvotes: 2

Abdulaziz
Abdulaziz

Reputation: 3426

You have to select the first embed like this and use [copy] if you want to change into it before sending again.(https://discordpy.readthedocs.io/en/latest/api.html?highlight=embed#discord.Embed.copy)

@bot.command()
async def fetch(ctx):
    channel = bot.get_channel(736984092368830468)
    msg = await channel.fetch_message(752779298439430164)
    await ctx.send(embed=msg.embeds[0])

Upvotes: 1

Related Questions