Search code examples
pythondiscorddiscord.pybots

Python discord bot deletes every message except specific ones


it's my first time coding something and making a discord bot. But I've been trying to figure it out what is wrong with this code for a long time. I want one channel to only able to type specific words and delete everything else, but in this code no matter what i do it deletes everything, cheers

import os
import discord

client = discord.Client(intents=discord.Intents.default())
intents = discord.Intents(message_content=True)


@client.event
async def on_ready():
  print('BOTCADO ONLINE')



@client.event
async def on_message(message):
  if (message.channel.id == 'id'):
    
    if (message.content != "cado"):
      await message.delete()


client.run(os.getenv('TOKEN'))

i tried basically everything


Solution

  • to get it to work I changed:

    client = discord.Client(intents=discord.Intents.default())
    intents = discord.Intents(message_content=True)
    

    to:

    intents = discord.Intents().all()
    client = commands.Bot(command_prefix="$", intents=intents)
    

    I would only use the intents you need to use however. I only put all for testing proposes.

    Also in this line of code, if (message.channel.id == 'id'):, message.channel.id is being set equal to a string. Message ID's are integers. You have to remove that conditional / replace it. You could set the message ID that corresponds with the channel you want to delete messages in by declaring the id as a variable first at the top of your code. id = < THE CHANNEL ID >