NovaBot/main.ts

84 lines
3.2 KiB
TypeScript
Raw Normal View History

// Created by Dalton Lange
//===Requirements===
2019-12-25 00:58:54 -08:00
export const Discord = require('discord.js');
const client = new Discord.Client();
2019-12-25 00:58:54 -08:00
export const fs = require('fs');
const info = JSON.parse(fs.readFileSync(`./data/info.json`));
2019-12-25 00:58:54 -08:00
import { player } from "./scripts/enums";
import { processes, channels } from "./scripts/commands";
import { encounterInProgress } from "./scripts/functions";
//===Global editable variables===
2019-12-25 00:58:54 -08:00
//===Initalization===
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
2019-12-25 00:58:54 -08:00
channels.general = client.channels.find(ch => ch.id === info.channels.general);
channels.encounter = client.channels.find(ch => ch.id === info.channels.encounter);
channels.preparing = client.channels.find(ch => ch.id === info.channels.preparing);
// const preparingCollector = new Discord.MessageCollector(preparingChannel);
// preparingCollector.on('collect', msg => {
// console.log(msg.content);
// });
// setInterval( function() { encounter(encounterChannel); }, 300000 );
// encounter(encounterChannel);
});
//===When receiving messages
client.on('message', msg => {
//Reasons to exit
if (!msg.content.split(" ")[0].startsWith(info.prefix)) return; // if the message doesn't start with the prefix
2019-12-25 00:58:54 -08:00
switch(msg.channel) {
case channels.encounter:
if(encounterInProgress) return;
encounterChannelMessage(msg);
break;
default:
defaultChannelMessage(msg);
}
});
2019-12-25 00:58:54 -08:00
// Log the bot in
client.login(info.key);
// Channel Cases
function defaultChannelMessage(msg: any): void {
let messageContent: Array<string> = msg.content.split(" "); // split message into an array on spaces
let command: string = messageContent[0]; // This is the command they are using
let args: Array<string> = [""]; // arguments of the message
if (messageContent.length > 1) args = messageContent.slice(1);
let authorId: number = msg.author.id; // Message Author
let playerFile: string = `./data/playerdata/${authorId}.json`;
if (fs.existsSync(playerFile)) { // If they're file exists, they can use commands, otherwise direct them to the create command
let playerData: player = JSON.parse(fs.readFileSync(playerFile));
Object.keys(processes).forEach(process => { // See if the command they typed is a command
if(`${info.prefix}${processes[process].title.toLowerCase()}` === command){
processes[process].run(msg, playerData, args, playerFile);
}
});
} else {
if(command === `${info.prefix}create`){ // if they are using the create command
console.log(`Attempting to make a squirrel for ${authorId}`);
2019-12-25 00:58:54 -08:00
if (processes["create"]["create"].run(msg, null, args, playerFile))
console.log(`Squirrel made succesfully for ${authorId}`);
else
console.log(`Failed to create a squirrel for ${authorId}`);
} else {
msg.reply(`Please make a squirrel before interacting with the bot. To do so please use the create command. For more information on creation type "!help create"`);
}
}
}
2019-12-25 00:58:54 -08:00
function encounterChannelMessage(msg: any): void {
}