【问题标题】:How do you have a bot add a reaction using a custom emoji?您如何让机器人使用自定义表情符号添加反应?
【发布时间】:2018-08-05 12:41:59
【问题描述】:

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

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

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

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    您可以使用实用函数discord.utils.get 来获取相应的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)
    

    【讨论】:

    • 我不知道这个功能,谢谢。我应该在文档中看到它,但我想我有隧道视野。 discordpy.readthedocs.io/en/latest/api.html#discord.utils.get
    • @patrick 如何为机器人命令回复添加自定义表情符号反应。对于这个问题stackoverflow.com/questions/52685273/…
    • @Demotry 你应该问一个单独的问题。我不确定你问的内容与这个问题有何不同。
    • 是的,我问了一个新问题,我在上面的评论中发布了问题链接。我需要的是,如果我们使用!ping 命令机器人发布消息或嵌入Pong,并且该嵌入应该带有添加的反应。
    • @Demotry 您是否尝试过此答案中的技术,使用getClient.emojisguild.emojis 按名称获取表情符号?
    【解决方案2】:

    没关系,伙计们,我想通了。您必须将表情符号对象本身传递给它。为了后代,这是我最终使用的代码:

    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)
    

    【讨论】:

    猜你喜欢
    • 2019-01-29
    • 2021-03-25
    • 2020-06-29
    • 2020-09-23
    • 2020-08-06
    • 2021-03-21
    • 2022-08-18
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多