+Commands{change name, help, read} better error catching.

This commit is contained in:
Julia Lange 2020-04-02 19:15:26 -07:00
parent 16fc473a2a
commit bd6374c1c7
6 changed files with 135 additions and 87 deletions

20
main.ts
View file

@ -2,12 +2,12 @@
//===Requirements===
export const fs = require('fs');
export const Discord = require('discord.js');
const Discord = require('discord.js');
const info = JSON.parse(fs.readFileSync(`./data/info.json`));
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const client = new Discord.Client();
export const client = new Discord.Client();
client.commands = new Discord.Collection();
for (const file of commandFiles) {
@ -15,6 +15,8 @@ for (const file of commandFiles) {
client.commands.set(command.name, command);
}
//===Initalization===
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
@ -37,12 +39,18 @@ function executeCommand(msg: any) {
let args: Array<string> = [""]; // arguments of the message
if (messageContent.length > 1) args = messageContent.slice(1);
if (!client.commands.has(command)) return;
if (!client.commands.has(command)) {
msg.reply(`That command: "${command}" doesn't exist. Sad.`);
return;
}
try {
client.commands.get(command).execute(msg, args);
} catch (e) {
console.error(e);
msg.reply(`Failed to execute the given command for some reason. Sad.`);
if (e.code === 'ENOENT')
msg.reply(`Please create a squirrel using "!create" first, before trying that command.`);
else {
console.error(e);
msg.reply(`Failed to execute the given command for some reason. Sad.`);
}
}
}