【问题标题】:How do I check if message author is my bot? [duplicate]如何检查消息作者是否是我的机器人? [复制]
【发布时间】:2022-01-22 09:32:33
【问题描述】:

我从我的 Discord 机器人中包含了一个代码 sn-p,这个代码 sn-p 的作用是检查消息作者是否是机器人。如果是机器人,它将删除该消息。当然,我将此设置为仅 1 个频道(常规)。无论如何我可以添加一个额外的命令参数,因此如果作者是我正在运行此代码的我的 BOT,则此代码都不会运行?我假设它类似于if (message.author.this_bot):

@client.event
async def on_message(message):
    if (message.author.bot):
        if message.channel.id == 861627289867517952:
          await message.delete()
          print("A Message Was Deleted In General")
          channel = client.get_channel(861627289867517952)
          await channel.send("Please do not send bot commmands in this channel!")

【问题讨论】:

    标签: python discord


    【解决方案1】:

    messageauthor 成员有id。如果此 ID 与 client.user.id 匹配,则此消息是由您的机器人发送的。

    @client.event
    async def on_message(message):
        if(message.author.id == client.user.id):
            # This message belongs to this bot.
    

    【讨论】:

    • 更简单:if message.author == client.user:
    【解决方案2】:

    你能做的就是这样

    @client.event
    async def on_message(message):
        if (message.author.id != YOUR_BOT_ID) and (message.author.bot):
            if message.channel.id == 861627289867517952:
              await message.delete()
              print("A Message Was Deleted In General")
              channel = client.get_channel(861627289867517952)
              await channel.send("Please do not send bot commmands in this channel!")
    

    您应该能够从 discord 开发者门户获取 YOUR_BOT_ID。

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 2018-09-14
      • 2019-04-24
      • 2021-08-13
      • 1970-01-01
      • 2018-06-03
      • 2017-07-10
      • 2019-02-01
      • 2021-10-13
      相关资源
      最近更新 更多