Search code examples
pythondiscorddiscord.py

How do I make a discord.py embed?


The code essentially is supposed to create an embed and for some reason it gives me the error " 'message' is not defined" (the embed will be put into anoter bot)

import discord
from discord.ext import commands
import random
from discord.ext.commands import Bot



class MyClient(discord.Client):

    async def on_ready(self):
        print('Logged on as', self.user)
        channel = self.get_channel(717005397655027805)
        await channel.send("I am now online")

        messageContent = message.Content
        if message.author == self.user:
            return

        @client.event
        async def on_message(message):
            if message.content.startswith('!hello'):
                embedVar = discord.Embed(
                    title="Title", description="Desc", color=0x336EFF
                )
                embedVar.add_field(name="Field1", value="hi", inline=False)
                embedVar.add_field(name="Field2", value="hi2", inline=False)
                client.send_message(message.channel, embed=embedVar)

                PREFIX = ("$")
                bot = commands.Bot(command_prefix=PREFIX, description='Hi')



client = MyClient()
client.run('TOKEN')

Solution

  • You didn't specify what message.Content is in on the on_ready() function, also you need to create the on_message() function outside of the on_ready() func, and you didn't specify what the @client decorator is, you just specified it at the end of your code.

    All in all, your fixed code should look something like this

    from discord.ext import commands
    
    TOKEN  = "Your token here"
    PREFIX = '$'
    bot    = commands.Bot(command_prefix=PREFIX)
    
    @bot.event
    async def on_ready():
        print('Logged on as', bot.user)
        channel = bot.get_channel(717005397655027805)
        await channel.send("I am now online")
    
    @bot.command() # Usage - $hello
    async def hello(ctx):
        if ctx.message.author == bot.user: return
        embedVar = discord.Embed(title="Title", description="Desc", color=0x336EFF)
        embedVar.add_field(name="Field1", value="hi", inline=False)
        embedVar.add_field(name="Field2", value="hi2", inline=False)
        await ctx.send(embed=embedVar)
    
    bot.run(TOKEN)