首页 > 解决方案 > 一旦对消息做出反应,如何让机器人向用户发送消息?

问题描述

我试图弄清楚on_reaction_add(message)当用户对另一个玩家的消息做出反应时让机器人发送消息的功能中缺少什么。

@client.listen()
async def on_message(message):
    """
    Looks for when a member calls 'bhwagon', and after 24 minutes, sends them a message
    :param message: the message sent by the user
    :return: a DM to the user letting them know their cooldown ended
    """
    if message.content.startswith("bhwagon") or message.content.startswith("missed wagon"):
        await cool_down_ended(message)
        await on_reaction_add(message)


@client.event
async def on_reaction_add(message):
    """
    Checks to see if there is a reaction on a message, and if there is, then send that user a message
    :param message: represents the context in which a command is being invoked under
    :return:
    """
    if message.emoji == '882814250710626405':  # If a certain reaction is used on a message
        await message.author.send("message")

标签: pythondiscord.py

解决方案


我在这里看到一些对我没有意义的错误/事情。

  1. on_reaction_add没有message参数,但取决于reactionand user

  2. await message.author.send()总是会向消息的作者发送消息,而不是给做出反应的人,这是行不通的。

  3. 您给定的 ID 似乎更像是消息 ID,而不是真正的 emoji-ID。

全局表情通常是这样构建的: <:NAME:ID- 这也是必须在代码中指定的方式。

Discord 中的表情符号可以轻松复制。

重要提示:所有表情符号必须在前面加上\要复制的名称或表情符号。

根据我现在的信息,一个新的可能代码:

@client.event
async def on_reaction_add(reaction, user): # reaction & user as an argument
    if reaction.emoji == '✅': # See if the reaction is the same as in the code
        await user.send("message") # Send the user who reacted a message

!!请注意,您还为此启用了意图。对此做出了很好的贡献:StackOverflow 上的文档和答案


推荐阅读