【问题标题】:To make the bot react to the last the last message or react to a message with message ID使机器人对最后一条消息作出反应或对带有消息 ID 的消息作出反应
【发布时间】:2019-07-13 12:38:38
【问题描述】:

我试图让机器人对命令发送的最后一条消息做出反应,我已经尝试过,但它给了我一个错误 Message Argument must be a Message 此外,我还试图让机器人对带有用户提供的消息 ID 的消息,以下是我尝试做的事情

# (Most important) This should react on the last message with the ???? emoji (Not this message it self)
@client.command(pass_context=True)
async def react(ctx):
    Channel=ctx.message.channel
    logs = client.logs_from(Channel, limit=1) #This is meant to check the last (1) message
    await client.add_reaction(logs, emoji="????")

还有其他脚本

# (Least important) This should react to a message that has the message id you give
@client.command(pass_context=True)
async def like(ctx,Message_id):
    message=Message_id
    await client.add_reaction(message, emoji="????")

【问题讨论】:

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


    【解决方案1】:

    对于您的第一个示例,logs_from 是一个协程(需要等待)returns a generator,因此您可以执行类似的操作

    async for msg in logs:
      await client.add_reaction(msg, emoji="?")
    

    对于您的第二个示例,您将命令的第一个参数作为消息传递,但您得到的是消息 id 的字符串
    您需要先将该字符串转换为整数,然后找到具有该 id 的消息

    message= await client.get_message(ctx.message.channel, int(Message_id))
    await client.add_reaction(message, emoji="?")
    

    第一个例子的注释
    如果您想获得倒数第二条消息,您可以执行类似

    的操作
    await logs.__anext__()
    await client.add_reaction(await logs.__anext__(), emoji="?")
    

    【讨论】:

    • 它有效,但第一个问题是它对您发送的消息做出反应,而不是对某人发送的最后一条消息而不是您发送的消息作出反应
    • 这可能有点棘手,因为 LogsFromIterator 使用 asynchronous generator function 来做到这一点,但我已经用解决方案编辑了我的答案
    猜你喜欢
    • 2021-07-04
    • 2021-04-11
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多