您如何让机器人使用自定义表情符号添加反应?

hop*_*wet 5 python python-3.x discord.py

我正在尝试使用 discord.py 0.16.12 版添加自定义表情符号作为对消息的反应,但无法使其正常工作。这是我正在使用的代码:

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

我还尝试将表情符号 id 作为类似于 discord.js 的字符串传递(message, '#EmojiID#')。我应该将add_reaction函数传递给emoji 对象吗?如果是这种情况,我如何从get_all_emojis函数中找到特定的表情符号对象?

Pat*_*ugh 8

您可以使用实用程序函数discord.utils.get来获取适当的Emoji对象

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)
Run Code Online (Sandbox Code Playgroud)