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

Viewed 144009

I've been working a new Discord bot.

I've learnt a few stuff,and, now, I'd like to make the things a little more custom.

I've been trying to make the bot send embeds, instead, of a common message.

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 this code, and I have no idea of any other way to send a embed.

5 Answers
@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)

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)

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))
Related