Working on refactor

This commit is contained in:
Dalton 2019-12-25 00:58:54 -08:00
parent 64b9f5ea68
commit 91764f2ae5
4 changed files with 275 additions and 251 deletions

278
main.ts
View file

@ -1,95 +1,25 @@
// Created by Dalton Lange // Created by Dalton Lange
//===Requirements=== //===Requirements===
const fs = require('fs'); export const Discord = require('discord.js');
const Discord = require('discord.js');
const client = new Discord.Client(); const client = new Discord.Client();
export const fs = require('fs');
const info = JSON.parse(fs.readFileSync(`./data/info.json`)); const info = JSON.parse(fs.readFileSync(`./data/info.json`));
import { player } from "./enums";
//===Data stored for use, such as class, race, and last names=== import { player } from "./scripts/enums";
const races: Array<string> = ["tree", "ground", "chipmunk"]; import { processes, channels } from "./scripts/commands";
const classes: Array<string> = ["rogue", "berserker", "knight", "ranger", "priest"]; import { encounterInProgress } from "./scripts/functions";
const lastNames: Array<string> = ["Nutcrack", "Nutmeg", "Seedsower", "McScuiri", "Rodentia", "Arbora", "Patagi"];
const COLOR: string = "#E81B47";
const newPlayerTemplate: player = {
"name": "",
"race": "",
"class": "",
"weapon": {
"weaponName": "",
"attackStat": 0,
"defenseStat": 0,
"secondStat": 0
},
"level": 1,
"nuts": 0,
"staminaMax": 0,
"stamina": 0,
"dm": false,
"dm_points": 0
}
//===Commands===
const processes: any = {
"help": {
title: "Help",
description: "Show a description of a command",
args: "**command** - the command you'd like to know more about",
run: (msg: any, data: player, args: string[], file: any) => help(msg, args)
},
"list": {
title: "List",
description: "List all commands",
args: "",
run: (msg: any, data: player, args: string[], file: any) => list(msg)
},
"read": {
title: "Read",
description: "Read a description of your squirrel",
args: "",
run: (msg: any, data: player, args: string[], file: any) => read(msg, data)
},
"namechange": {
title: "Namechange",
description: "Change the name of your squirrel, can only be used at level 1",
args: "**name** - the new name for your squirrel",
run: (msg: any, data: player, args: string[], file: any) => nameChange(msg, data, args, file)
},
"races": {
title: "Races",
description: "List all the races",
args: "",
run: (msg: any, data: player, args: string[], file: any) => printRaces(msg)
},
"classes": {
title: "Classes",
description: "List all the classes",
args: "",
run: (msg: any, data: player, args: string[], file: any) => printClasses(msg)
},
"create": {
title: "Create",
description: "Create a squirrel, can only be used once",
args: "**name** - the name of the created squirrel\n**race** - The race of the created squirrel, type !races for race options\n**class** - The class of the created squirrel, type !classes for class options",
run: (msg: any, data: player, args: string[], file: any) => msg.reply(`You already have a squirrel silly.`)
},
}
//===Global editable variables=== //===Global editable variables===
let encounterChannel;
let preparingChannel;
let generalChannel;
let barChannel;
// let encounterInProgress: boolean;
//===Initalization=== //===Initalization===
client.on('ready', () => { client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`); console.log(`Logged in as ${client.user.tag}!`);
encounterChannel = client.channels.find(ch => ch.name === 'the-wild'); channels.general = client.channels.find(ch => ch.id === info.channels.general);
preparingChannel = client.channels.find(ch => ch.name === 'preparing-for-expedition'); channels.encounter = client.channels.find(ch => ch.id === info.channels.encounter);
generalChannel = client.channels.find(ch => ch.name === 'general-table'); channels.preparing = client.channels.find(ch => ch.id === info.channels.preparing);
barChannel = client.channels.find(ch => ch.name === 'the-bar');
// const preparingCollector = new Discord.MessageCollector(preparingChannel); // const preparingCollector = new Discord.MessageCollector(preparingChannel);
// preparingCollector.on('collect', msg => { // preparingCollector.on('collect', msg => {
// console.log(msg.content); // console.log(msg.content);
@ -102,13 +32,28 @@ client.on('ready', () => {
client.on('message', msg => { client.on('message', msg => {
//Reasons to exit //Reasons to exit
if (!msg.content.split(" ")[0].startsWith(info.prefix)) return; // if the message doesn't start with the prefix if (!msg.content.split(" ")[0].startsWith(info.prefix)) return; // if the message doesn't start with the prefix
switch(msg.channel) {
case channels.encounter:
if(encounterInProgress) return;
encounterChannelMessage(msg);
break;
default:
defaultChannelMessage(msg);
}
});
// 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 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 command: string = messageContent[0]; // This is the command they are using
let args: Array<string> = [""]; // arguments of the message let args: Array<string> = [""]; // arguments of the message
if (messageContent.length > 1) args = messageContent.slice(1); if (messageContent.length > 1) args = messageContent.slice(1);
let authorId: number = msg.author.id; // Message Author let authorId: number = msg.author.id; // Message Author
let playerFile: string = `./data/playerdata/${authorId}.json`; let playerFile: string = `./data/playerdata/${authorId}.json`;
@ -124,7 +69,7 @@ client.on('message', msg => {
} else { } else {
if(command === `${info.prefix}create`){ // if they are using the create command if(command === `${info.prefix}create`){ // if they are using the create command
console.log(`Attempting to make a squirrel for ${authorId}`); console.log(`Attempting to make a squirrel for ${authorId}`);
if (create(msg, args, playerFile)) if (processes["create"]["create"].run(msg, null, args, playerFile))
console.log(`Squirrel made succesfully for ${authorId}`); console.log(`Squirrel made succesfully for ${authorId}`);
else else
console.log(`Failed to create a squirrel for ${authorId}`); console.log(`Failed to create a squirrel for ${authorId}`);
@ -132,175 +77,8 @@ client.on('message', msg => {
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"`); 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"`);
} }
} }
});
// Log the bot in
client.login(info.key);
//===FUNCTIONS===
/*
function encounter(encounterChannel: any): void {
if (!encounterInProgress) { // randomInt(1,60) === 60 &&
console.log("Starting Encounter");
encounterInProgress = true;
encounterChannel.send("An encounter is beginning!");
encounterChannel.send(`\`■■■ ■■■ ■■■ ■■■\`\n\`■■■ ■■■ ■■■ ■■■\`\n\`■■■ ■■■ ■■■ ■■■\``).then(msg => {
console.log("Encounter finished");
encounterInProgress = false;
});
}
}
*/
function randomInt(low: number, high: number): number {
return Math.floor(Math.random() * (high - low + 1) + low)
} }
function raceText(race: string): string { function encounterChannelMessage(msg: any): void {
if (race === races[2]) return capitalize(race);
else if (race === races[0] || race === races[1]) return `${capitalize(race)} Squirrel`;
else {
console.log(`Error ${race} is not a valid race`);
return "";
}
}
function capitalize(toCaps: string): string {
return toCaps.charAt(0).toUpperCase() + toCaps.slice(1).toLowerCase();
}
//===COMMAND FUNCTIONS==
function help(msg: any, args: string[]): void {
let argument: string = args[0].toLowerCase();
if(argument === "") {
msg.reply(`Please provide a command, like "!help help", or "!help races"\nAlternatively type "!list" for a list of commands`);
} else if(processes.hasOwnProperty(argument)){
let embed = new Discord.RichEmbed()
.setTitle(processes[argument].title)
.setColor(COLOR)
.setDescription(processes[argument].description);
if(processes[argument].args === "") {
embed.addField("Arguments:", `This command has no arguments`);
} else {
embed.addField("Arguments:", processes[argument].args);
}
msg.channel.send(embed);
} else {
msg.reply(`That command does not exist`);
}
}
function list(msg: any): void {
let embed = new Discord.RichEmbed()
.setTitle("List of Commands")
.setColor(COLOR);
let description: string = "";
Object.keys(processes).forEach(process => {
description += `**${processes[process].title}:** ${processes[process].description}.\n`;
});
embed.setDescription(description);
msg.author.send(embed);
}
function read(msg: any, data: player): void {
let embed = new Discord.RichEmbed()
.setTitle(msg.author.username)
.setColor(COLOR)
.setThumbnail(msg.author.avatarURL)
.setDescription("Squirrel Info")
.addField("Name", data.name)
.addField("Race", raceText(data.race))
.addField("Class", capitalize(data.class))
.addField("Nuts", data.nuts)
.addField("Level", data.level);
if (data.dm === true)
embed.addField("DM Points", data.dm_points);
msg.channel.send(embed);
}
function create(msg: any, args: Array<string>, file: any): boolean {
if (args.length === 3) {
let newPlayer: player = newPlayerTemplate;
let incorrect: string = "";
incorrect = " race";
races.some(type => {
if (args[1].toLowerCase() == type) {
incorrect = "";
return true;
}
});
if (incorrect == "")
incorrect = " class";
else
incorrect += ", and class";
classes.some(type => {
if (args[2].toLowerCase() == type) {
if (incorrect == " race, and class")
incorrect = " race";
else
incorrect = "";
return true;
}
});
if (incorrect === "") {
newPlayer.name = args[0] + " " + lastNames[randomInt(0, lastNames.length - 1)];
newPlayer.race = args[1];
newPlayer.class = args[2];
fs.writeFileSync(file, JSON.stringify(newPlayer));
msg.reply(`Squirrel ${newPlayer.name} has been created`);
return true;
} else {
msg.reply(`Some parameters were entered incorrectly. Remember the format is "create name race class". Your name can be whatever you want, but you have to choose from !race, and !class. You incorrectly entered your:${incorrect}.`);
return false;
}
} else {
msg.reply(`Too many or too few parameters were entered. The format is "create name race class". You only get to choose your first name, don't write a last name.`);
return false;
}
}
function nameChange(msg: any, data: player, args: string[], file: any): void {
if (data.level == 1) {
if (args.length >= 1) {
data.name = capitalize(args[0]) + " " + data.name.split(' ')[1];
fs.writeFileSync(file, JSON.stringify(data));
msg.reply(`Name changed to ${data.name}`);
} else {
msg.reply(`Please provide a name`);
}
} else {
msg.reply(`Sorry, you have to be level 1 to change your name. You're stuck with ${data.name}`);
}
}
function printRaces(msg: any): void {
let print = "The races are: ";
for (let i = 0; i < races.length; i++) {
if (i === races.length - 1) {
print += `& ${raceText(races[i])}s`;
} else {
print += `${raceText(races[i])}s, `;
}
}
msg.reply(print);
}
function printClasses(msg: any): void {
let print = "The classes are: ";
for (let i = 0; i < classes.length; i++) {
if (i === classes.length - 1) {
print += `& ${capitalize(classes[i])}s`;
} else {
print += `${capitalize(classes[i])}s, `;
}
}
msg.reply(print);
} }

214
scripts/commands.ts Normal file
View file

@ -0,0 +1,214 @@
import { Discord, fs} from "../main";
import { player } from "./enums";
import { raceText, capitalize, randomInt, encounter } from "./functions";
//===Data stored for use, such as class, race, and last names===
export const races: Array<string> = ["tree", "ground", "chipmunk"];
const classes: Array<string> = ["rogue", "berserker", "knight", "ranger", "priest"];
const lastNames: Array<string> = ["Nutcrack", "Nutmeg", "Seedsower", "McScuiri", "Rodentia", "Arbora", "Patagi"];
const COLOR: string = "#E81B47";
const newPlayerTemplate: player = {
"name": "",
"race": "",
"class": "",
"weapon": {
"weaponName": "",
"attackStat": 0,
"defenseStat": 0,
"secondStat": 0
},
"level": 1,
"nuts": 0,
"staminaMax": 0,
"stamina": 0,
"dm": false,
"dm_points": 0
}
export let channels: any = {
general: "",
encounter: "",
preparing: ""
}
export let processes: any = {
"test": {
title: "Test",
description: "For testing",
args: "",
run: (msg: any, data: player, args: string[], file: any) => encounter()
},
"help": {
title: "Help",
description: "Show a description of a command",
args: "**command** - the command you'd like to know more about",
run: (msg: any, data: player, args: string[], file: any) => help(msg, args)
},
"list": {
title: "List",
description: "List all commands",
args: "",
run: (msg: any, data: player, args: string[], file: any) => list(msg)
},
"read": {
title: "Read",
description: "Read a description of your squirrel",
args: "",
run: (msg: any, data: player, args: string[], file: any) => read(msg, data)
},
"namechange": {
title: "Namechange",
description: "Change the name of your squirrel, can only be used at level 1",
args: "**name** - the new name for your squirrel",
run: (msg: any, data: player, args: string[], file: any) => nameChange(msg, data, args, file)
},
"races": {
title: "Races",
description: "List all the races",
args: "",
run: (msg: any, data: player, args: string[], file: any) => printRaces(msg)
},
"classes": {
title: "Classes",
description: "List all the classes",
args: "",
run: (msg: any, data: player, args: string[], file: any) => printClasses(msg)
},
"create": {
title: "Create",
description: "Create a squirrel, can only be used once",
args: "**name** - the name of the created squirrel\n**race** - The race of the created squirrel, type !races for race options\n**class** - The class of the created squirrel, type !classes for class options",
run: (msg: any, data: player, args: string[], file: any) => create(msg, args, file)
}
}
//===COMMAND FUNCTIONS==
function help(msg: any, args: string[]): void {
let argument: string = args[0].toLowerCase();
if(argument === "") {
msg.reply(`Please provide a command, like "!help help", or "!help races"\nAlternatively type "!list" for a list of commands`);
} else if(processes.hasOwnProperty(argument)){
let embed = new Discord.RichEmbed()
.setTitle(processes[argument].title)
.setColor(COLOR)
.setDescription(processes[argument].description);
if(processes[argument].args === "") {
embed.addField("Arguments:", `This command has no arguments`);
} else {
embed.addField("Arguments:", processes[argument].args);
}
msg.channel.send(embed);
} else {
msg.reply(`That command does not exist`);
}
}
function list(msg: any): void {
let embed = new Discord.RichEmbed()
.setTitle("List of Commands")
.setColor(COLOR);
let description: string = "";
Object.keys(processes).forEach(process => {
description += `**${processes[process].title}:** ${processes[process].description}.\n`;
});
embed.setDescription(description);
msg.author.send(embed);
}
function read(msg: any, data: player): void {
let embed = new Discord.RichEmbed()
.setTitle(msg.author.username)
.setColor(COLOR)
.setThumbnail(msg.author.avatarURL)
.setDescription("Squirrel Info")
.addField("Name", data.name)
.addField("Race", raceText(data.race))
.addField("Class", capitalize(data.class))
.addField("Nuts", data.nuts)
.addField("Level", data.level);
if (data.dm === true)
embed.addField("DM Points", data.dm_points);
msg.channel.send(embed);
}
function create(msg: any, args: Array<string>, file: any): boolean {
if (args.length === 3) {
let newPlayer: player = newPlayerTemplate;
let incorrect: string = "";
incorrect = " race";
races.some(type => {
if (args[1].toLowerCase() == type) {
incorrect = "";
return true;
}
});
if (incorrect == "")
incorrect = " class";
else
incorrect += ", and class";
classes.some(type => {
if (args[2].toLowerCase() == type) {
if (incorrect == " race, and class")
incorrect = " race";
else
incorrect = "";
return true;
}
});
if (incorrect === "") {
newPlayer.name = args[0] + " " + lastNames[randomInt(0, lastNames.length - 1)];
newPlayer.race = args[1];
newPlayer.class = args[2];
fs.writeFileSync(file, JSON.stringify(newPlayer));
msg.reply(`Squirrel ${newPlayer.name} has been created`);
return true;
} else {
msg.reply(`Some parameters were entered incorrectly. Remember the format is "create name race class". Your name can be whatever you want, but you have to choose from !race, and !class. You incorrectly entered your:${incorrect}.`);
return false;
}
} else {
msg.reply(`Too many or too few parameters were entered. The format is "create name race class". You only get to choose your first name, don't write a last name.`);
return false;
}
}
function nameChange(msg: any, data: player, args: string[], file: any): void {
if (data.level == 1) {
if (args.length >= 1) {
data.name = capitalize(args[0]) + " " + data.name.split(' ')[1];
fs.writeFileSync(file, JSON.stringify(data));
msg.reply(`Name changed to ${data.name}`);
} else {
msg.reply(`Please provide a name`);
}
} else {
msg.reply(`Sorry, you have to be level 1 to change your name. You're stuck with ${data.name}`);
}
}
function printRaces(msg: any): void {
let print = "The races are: ";
for (let i = 0; i < races.length; i++) {
if (i === races.length - 1) {
print += `& ${raceText(races[i])}s`;
} else {
print += `${raceText(races[i])}s, `;
}
}
msg.reply(print);
}
function printClasses(msg: any): void {
let print = "The classes are: ";
for (let i = 0; i < classes.length; i++) {
if (i === classes.length - 1) {
print += `& ${capitalize(classes[i])}s`;
} else {
print += `${capitalize(classes[i])}s, `;
}
}
msg.reply(print);
}

View file

@ -16,4 +16,4 @@ export interface weapon {
attackStat: number; attackStat: number;
defenseStat: number; defenseStat: number;
secondStat: number; secondStat: number;
} }

32
scripts/functions.ts Normal file
View file

@ -0,0 +1,32 @@
import { races, channels } from "./commands";
export let encounterInProgress: boolean = false;
export function encounter(): void {
if (encounterInProgress == false) { // randomInt(1,20) === 20 &&
console.log("Starting Encounter");
encounterInProgress = true;
channels.encounter.send("An encounter is beginning!");
channels.encounter.send(`\`■■■ ■■■ ■■■ ■■■\`\n\`■■■ ■■■ ■■■ ■■■\`\n\`■■■ ■■■ ■■■ ■■■\``)
console.log("Encounter finished");
encounterInProgress = false;
}
}
export function randomInt(low: number, high: number): number {
return Math.floor(Math.random() * (high - low + 1) + low)
}
export function raceText(race: string): string {
if (race === races[2]) return capitalize(race);
else if (race === races[0] || race === races[1]) return `${capitalize(race)} Squirrel`;
else {
console.log(`Error ${race} is not a valid race`);
return "";
}
}
export function capitalize(toCaps: string): string {
return toCaps.charAt(0).toUpperCase() + toCaps.slice(1).toLowerCase();
}