Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 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 Reply

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...