简体   繁体   中英

How do I stop my bot from responding to @everyone pings but also respond to @Bot pings in discord.py?

I have a bot that responds when I ping it by tagging its ID but it also responds to @everyone pings. How do I filter those out? I've tried using an if statement to filter out @everyone and the ID of @everyone but then the bot doesn't respond to normal pings. Here's the current code:

@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)

I'm using the commands extension from the rewrite. The first block of the code is the filter for @everyone and the second part, below the else statement is the response to a ping.

You can use the message.mention_everyone attribute to filter out @everyone and @here-pings like so:

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

Just check if the bot was mentioned in the message, instead of check if it wasn't an everyone ping

if "<!@100>" in message.content: # 100 is the bots id, change it to yours
    # handle the ping here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM