0
votes

I have been getting into coding my very own discord bot using Discord.py in Thonny, using python version 3.7.6. I want to have an embed when a certain command is typed (!submit) to have the users name as the title and the content of the message as the description. I am fine having !submit ' ' in my embed but if there is any way of taking that out and only having the content of the message minus the !submit i would highly appreciate it. Right now with my code i am getting two errors, one is that the client.user.name is the name of the bot (submit bot) and not the author (old code), and i am getting this message 'Command raised an exception: TypeError: Object of type Member is not JSON serializable' with my new code(below), if anyone could offer insight please reply with the appropriate fixes!

client = commands.Bot(command_prefix = '!')
channel = client.get_channel(707110628254285835)

@client.event
async def on_ready():
 print ("bot online")


@client.command()
async def submit(message):
 embed = discord.Embed(
    title = message.message.author,
    description = message.message.content,
    colour = discord.Colour.dark_purple()
 )
 channel = client.get_channel(707110628254285835)
 await channel.send(embed=embed)





client.run('TOKEN')
2

2 Answers

0
votes
client = commands.Bot(command_prefix = '!')
#got rid of the old channel variable here
@client.event
async def on_ready():
 print ("bot online")

@client.command()
async def submit(ctx, *, message): #the `*` makes it so that the message can include spaces
 embed = discord.Embed(        
    title = ctx.author, #author is an instance of the context class
    description = message, #No need to get the content of a message object
    colour = discord.Colour.dark_purple()
 )
 channel = client.get_channel(707110628254285835)
 await channel.send(embed=embed)

client.run('TOKEN')

Problems: 1. channel is defined twice, 2. function commands in discord.py take an implicit context argument, usually called ctx. Overall, it looks like you aren't understand the underlying OOP concepts that discord.py has to offer. It might be helpful to refresh your memory with an online class or article.

0
votes

You're pretty close...

The following items should help:

  1. Working with commands you generally define the context of the command as "ctx", which is the first and sometime only argument. "message" is generally used on message events like async def on_message(message):.
  2. To break out the message from the command itself you add arguments to the function definition.
  3. To get the name of the user you need to cast to a string to avoid the TypeError.
  4. To send the message back in the same channel as the !submit was entered, you can use await ctx.send(embed=embed)

Try:

@client.command()
async def submit(ctx, *, extra=None):
    embed = discord.Embed(
        title=str(ctx.author.display_name),
        description=extra,
        colour=discord.Colour.dark_purple()
    )
    await ctx.send(embed=embed)

Result:

enter image description here