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);