Home > Software engineering >  discord.py How to get a custom emoji's url (NOT as a command)
discord.py How to get a custom emoji's url (NOT as a command)

Time:01-08

I found an answer online which looks like this:

@bot.command()
async def geturl(emoji: discord.Emoji):
    await bot.say(emoji.url)

But my bot needs to know the url of an emoji right after it's put in a database, so this won't do. I tried doing something like:

emoji = discord.PartialEmoji(name="<:duckysleep:1059974724588945479>")

print(emoji.url)

But this just... won't work for some reason. The console is blank.

How can I get an emoji's url from its name?

CodePudding user response:

The reason this doesn't work:

emoji = discord.PartialEmoji(name="<:duckysleep:1059974724588945479>")

is because "<:duckysleep:1059974724588945479>" is not the name of your emoji, that is the entire format string to represent it in Discord, made up of <animated?:name:id>. The name is duckysleep.

To construct a PartialEmoji, pass the name, id & animated arguments separately. Note: the name is not used when building the URL, so if you only have the id then that's fine as well.

Alternatively, if you already have the entire string, you can use PartialEmoji.from_str() to create one from the format string, as the name implies. That way you don't have to parse it yourself, and optionally make mistakes against it.

emoji = PartialEmoji.from_str("<:duckysleep:1059974724588945479>")

Then you can just access the .url property to get the URL.

  • Related