discord message embed

How To Make a Message Embed With Discord Bot Using Python

In this post, we’ll take a look at how we can stylize a message embed with a Discord bot. To clarify, message embeds are structured messages, which can include images, videos, links, title, and more. However, do keep in mind that we can’t change the positions of elements inside the embed.

In the following example, we’ll make a simple Discord bot that will include a command, which will send an embed with hardcoded text and images. Additionally, its sole purpose is to demonstrate, how to add different elements to the embed.

Coding the function for sending Discord embed message

In the following function, we’ll define several things, which we’ll include inside the embed. Firstly, all embeds have a border color on the left side. If we don’t define this value, it will inherit the default value from Discord.

In our case, we’ll use a nice red color, but you don’t need to limit yourself to predefined colors by any means. Actually, you can declare a custom color with the function from_rgb().

Along with this color, we define the title, description, author and a field with a link. All these values will be present on the upper side of the embed. Besides all the text, we also add an image and a thumbnail from URL addresses.

@commands.hybrid_command(name='embed')
async def embed(self, ctx):
    embed = discord.Embed(
        color=discord.Color.red(),
        title='This is the title',
        description='This is a descriptions *first line*\nAnd this is the **second line**'
    )

    embed.set_footer(text='This is some text in the footer')
    embed.set_author(name='Andraž', url='https://ak-codes.com/')
    embed.set_thumbnail(url='https://ak-codes.com/wp-content/uploads/2023/05/cropped-logo.png')
    embed.set_image(url='https://ak-codes.com/wp-content/uploads/2023/06/20191103_113510-scaled.webp')
    embed.add_field(name='Blog', value='https://ak-codes.com/')

    await ctx.send(embed=embed)

The following image shows, what Discord bot posts with this function.

Discord message embed

Entire code for the Discord bot

I’ll also post the rest of the code of this Discord bot here, so you can see how to put it to use. If you’re interested in more Discord bot development, I’d recommend you to check out my other Discord bot tutorials.

I’d also like to mention that there are many ways of fetching the access token. I chose it’s simple enough if I just store it inside a json file in the same directory. This way, you needn’t reveal it inside the code in case you’ll share this code with anyone.

import os
import json
import asyncio
import discord
from discord.ext import commands

ROOT = os.path.dirname(__file__)

def get_token(token_name):
    try:

        with open(os.path.join(ROOT, 'auth.json'), 'r') as auth_file:
            auth_data = json.load(auth_file)
            token = auth_data[token_name]
            return token
    except:
        return
    

class DemoCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    
    @commands.hybrid_command(name='embed')
    async def embed(self, ctx):
        embed = discord.Embed(
            color=discord.Color.red(),
            title='This is the title',
            description='This is a descriptions *first line*\nAnd this is the **second line**'
        )

        embed.set_footer(text='This is some text in the footer')
        embed.set_author(name='Andraž', url='https://ak-codes.com/')
        embed.set_thumbnail(url='https://ak-codes.com/wp-content/uploads/2023/05/cropped-logo.png')
        embed.set_image(url='https://ak-codes.com/wp-content/uploads/2023/06/20191103_113510-scaled.webp')
        embed.add_field(name='Blog', value='https://ak-codes.com/')

        await ctx.send(embed=embed)

    
intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(
    command_prefix=commands.when_mentioned_or('!'),
    description='Embed demonstration bot',
    intents=intents
)

@bot.event
async def on_ready():

    print(f'Logged in as {bot.user} (ID: {bot.user.id})')
    print('------')
    await bot.tree.sync()

async def main():
    async with bot:
        await bot.add_cog(DemoCog(bot))
        await bot.start(get_token('discord-token'))

asyncio.run(main())

Conclusion

To conclude, we made a simple Discord bot to demonstrate how to put together a message embed with various different elements. I learned a lot while working on this example and I hope it proves to be helpful to you as well.

Share this article:

Related posts

Discussion(0)