Norberto A.
Norberto A.

Reputation: 388

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

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.

Upvotes: 24

Views: 179096

Answers (7)

Pranjal Prakarsh
Pranjal Prakarsh

Reputation: 91

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.

Upvotes: 0

Md Shayon
Md Shayon

Reputation: 375

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

Upvotes: 0

clxrity
clxrity

Reputation: 346

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

Upvotes: 1

XxCascoplaysXx
XxCascoplaysXx

Reputation: 31

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)

Upvotes: 2

Tim
Tim

Reputation: 1173

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.

Upvotes: 41

Mad
Mad

Reputation: 29

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

Upvotes: 2

khazhyk
khazhyk

Reputation: 1888

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

Upvotes: 7

Related Questions