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

134,221

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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)
Share:
134,221

Related videos on Youtube

Norberto A.
Author by

Norberto A.

Hello! I'm Norberto! I'm a 19 year old self taught programmer from Portugal! I currently code LUA, JavaScript. I'm learning Python (again) & Java. I'm also working in web development projects.

Updated on July 09, 2022

Comments

  • Norberto A.
    Norberto A. almost 2 years

    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.

    • Joel
      Joel almost 3 years
      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 error
  • Norberto A.
    Norberto A. almost 7 years
    Somehow now, it gives me a new error. "expected an intend block", and the arrow shows the error in the 'd' of the "embed = "
  • Tim
    Tim almost 7 years
    Make sure you haven't by accident indented something. Check the indentation of the if statement and that line.
  • Norberto A.
    Norberto A. almost 7 years
    I did, and everything's right. It keeps saying that somehow.
  • Tim
    Tim almost 7 years
    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 stackoverflow.com/questions/44474955/….
  • Norberto A.
    Norberto A. almost 7 years
    It says that there's a syntax error in the install bit.
  • khazhyk
    khazhyk almost 7 years
    Where does it say you're getting a syntax error? You run this command in the command line
  • Norberto A.
    Norberto A. almost 7 years
    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?
  • Person
    Person over 5 years
    @Norby I'm a little late, but you haven't added pip to path
  • knee-cola
    knee-cola about 4 years
    maybe you should point out what was wrong with the code posted in the question
  • Wise Man
    Wise Man almost 4 years
    You should pick a different name for variable "embed", not sure if it can cause errors, bat anyways, embed=embed is not that clear..
  • Tim
    Tim almost 4 years
    @WiseMan I've changed it to embedVar. Do you think that name is suitably clear or have any other suggestions?
  • Westlenando
    Westlenando over 3 years
    change self.bot.say to ctx.send