JavaScript Discord Bot Not Working

Hey everyone, I’m having trouble with my Discord bot. It’s supposed to ban certain words, but it’s not working right now. The main issue seems to be between lines 100 and 115 of the code.

When I try to run it, the Start.bat window just opens and closes quickly. I’m using this for my Discord server and really need it to work.

Here’s a simplified version of the problematic part:

bot.on('message', message => { 
    var sender = message.author;
    var msg = message.content.toUpperCase();
    var prefix = '>'

    if (sender.id === 'SOME_ID') { 
        return;
    }

    if (msg.includes('BAD_WORD')) {
        message.delete();
        message.author.send('This word is not allowed. Please be careful!')
    }
}

Can anyone spot what might be wrong? I’d really appreciate some help fixing this. Thanks!

I’ve dealt with similar Discord bot issues before. The problem might be in your event handling. Make sure you’re using the correct Discord.js version and the appropriate event names. For newer versions, ‘messageCreate’ is used instead of ‘message’.

Also, check if your bot token is correctly set and if the bot has the necessary permissions in your server. Sometimes, permission issues can cause the bot to crash on startup.

For better error handling and debugging, try wrapping your code in a try-catch block and add some console logging. This way, you can see what’s happening when the bot starts up or encounters an error.

Lastly, ensure you’re properly closing the bot connection when you’re done. This can sometimes cause issues with the Start.bat window closing unexpectedly.

I’ve encountered similar issues with Discord bots before. From what I can see, there might be a couple of problems with your code.

First, make sure you’re properly initializing the bot and connecting to Discord. The quick opening and closing of the Start.bat window suggests the bot might be crashing on startup.

Secondly, your event listener is using ‘message’ which is deprecated. Try using ‘messageCreate’ instead. Also, don’t forget to add error handling.

Here’s a modified version that might work better:

bot.on('messageCreate', message => {
    try {
        if (message.author.bot) return;
        
        const msg = message.content.toUpperCase();
        
        if (msg.includes('BAD_WORD')) {
            message.delete();
            message.author.send('This word is not allowed. Please be careful!');
        }
    } catch (error) {
        console.error('Error processing message:', error);
    }
});

Remember to check your bot’s permissions in the server too. It needs the ability to read messages, delete messages, and send DMs. Hope this helps!

hey mate, i think i kno whats up. ur prolly using an old version of discord.js. try updating it and changing ‘message’ to ‘messageCreate’ in ur code. also, make sure ur bots got the right permissions in ur server. that should fix things up for ya!