Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...