Skip to content Skip to sidebar Skip to footer

Sending Embeds Through Discord.py

I've been doing a lot of work creating discord bots with the discord api through javascript with discord.js. I am trying to create my first discord bot with python using the discor

Solution 1:

  1. In the line you set the embed description, outputting r.status_code as a string instead of the value it contains. Try embed.description = '**Status Code:** {0}'.format(r.status_code)

  2. The 0 resembles the index of the value that is supposed to be there. For example, '{1}'.format(10, 20) would print out the value at index 1 which in this case is 20.

  3. When you use send(embed) then the bot will end up sending the embed in string form which will look very off, if you try sending it you will see what I mean. In this case, we have to specify which argument we are assigning the value to. This function accepts kwargs which are Keyworded arguments, in this case, embed is one of the kwargs in this send() function. This function accepts other kwargs as well such as content, tts, delete_after, etc. It is all documented.

  4. You can simplify creating the embed by passing in the kwargs, for example: discord.Embed(title='whatever title', color='whatever color') The discord.Embed() can support more arguments if you look at the documentation.

Here is a link to the documentation: https://discordpy.readthedocs.io/en/latest/index.html If you search for TextChannel, and look for the send() function, you can find more arguments supported, as well as for discord.Embed().

Solution 2:

Okay working down your list of questions:

  1. You're not formatting {r.status_code} but just sending it as a string which is why it shows up as just that. To fix it, all you have to do is add 1 "f".
embed.description = f'**Status Code:** {r.status_code}'

or, if you want to use a consistent way of formatting your strings:

embed.description = '**Status Code:** {0}'.format(r.status_code)
  1. In Python, the curly brackets '{}' in a string can be used for formatting. This can be done in multiple ways including how you're doing it in your code. str.format() can take multiple arguments if you want to format different values in your string. The 0 is just an index for which argument you want to use.
  2. I'm not too familiar with the discord library but having taken a quick look at the documentation it would seem that if you did that it would take your embed variable and pass it into send as the content variable. Thereby sending it as a normal message instead of embedding it.
  3. I personally like f-strings more for formatting because it makes your code more easily readable. I can't really comment on your use of the discord library but besides that your code looks fine! Purely for aesthetics/readability I would put an empty line between your imports and defining your variables, and between your variables and your class.

Solution 3:

Here is an example!

import discord

client = commands.Bot(command_prefix = '~')

@client.command()asyncdefembed(ctx):
   embed=discord.Embed(title="Hello!",description="Im a embed text!")
await ctx.send(embed=embed)

client.run("TOKEN")

Post a Comment for "Sending Embeds Through Discord.py"