【问题标题】:How to make discord python bot react to its own message?如何让 discord python bot 对自己的消息做出反应?
【发布时间】:2019-06-07 23:12:51
【问题描述】:

目前正在开发一个发送嵌入的小型机器人,我希望它对自己做出反应,但不确定如何做到这一点。有什么帮助吗?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    获取您想要回复的消息并使用Client.add_reaction()

    例如,如果您正在对嵌入做出反应

    msg = await bot.send_message(ctx.message.channel,embed=embed)
    await bot.add_reaction(msg, "?")
    

    【讨论】:

    • 我收到一个错误提示 ctx 未定义,我在哪里实现?
    • ctx 是一个上下文变量,您可以将其传递给您的函数以获取消息的某些属性。例如:async def test(ctx):。如果您能分享创建嵌入的函数的代码会有所帮助
    • 这是创建嵌入的代码部分:@client.command() async def embed1(): embed = discord.Embed(title="Title", color=0xa27250) embed .set_thumbnail(url="") embed.add_field(name="text", value="text", inline=False) embed.set_footer(text="text") await client.send_message(discord.Object(id='频道 id'), embed=embed)
    • 是的,所以现在在最后一行有msg = await client.send_message(discord.Object(id='458269214822891522'), embed=embed) 然后await bot.add_reaction(msg, "?") 或任何你想要的表情,就像上面的例子一样
    • 一切正常,但是当我执行该函数时,我得到:“命令引发异常:HTTPException:BAD REQUEST(状态代码:400)”我查了一下,但嵌入不是接近 2000 个字符的限制?
    【解决方案2】:

    如果你的代码中有这个

    if message.author == client.user:
        return
    

    您的机器人不会响应自己,也不应该正常响应自己。解决此问题的一种方法是将您想要执行的任何代码放在上面,尽管 message.author == client.user 在它上面。例如:

    # This will respond to itself
    if (message.content.lower() == "!yell"):
        msg = "!yell"
        await client.send_message(message.channel, msg)
    
    # This checks to see if the author is the client    
    if message.author == client.user:
        return
    
    # This will not respond to itself
    if (message.content.lower() == "!yell2"):
        msg = "!yell"
        await client.send_message(message.channel, msg)
    

    !yell 将导致您的机器人与自己对话,直到达到消息阈值,但是,!yell2 不会,因为它位于检查它是否是 client.user 的部分之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 2020-07-29
      • 2019-10-18
      • 1970-01-01
      • 2018-12-15
      • 2021-07-13
      • 1970-01-01
      • 2020-07-27
      相关资源
      最近更新 更多