Home > Mobile >  How do I check if message author is my bot?
How do I check if message author is my bot?

Time:01-22

I have included a code snippet from my Discord bot, What this code snippet does is checks if the message author is a bot. If it is a bot it will delete the message. Of course I have this set to only 1 channel(General). Is there anyway I can add an additional command argument so none of this code runs if the author is MY BOT that I'm running this code on? I assume its something along the lines of 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!")

CodePudding user response:

The author member of message has id. If this id matches client.user.id then this message was sent by your bot.

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

CodePudding user response:

What you can do is something like this

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

You should be able to get YOUR_BOT_ID from the discord developer portal.

  •  Tags:  
  • Related