Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you have a bot add a reaction using a custom emoji?

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?

like image 282
hopethatsacleanwet Avatar asked Feb 26 '18 05:02

hopethatsacleanwet


2 Answers

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)
like image 66
Patrick Haugh Avatar answered Nov 03 '22 11:11

Patrick Haugh


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)
like image 37
hopethatsacleanwet Avatar answered Nov 03 '22 10:11

hopethatsacleanwet