Sending Embeds Through Discord.py
Solution 1:
In the line you set the embed description, outputting
r.status_code
as a string instead of the value it contains. Tryembed.description = '**Status Code:** {0}'.format(r.status_code)
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.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 acceptskwargs
which are Keyworded arguments, in this case, embed is one of thekwargs
in thissend()
function. This function accepts otherkwargs
as well such ascontent, tts, delete_after, etc.
It is all documented.You can simplify creating the embed by passing in the
kwargs
, for example:discord.Embed(title='whatever title', color='whatever color')
Thediscord.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:
- 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)
- 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.
- 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 thecontent
variable. Thereby sending it as a normal message instead of embedding it. - 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"