How make a bot delete Discord channel messages that start with or are from a bot

Viewed 55

I have a bot in my Discord server for playing music. As it works with commands, and it answers if there is something like a playlist the channel is spammed with songs or messages that later on are meaningless. So I want the bot that each time it leaves a voice channel, it clears the command messages that were sent to it and the answer messages that it sent.

So I have looked around a bit and came to this code but it doesnt work. I added some console logs to see up where it goes in, and it reaches FLAG 1 but not FLAG 2. So what is wrong in that condition? PREFIX is the "!" that commands start with, and 0123456789 is the bot ID

else if (msg.content.trim().toLowerCase() == _CMD_LEAVE) {
        const channel = msg.channel;
        const messageManager = channel.messages;
        messageManager.fetch({ limit: 100 }).then((messages) => {
             messages.forEach((message) => {
                 console.log("FLAG 1: "+ util.inspect(message))
                 if ( (message.content.startWith(PREFIX)) || (message.author.id == 0123456789)) {
                     console.log("FLAG 2: "+ util.inspect(message))   
                     message.delete();
                 }  
             });
        });
            
}

Thanks in advance

1 Answers

Two things here: startWith should actually be startsWith since you didn't mention any errors I'll assume this is just a typo from the example code.

Second, message.author.id == 0123456789 won't work since IDs should be strings, not numbers.

Related