Ethan
Ethan

Reputation: 11

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

Upvotes: 0

Views: 5088

Answers (3)

Anony Mous
Anony Mous

Reputation: 354

Try this (based of Bhavyadeep's code):

import discord
from discord.ext import commands
import random

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

@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.event
async def on_message(ctx):
    if ctx.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)
        await ctx.channel.send(embed=embedVar)

bot.run("TOKEN")

I've simply replaced "message" with "ctx". If this still doesn't work, then try replacing bot = commands.Bot(command_prefix=PREFIX, description="Hi") with bot = commands.Bot(command_prefix=PREFIX, description="Hi", pass_context=True). It also appears your indentation in the class MyClient is wrong, so fix that first. Make sure that async def on_message(... is in the class scope.

If all fails, try and move the code to the global scope (delete the class).

Upvotes: 0

Bhavyadeep Yadav
Bhavyadeep Yadav

Reputation: 831

As you use CLASSES AND OBJECTS or OOP for the bot you need correct syntax too. I can't really help you with than but can use normal way.

Step 1:

We will import the libraries:

import discord
from discord.ext import commands
import random

Step 2:

We will define the Prefix and Bot's variable.

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

Step 3:

We will now write the bot's on_ready() command according to your code.

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

The statement bot.get_channel() is being used to get the channel to send the message.

Step 4:

We will now write bot's on_message function according to your code.

@bot.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)
        await message.channel.send(embed=embedVar)

Step 5:

We will now create a statement to start up your bot.

bot.run("TOKEN")

Add this command at the bottom of the script.


The whole code compiled will be:

import discord
from discord.ext import commands
import random

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

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

@bot.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)
        await message.channel.send(embed=embedVar)

This would solve your problem. If you still get any problem, please ask me in the comments. :)

Thank You! :D

Upvotes: 2

brni-dev
brni-dev

Reputation: 34

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)

Upvotes: -1

Related Questions