18
votes

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.

4
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 errorJoel

4 Answers

33
votes

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.

6
votes

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
1
votes
@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)
0
votes

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)