Help command + Cleaner code

Implemented the help command, and moved global variables into the correct scope.
Commented out encounters until finished.
This commit is contained in:
Julia Lange 2019-03-22 14:02:36 -07:00
parent 011fedb1c2
commit 7b075255b2
2 changed files with 93 additions and 48 deletions

65
main.ts
View file

@ -22,44 +22,43 @@ const processes: any = {
"help": {
title: "Help",
description: "Show a description of a command",
args: "command - the command you'd like to know more about\n",
run: (msg: any, data: player, args: string[], file: any) => help(msg, args)
},
"read": {
title: "Read",
description: "Read a description of your squirrel",
args: "",
run: (msg: any, data: player, args: string[], file: any) => read(msg, data)
},
"create": {
title: "Create",
description: "Create a squirrel, can only be used once.",
args: "name - the name of the created squirrel\nrace - The race of the created squirrel, type !races for race options\nclass - 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.`)
},
"namechange": {
title: "Namechange",
description: "Change the name of your squirrel, can only be used once",
args: "name - the new name for your squirrel",
run: (msg: any, data: player, args: string[], file: any) => nameChange(msg, data, 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)
}
}
const COLOR: string = "#E81B47";
let authorId: number;
let playerFile: string;
let rawPlayerData: string;
let command: string;
let messageContent: Array<string>;
let args: Array<string> = [""];
let encounterInProgress: boolean;
let playerData: player;
// let encounterInProgress: boolean;
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
@ -74,31 +73,34 @@ client.on('ready', () => {
});
client.on('message', msg => {
if (msg.channel.type === "dm") return;
messageContent = msg.content.split(" ");
command = messageContent[0];
if (messageContent.length > 1) args = messageContent.slice(1);
let messageContent: Array<string> = msg.content.split(" ");
let command: string = messageContent[0];
if (!command.startsWith(info.prefix)) return;
authorId = msg.author.id;
playerFile = `./data/playerdata/${authorId}.json`;
let args: Array<string> = [""];
if (messageContent.length > 1) args = messageContent.slice(1);
let authorId: number = msg.author.id;
let playerFile: string = `./data/playerdata/${authorId}.json`;
if (fs.existsSync(playerFile)) {
rawPlayerData = fs.readFileSync(playerFile);
playerData = JSON.parse(rawPlayerData);
let rawPlayerData: string = fs.readFileSync(playerFile);
let playerData: player = JSON.parse(rawPlayerData);
Object.keys(processes).forEach(process => {
if(`!${processes[process].title.toLowerCase()}` === command){
if(`${info.prefix}${processes[process].title.toLowerCase()}` === command){
processes[process].run(msg, playerData, args, playerFile);
}
});
} else if (command === `!create`) {
} else if (command === `${info.prefix}create`) {
console.log(`Attempting to make a squirrel for ${authorId}`);
if (create(msg, args))
if (create(msg, args, playerFile))
console.log(`Squirrel made succesfully for ${authorId}`);
else
console.log(`Failed to create quirrel for ${authorId}`);
@ -106,7 +108,7 @@ client.on('message', msg => {
});
client.login(info.key);
/*
function encounter(encounterChannel: any): void {
if (!encounterInProgress) { // randomInt(1,60) === 60 &&
console.log("Starting Encounter");
@ -122,6 +124,7 @@ function encounter(encounterChannel: any): void {
});
}
}
*/
function randomInt(low: number, high: number): number {
return Math.floor(Math.random() * (high - low + 1) + low)
}
@ -141,8 +144,24 @@ function capitalize(toCaps: string): string {
// Commands:
function help(msg: any, args: string[]) {
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"`);
} else if(processes.hasOwnProperty(argument)){
let embed = new Discord.RichEmbed()
.setTitle(processes[argument].title)
.setDescription(processes[argument].description)
.setColor(COLOR);
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 read(msg: any, data: player): void {
@ -162,7 +181,7 @@ function read(msg: any, data: player): void {
msg.channel.send(embed);
}
function create(msg: any, args: Array<string>): boolean {
function create(msg: any, args: Array<string>, file: any): boolean {
if (args.length === 3) {
let newSquirrel: Array<string> = msg.content.split(' ');
let newPlayer: player = newPlayerTemplate;
@ -194,7 +213,7 @@ function create(msg: any, args: Array<string>): boolean {
newPlayer.name = newSquirrel[1] + " " + lastNames[randomInt(0, lastNames.length - 1)];
newPlayer.race = newSquirrel[2];
newPlayer.class = newSquirrel[3];
fs.writeFileSync(playerFile, JSON.stringify(newPlayer));
fs.writeFileSync(file, JSON.stringify(newPlayer));
msg.reply(`Squirrel ${newPlayer.name} has been created`);
return true;
} else {