My Discord bot keeps crashing whenever I try to run it. The command window opens for a second and then immediately closes. I’m trying to make a moderation bot that can automatically remove inappropriate messages from my server. The bot worked fine before I added the word filtering feature, but now it won’t even start up properly. I think there might be a syntax error or missing bracket somewhere in my message handling code. Can someone help me figure out what’s wrong?
My Code:
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
let previousUser;
const botToken = 'YOUR_TOKEN_HERE';
client.on('ready', () => {
console.log('Bot is online!');
});
client.on('message', msg => {
let messageWords = msg.content.split(' ');
if(messageWords[0] == '!addcmd') {
let cmdContent = msg.content.split('|', 2);
let cmdTitle = msg.content.split(' ');
if(cmdTitle[1].charAt(0) == '!') {
saveNewCommand(cmdContent, cmdTitle);
msg.channel.send('New command ' + cmdTitle[1] + ' added successfully');
} else {
msg.channel.send('Commands must start with !');
}
}
fs.readFile('./data/cmdlist.txt', 'utf8', function(error, data) {
let cmdArray = data.toString().split(';');
for(let j = 0; j < cmdArray.length; j++) {
if(msg.content == cmdArray[j]) {
if(cmdArray[j] == '!list') {
msg.channel.send(cmdArray);
break;
}
if(cmdArray[j] == '!info') {
msg.channel.send('Available commands: !info, !list, !database, !hello');
break;
}
let cmdFile = './data/' + cmdArray[j] + '.txt';
fs.readFile(cmdFile, 'utf8', function(error, content) {
try {
let responses = content.toString().split(';');
let randomIndex = Math.random() * responses.length;
msg.channel.send(responses[Math.floor(randomIndex)]);
} catch(error) {
console.log('Error:', error);
}
});
}
}
});
});
function saveNewCommand(cmdContent, cmdTitle) {
let commandName = cmdTitle[1];
let commandDesc = cmdContent[1];
let exists = false;
fs.readFile('./data/cmdlist.txt', 'utf8', function(error, fileData) {
let existingCmds = fileData.toString().split(';');
for(let k = 0; k < existingCmds.length; k++) {
if(commandName == existingCmds[k]) {
exists = true;
}
}
if(exists) {
writeCommand(commandDesc, true, commandName);
} else {
writeCommand(commandDesc, false, commandName);
}
});
}
client.on('message', msg => {
let user = msg.author;
let content = msg.content.toLowerCase();
let adminPrefix = '>';
if(user.id === 'ADMIN_ID_HERE') {
return;
}
if(content.includes('badword')) {
msg.delete();
msg.author.send('That word is not allowed here. Please watch your language!');
}
}) // Missing semicolon here causes the crash
function writeCommand(desc, exists, cmdName) {
let filePath = './data/' + cmdName + '.txt';
if(exists) {
fs.writeFile(filePath, desc, function(error) {
if(error) {
console.error(error);
}
});
} else {
fs.appendFile('./data/cmdlist.txt', cmdName + ';', (error) => {
if(error) throw error;
});
fs.writeFile(filePath, desc, function(error) {
if(error) {
console.error(error);
}
});
}
}
client.login(botToken);