How can I send an embed via my Discord bot, w/python?
Asked Answered
K

7

24

I've been working on a Discord bot, and I'd like to make things more custom.

I've been trying to make the bot send embeds, instead of normal messages.

embed = discord.Embed(title="Tile", description="Desc", color=0x00ff00)
embed.add_field(name="Fiel1", value="hi", inline=False)
embed.add_field(name="Field2", value="hi2", inline=False)
await self.bot.say(embed=embed)

When executing this code, I get the error that Embed is not a valid member of the module discord. All websites show me similar code, and I do not know any other way to send an embed.

Kashmiri answered 1/7, 2017 at 14:36 Comment(1)
The discord.py error handling ignores errors like these, since in production a single error could cuase your whole bot to break. Hence, i recommend putting a try except block inside the function everytime you are testing, it will help alot in terms of understanding the errorFunk
I
41

To get it to work I changed your send_message line to await message.channel.send(embed=embed)

Here is a full example bit of code to show how it all fits:

@client.event
async def on_message(message):
    if message.content.startswith('!hello'):
        embedVar = discord.Embed(title="Title", description="Desc", color=0x00ff00)
        embedVar.add_field(name="Field1", value="hi", inline=False)
        embedVar.add_field(name="Field2", value="hi2", inline=False)
        await message.channel.send(embed=embedVar)

I used the discord.py docs to help find this. https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.send for the layout of the send method.

https://discordpy.readthedocs.io/en/latest/api.html#embed for the Embed class.

Before version 1.0: If you're using a version before 1.0, use the method await client.send_message(message.channel, embed=embed) instead.

Irizarry answered 1/7, 2017 at 16:41 Comment(6)
Somehow now, it gives me a new error. "expected an intend block", and the arrow shows the error in the 'd' of the "embed = "Kashmiri
Make sure you haven't by accident indented something. Check the indentation of the if statement and that line.Irizarry
I'm not sure then. Sorry. Try asking in this discord server discord.gg/discord-api or you could try reporting it as a issue on the github. Also someone else seems to of had this issue so maybe monitor that post as well #44475455.Irizarry
You should pick a different name for variable "embed", not sure if it can cause errors, bat anyways, embed=embed is not that clear..Dialyze
@WiseMan I've changed it to embedVar. Do you think that name is suitably clear or have any other suggestions?Irizarry
change self.bot.say to ctx.sendMenswear
N
7

When executing this code, I get the error that 'Embed' is not a valid member of the module 'discord'. All websites, show me this code, and I have no idea of any other way to send a embed.

This means you're out of date. Use pip to update your version of the library.

pip install --upgrade discord.py
Nadean answered 8/7, 2017 at 4:23 Comment(4)
It says that there's a syntax error in the install bit.Kashmiri
Where does it say you're getting a syntax error? You run this command in the command lineNadean
I had done this in the bot's script, lmao. Anyways, I tried run this in the cmds thing, and it didn't work. Let me send print. prntscr.com/fvjbff What am I doing wrong?Kashmiri
@Norby I'm a little late, but you haven't added pip to pathAlmsgiver
L
2
@bot.command()
async def displayembed(ctx):
    embed = discord.Embed(title="Your title here", description="Your desc here") #,color=Hex code
    embed.add_field(name="Name", value="you can make as much as fields you like to")
    embed.set_footer(name="footer") #if you like to
    await ctx.send(embed=embed)
Lilywhite answered 25/3, 2020 at 11:39 Comment(1)
maybe you should point out what was wrong with the code posted in the questionStroh
F
2

how about put @client.event instead of the @bot.command() it fixed everything when I put @client.event... @bot.command() does not work you can type

@client.event
async def displayembed(ctx):
    embed = discord.Embed(title="Your title here", description="Your desc here") #,color=Hex code
    embed.add_field(name="Name", value="you can make as much as fields you like to")
    embed.set_footer(name="footer") #if you like to
    await ctx.send(embed=embed)
Furman answered 19/1, 2021 at 21:16 Comment(0)
S
1

For anyone coming across this in 2022:

how about put @client.event instead of the @bot.command() it fixed everything when I put @client.event... @bot.command() does not work you can type

To this ^, I don't recommend using @client.event / @bot.event as you'd want to register your commands as a command.

If you want to simply make a command with an embed in your main.py file, make sure you have something like:

import discord
from discord.ext import commands

intents = discord.Itents.default()

bot = commands.Bot(command_prefix='YOURPREFIX', description='description', intents=intents)

@bot.command(name="embed")
async def embed(ctx):
    embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
    embed.add_field(name="Name", value="Value", inline=False)
    await ctx.send(embed=embed)

However, I personally like separating my commands into a /commands folder and with separate files for all of them as it's good practice for neater code.

For that, I use cogs.

/commands/embed.py

from discord.ext import commands
import discord

class EmbedCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    
    @commands.command(name="embed")
    async def embed(self, ctx):
        embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
        embed.add_field(name="Name", value="Value", inline=False)
        await ctx.send(embed=embed)

Then import it all into your main.py file:

from commands.embed import EmbedCog

bot.add_cog(EmbedCog(bot))
Sethrida answered 4/9, 2022 at 14:33 Comment(0)
S
0

Before using embed

embed (Embed) – The rich embed for the content to send. This cannot be mixed with embeds parameter.

embeds (List[Embed]) – A list of embeds to send with the content. Maximum of 10. This cannot be mixed with the embed

TypeError – You specified both embed and embeds or file and files or thread and thread_name.

@client.event
async def on_message(message):
    if message.content.startswith('!hi'):
        embed = discord.Embed(title="This is title of embedded element", description="Desc", color=0x00ff00)
        embed.add_field(name="Like header in HTML", value="Text of field  1", inline=False)
        embed.add_field(name="Like header in html", value="text of field 2", inline=False)
        await message.channel.send(embed=embed)

Reference

Standby answered 23/11, 2022 at 18:49 Comment(0)
D
0

Your code is incorrect on few points listed below. I am assuming that you have corrected defined bot in the Cog (OOP) Still I am sharing the whole corrected code

Corrected Code

import discord
from discord.ext import commands

class YourCog(commands.Cog): # replace with the real name
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='send_embed')
    async def send_embed(self, ctx):
        embed = discord.Embed(title="Title", description="Desc", color=0x00ff00)
        embed.add_field(name="Field1", value="hi", inline=False)
        embed.add_field(name="Field2", value="hi2", inline=False)
        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(YourCog(bot))

Changes EXPLAINED:

  • Added Cog Structure:

    Created a class YourCog that inherits from commands.Cog. Initialized the class with init method, taking bot as a parameter.

  • Added a command send_embed within the cog.

  • Updated await self.bot.say(embed=embed) to await ctx.send(embed=embed) ( the main problem was here )

Why your code didn't work

  • The original code seemed to be using an outdated version of the discord.py library. The method say is no longer used for sending messages with embeds. It has been replaced by the send method in the newer versions.
Discussion answered 29/2, 2024 at 13:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.