6

I'm trying to add a custom emoji as a reaction to a message using discord.py version 0.16.12 and I'm unable to get it functional. Here's the code I'm using:

@bot.event
async def on_message(message):
    if message.content.find(':EmojiName:'):
        await bot.add_reaction(message, '<:EmojiName:#EmojiID#>')

I've also tried passing the emoji id as a string similar to discord.js (message, '#EmojiID#'). Am I supposed to pass the add_reaction function an emoji object? And if that's the case, how do I find the specific emoji object from the get_all_emojis function?

hopethatsacleanwet
  • 418
  • 1
  • 4
  • 13

2 Answers2

9

You can use the utility function discord.utils.get to get the appropriate Emoji object.

from discord.utils import get

@bot.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == bot.user:
        return
    if ':EmojiName:' in message.content:
        emoji = get(bot.get_all_emojis(), name='EmojiName')
        await bot.add_reaction(message, emoji)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • I was unaware of that function, thank you. I should have seen it in the documentation but I guess I got tunnel vision. https://discordpy.readthedocs.io/en/latest/api.html#discord.utils.get – hopethatsacleanwet Feb 26 '18 at 06:15
  • @patrick how to add custom emoji reactions for bot commands reply. for this question https://stackoverflow.com/questions/52685273/python-add-custom-reaction-for-message – Demotry Oct 07 '18 at 19:28
  • @Demotry You should ask a separate question. I'm not sure how what you're asking differs from this question. – Patrick Haugh Oct 07 '18 at 19:31
  • yes i asked a new question i posted question link in above comment. what i need is if we use `!ping` command bot posts a message or embed with `Pong` and that embed should come with reactions added. – Demotry Oct 07 '18 at 19:39
  • @Demotry Did you try the technique from this answer, using `get` to get the emoji by name from `Client.emojis` or `guild.emojis`? – Patrick Haugh Oct 07 '18 at 19:49
  • yes tried but i using custom emojis or animated emojis like this `<:emoji_name1:emoji_id1>` and i need to add 3 to 5 emojis to that bot posts. So mostly like a poll bot posts message for commands with pre added reactios. – Demotry Oct 07 '18 at 19:52
  • @PatrickHaugh I tried same like yours with with some changes `if message.content.startswith("!ping"):` but it adds reaction for our message `!ping` not for bots message. – Demotry Oct 07 '18 at 20:06
  • AttributeError: 'Client' object has no attribute 'get_all_emojis' - this is what it says to me – zabop Jun 23 '20 at 21:19
  • @zabop There was an API change since this answer was written. The corresponding attribute is now `bot.emojis` (not a function). – Patrick Haugh Jun 23 '20 at 21:43
  • Made it a different q in the meantime: https://stackoverflow.com/questions/62544309/how-to-make-client-get-all-emojis-work-using-discords-python-api – zabop Jun 23 '20 at 21:51
  • Couldn't make it work, explaned it in the new question :) – zabop Jun 23 '20 at 21:56
  • It says 'Bot' object does not have attribute 'get_all_emojis' – Yozy Opto Jun 28 '23 at 15:54
  • @YozyOpto I think the modern version is `emojis`. You should take a look at the documentation though, there have been a few breaking redesigns in the last few years so many old SO answers will be using the old interfaces. – Patrick Haugh Jun 28 '23 at 16:55
  • Now it says 'SequenceProxy' object is not callable – Yozy Opto Jun 28 '23 at 20:08
2

Nevermind, folks, I figured it out. You do have to pass it the emoji object itself. For posterity's sake, here's the code I ended up using:

async def on_message(message):
if message.content.find(':EmojiName:'):
    for x in client.get_all_emojis():
        if x.id == '#EmojiID#':
            return await client.add_reaction(message, x)
hopethatsacleanwet
  • 418
  • 1
  • 4
  • 13
  • 3
    `client.get_all_emojis()` seems deprecated. Asked an updated question here, maybe can help? https://stackoverflow.com/questions/62544309/how-to-make-client-get-all-emojis-work-using-discords-python-api – zabop Jun 23 '20 at 21:59