NovaBot/main.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

// Created by Julia Lange
//===Requirements===
2019-12-25 00:58:54 -08:00
export const fs = require('fs');
export 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();
client.commands = new Discord.Collection();
2019-12-25 00:58:54 -08:00
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
//===Initalization===
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
//===When receiving messages
client.on('message', msg => {
//Reasons to exit
if (!msg.content.split(" ")[0].startsWith(info.prefix) || msg.author.bot) return; // if the message doesn't start with the prefix
executeCommand(msg);
2019-12-25 00:58:54 -08:00
});
2019-12-25 00:58:54 -08:00
// Log the bot in
client.login(info.key);
function executeCommand(msg: any) {
const messageContent: Array<string> = msg.content.slice(1).split(" "); // split message into an array on spaces
const command: string = messageContent[0].toLowerCase(); // This is the command they are using
let args: Array<string> = [""]; // arguments of the message
if (messageContent.length > 1) args = messageContent.slice(1);
if (!client.commands.has(command)) 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.`);
}
}