首页 > 解决方案 > 如何阻止我的机器人响应@everyone pings,但也响应discord.py 中的@Bot pings?

问题描述

我有一个机器人,当我通过标记其 ID 对其进行 ping 操作时会做出响应,但它也会响应 @everyone pings。我如何过滤掉那些?我尝试使用 if 语句过滤掉 @everyone 和 @everyone 的 ID,但是机器人不会响应正常的 ping。这是当前代码:

@bot.listen('on_message')
async def on_ping(message):
  if bot.user.mentioned_in(message):
    if "<@747577395086884998>" in message.content:
      return
    else:
      channel = message.channel
      embed=discord.Embed(color=0x0fd249)
      file = discord.File("logo.png", filename="image.png")
      embed.set_author(name="Fallen Bot", icon_url="attachment://image.png")
      embed.add_field(name="Hi! I'm Fallen Bot!", value=f"My prefix is, `{bot.command_prefix}` and you can use `{bot.command_prefix}help` for help!", inline=False)
      await channel.send(file=file, embed=embed)

我正在使用重写中的命令扩展名。代码的第一块是@everyone 的过滤器,第二部分在else语句下方是对 ping 的响应。

标签: pythonpython-3.xdiscorddiscord.py

解决方案


您可以使用该message.mention_everyone属性过滤掉@everyone 和@here-pings,如下所示:

@bot.listen('on_message')
async def on_ping(message):
    if message.mention_everyone:
        return
    else:
        # this is a real ping, do your thing

推荐阅读