【问题标题】:How to add a reaction to a message using the message ID如何使用消息 ID 向消息添加反应
【发布时间】:2020-02-27 21:18:12
【问题描述】:

我想做一个 Bot 命令,当给定表情符号和消息 ID 时,它会在消息中添加一个反应。

看来我需要将从原始消息中获得的字符串转换为 discord.Message 类才能使代码正常工作,但我没有找到任何方法。

我已经通读了文档,它只是说 “应该没有必要手动创建其中之一。”

import discord
token = "Token Here"
client = discord.Client()
@client.event
async def on_message(message):
    if message.content.lower().startswith("e react"):
        msg_id = message.content[8:26] #This filters out the ID
        emoji = message.content[27:] #This filters out the Emoji
        await msg_id.add_reaction(emoji)

client.run(token)

我收到以下错误:
AttributeError: 'str' object has no attribute 'add_reaction'

【问题讨论】:

    标签: python discord message discord.py


    【解决方案1】:

    使用converters 使这变得更加容易。具体来说,我们可以使用MessageConverter 来获取目标Message 对象和PartialEmojiConverter 来获取我们想要反应的表情符号:

    from discord.ext.commands import Bot
    from discord import Message, PartialEmoji
    
    bot = Bot("e ")
    
    @bot.command()
    async def react(ctx, message: Message, emoji: PartialEmoji):
        await message.add_reaction(emoji)
    
    bot.run("TOKEN")
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 2021-10-10
      • 2021-05-21
      • 2019-09-06
      • 2020-10-27
      • 1970-01-01
      • 2020-11-07
      • 2022-08-02
      • 2021-01-03
      相关资源
      最近更新 更多