JavaScript Discord Bot Crashing Issue

I’m having trouble with my JavaScript Discord bot. When I run the startup file, the command window opens briefly and then closes immediately. The main issue seems to be in the message handling section where I’m trying to filter inappropriate words. I modified an existing bot template to add word filtering functionality, but now it won’t stay running. The bot was working fine before I added the word moderation feature. I need help identifying what’s causing the crash so I can get my server moderation bot working again.

My Code:

const Discord = require('discord.js');
const client = new Discord.Client();
var fileSystem = require("fs");
var previousUser;

// Bot token placeholder
const botToken = "your_token_here";

client.on('ready', () => {
  console.log('Bot is online!');
});

client.on('message', msg => {
	var splitMsg = msg.content.split(" ");
	if(splitMsg[0] == "!addcmd")
	{
		var cmdContent = msg.content.split("|",2);
		var cmdTitle = msg.content.split(" ");
		if(cmdTitle[1].charAt(0) == "!")
			{
				verifyCommand(cmdContent,cmdTitle);
				msg.channel.send("New command " + cmdTitle[1] + " added successfully");
			} else {
				msg.channel.send("Commands must start with '!'");
			}
	}

	fileSystem.readFile('./data/commands.txt','utf8',function(error,content){
		var cmdList = content.toString().split(";");
		for(j = 0; j < cmdList.length; j++)
		{
			if(msg.content == cmdList[j])
			{
				if(cmdList[j] == "!cmdlist")
					{
						msg.channel.send(cmdList);
						break;
					}
				if(cmdList[j] == "!info")
					{
						msg.channel.send("Available commands: !info, !cmdlist, !data, !hello, !fun and !support");
						break;
					}
				var cmdPath = "./data/" + cmdList[j] + ".txt";
				fileSystem.readFile(cmdPath,'utf8', function(error,content){
				try{
					var responses = content.toString().split(";");
					var randomNum = Math.random() * ((responses.length - 1) - 0) + 0;
					msg.channel.send(responses[Math.floor(randomNum)]);
				}
				catch(error) {
					console.error("",error);
				}
				});
			}
		}
	});
});

function verifyCommand(cmdContent,cmdTitle)
{
	var command = cmdTitle[1];
	var description = cmdContent[1];
	var exists = false;
	fileSystem.readFile('./data/commands.txt','utf8',function(error,content){
		var existingCmds = content.toString().split(";");
		for(j = 0; j < existingCmds.length; j++)
		{
			if(command == existingCmds[j])
			{
				exists = true;
			}
		}
		if(exists == true)
		{
			buildCommand(description,true,command);
		} else if (exists == false)
		{
			buildCommand(description,false,command);
		}
	});
}

client.on('message', msg => { 
    var user = msg.author;
    var text = msg.content.toLowerCase();
    var cmdPrefix = '!'

    if (user.id === 'bot_user_id') { 
    return;
    }

    if (text.includes('badword')) {
        msg.delete();
        msg.author.send('That word is not allowed here. Please follow server rules!')
    }
}

function buildCommand(description,exists,command)
{
	var filePath = "./data/" + command + ".txt";
	if(exists == true)
	{
		fileSystem.writeFile(filePath,description,function(error){
		if(error) {
			return console.error(error);
		}
		});
	} else if (exists == false){
		fileSystem.appendFile('./data/commands.txt',command+';',(error) =>
		{
		if(error) throw error;
		});
		
		fileSystem.writeFile(filePath,description,function(error){
		if(error) {
			return console.error(error);
		}
		});
	}
	return;
}

client.login(botToken);

you’re missing a closing parenthesis in your second message event listener - there’s an extra { without the matching }. also make sure your bot token isn’t literally “your_token_here” lol. try adding some console.log statements to see where exactly it’s crashing.

Your code has several issues beyond Luke’s missing bracket fix. You’ve got two separate client.on('message') listeners - that’ll cause conflicts. Merge the word filtering into the first listener or handle it differently. Your msg.delete() will crash if the bot doesn’t have delete permissions, so wrap it in try-catch. Add process.on('uncaughtException', console.error) at the top to see what’s actually breaking before the window closes. Also check if your data directory exists - the file reading will fail without it.

Had the same crash with my moderation bot. Your file system operations aren’t handling missing files - when commands.txt or the data directory don’t exist, readFile throws unhandled exceptions and kills the process instantly. Create the data folder first and drop in an empty commands.txt file. Your bot token might also be invalid - Discord just disconnects bad tokens without telling you why. Add error handling to file operations and throw in client.on('error', console.error) to catch auth issues. That syntax error will definitely break things, but the file system crashes are probably killing your process before you see any real output.