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:
parent
011fedb1c2
commit
7b075255b2
2 changed files with 93 additions and 48 deletions
76
main.js
76
main.js
|
|
@ -21,43 +21,42 @@ var processes = {
|
||||||
"help": {
|
"help": {
|
||||||
title: "Help",
|
title: "Help",
|
||||||
description: "Show a description of a command",
|
description: "Show a description of a command",
|
||||||
|
args: "command - the command you'd like to know more about\n",
|
||||||
run: function (msg, data, args, file) { return help(msg, args); }
|
run: function (msg, data, args, file) { return help(msg, args); }
|
||||||
},
|
},
|
||||||
"read": {
|
"read": {
|
||||||
title: "Read",
|
title: "Read",
|
||||||
description: "Read a description of your squirrel",
|
description: "Read a description of your squirrel",
|
||||||
|
args: "",
|
||||||
run: function (msg, data, args, file) { return read(msg, data); }
|
run: function (msg, data, args, file) { return read(msg, data); }
|
||||||
},
|
},
|
||||||
"create": {
|
"create": {
|
||||||
title: "Create",
|
title: "Create",
|
||||||
description: "Create a squirrel, can only be used once.",
|
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: function (msg, data, args, file) { return msg.reply("You already have a squirrel silly."); }
|
run: function (msg, data, args, file) { return msg.reply("You already have a squirrel silly."); }
|
||||||
},
|
},
|
||||||
"namechange": {
|
"namechange": {
|
||||||
title: "Namechange",
|
title: "Namechange",
|
||||||
description: "Change the name of your squirrel, can only be used once",
|
description: "Change the name of your squirrel, can only be used once",
|
||||||
|
args: "name - the new name for your squirrel",
|
||||||
run: function (msg, data, args, file) { return nameChange(msg, data, file); }
|
run: function (msg, data, args, file) { return nameChange(msg, data, file); }
|
||||||
},
|
},
|
||||||
"races": {
|
"races": {
|
||||||
title: "Races",
|
title: "Races",
|
||||||
description: "List all the races",
|
description: "List all the races",
|
||||||
|
args: "",
|
||||||
run: function (msg, data, args, file) { return printRaces(msg); }
|
run: function (msg, data, args, file) { return printRaces(msg); }
|
||||||
},
|
},
|
||||||
"classes": {
|
"classes": {
|
||||||
title: "Classes",
|
title: "Classes",
|
||||||
description: "List all the classes",
|
description: "List all the classes",
|
||||||
|
args: "",
|
||||||
run: function (msg, data, args, file) { return printClasses(msg); }
|
run: function (msg, data, args, file) { return printClasses(msg); }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var COLOR = "#E81B47";
|
var COLOR = "#E81B47";
|
||||||
var authorId;
|
// let encounterInProgress: boolean;
|
||||||
var playerFile;
|
|
||||||
var rawPlayerData;
|
|
||||||
var command;
|
|
||||||
var messageContent;
|
|
||||||
var args = [""];
|
|
||||||
var encounterInProgress;
|
|
||||||
var playerData;
|
|
||||||
client.on('ready', function () {
|
client.on('ready', function () {
|
||||||
console.log("Logged in as " + client.user.tag + "!");
|
console.log("Logged in as " + client.user.tag + "!");
|
||||||
var encounterChannel = client.channels.find(function (ch) { return ch.name === 'the-wild'; });
|
var encounterChannel = client.channels.find(function (ch) { return ch.name === 'the-wild'; });
|
||||||
|
|
@ -72,43 +71,50 @@ client.on('ready', function () {
|
||||||
client.on('message', function (msg) {
|
client.on('message', function (msg) {
|
||||||
if (msg.channel.type === "dm")
|
if (msg.channel.type === "dm")
|
||||||
return;
|
return;
|
||||||
messageContent = msg.content.split(" ");
|
var messageContent = msg.content.split(" ");
|
||||||
command = messageContent[0];
|
var command = messageContent[0];
|
||||||
if (messageContent.length > 1)
|
|
||||||
args = messageContent.slice(1);
|
|
||||||
if (!command.startsWith(info.prefix))
|
if (!command.startsWith(info.prefix))
|
||||||
return;
|
return;
|
||||||
authorId = msg.author.id;
|
var args = [""];
|
||||||
playerFile = "./data/playerdata/" + authorId + ".json";
|
if (messageContent.length > 1)
|
||||||
|
args = messageContent.slice(1);
|
||||||
|
var authorId = msg.author.id;
|
||||||
|
var playerFile = "./data/playerdata/" + authorId + ".json";
|
||||||
if (fs.existsSync(playerFile)) {
|
if (fs.existsSync(playerFile)) {
|
||||||
rawPlayerData = fs.readFileSync(playerFile);
|
var rawPlayerData = fs.readFileSync(playerFile);
|
||||||
playerData = JSON.parse(rawPlayerData);
|
var playerData_1 = JSON.parse(rawPlayerData);
|
||||||
Object.keys(processes).forEach(function (process) {
|
Object.keys(processes).forEach(function (process) {
|
||||||
if ("!" + processes[process].title.toLowerCase() === command) {
|
if ("" + info.prefix + processes[process].title.toLowerCase() === command) {
|
||||||
processes[process].run(msg, playerData, args, playerFile);
|
processes[process].run(msg, playerData_1, args, playerFile);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (command === "!create") {
|
else if (command === info.prefix + "create") {
|
||||||
console.log("Attempting to make a squirrel for " + authorId);
|
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);
|
console.log("Squirrel made succesfully for " + authorId);
|
||||||
else
|
else
|
||||||
console.log("Failed to create quirrel for " + authorId);
|
console.log("Failed to create quirrel for " + authorId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
client.login(info.key);
|
client.login(info.key);
|
||||||
function encounter(encounterChannel) {
|
/*
|
||||||
if (!encounterInProgress) { // randomInt(1,60) === 60 &&
|
function encounter(encounterChannel: any): void {
|
||||||
|
if (!encounterInProgress) { // randomInt(1,60) === 60 &&
|
||||||
console.log("Starting Encounter");
|
console.log("Starting Encounter");
|
||||||
encounterInProgress = true;
|
encounterInProgress = true;
|
||||||
encounterChannel.send("An encounter is beginning!");
|
encounterChannel.send("An encounter is beginning!");
|
||||||
encounterChannel.send("`\u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0`\n`\u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0`\n`\u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0 \u25A0\u25A0\u25A0`").then(function (msg) {
|
|
||||||
|
encounterChannel.send(`\`■■■ ■■■ ■■■ ■■■\`\n\`■■■ ■■■ ■■■ ■■■\`\n\`■■■ ■■■ ■■■ ■■■\``).then(msg => {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
console.log("Encounter finished");
|
console.log("Encounter finished");
|
||||||
encounterInProgress = false;
|
encounterInProgress = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
function randomInt(low, high) {
|
function randomInt(low, high) {
|
||||||
return Math.floor(Math.random() * (high - low + 1) + low);
|
return Math.floor(Math.random() * (high - low + 1) + low);
|
||||||
}
|
}
|
||||||
|
|
@ -127,6 +133,26 @@ function capitalize(toCaps) {
|
||||||
}
|
}
|
||||||
// Commands:
|
// Commands:
|
||||||
function help(msg, args) {
|
function help(msg, args) {
|
||||||
|
var argument = args[0].toLowerCase();
|
||||||
|
if (argument === "") {
|
||||||
|
msg.reply("Please provide a command, like \"!help help\", or \"!help races\"");
|
||||||
|
}
|
||||||
|
else if (processes.hasOwnProperty(argument)) {
|
||||||
|
var 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, data) {
|
function read(msg, data) {
|
||||||
var embed = new Discord.RichEmbed()
|
var embed = new Discord.RichEmbed()
|
||||||
|
|
@ -143,7 +169,7 @@ function read(msg, data) {
|
||||||
embed.addField("DM Points", data.dm_points);
|
embed.addField("DM Points", data.dm_points);
|
||||||
msg.channel.send(embed);
|
msg.channel.send(embed);
|
||||||
}
|
}
|
||||||
function create(msg, args) {
|
function create(msg, args, file) {
|
||||||
if (args.length === 3) {
|
if (args.length === 3) {
|
||||||
var newSquirrel_1 = msg.content.split(' ');
|
var newSquirrel_1 = msg.content.split(' ');
|
||||||
var newPlayer = newPlayerTemplate;
|
var newPlayer = newPlayerTemplate;
|
||||||
|
|
@ -172,7 +198,7 @@ function create(msg, args) {
|
||||||
newPlayer.name = newSquirrel_1[1] + " " + lastNames[randomInt(0, lastNames.length - 1)];
|
newPlayer.name = newSquirrel_1[1] + " " + lastNames[randomInt(0, lastNames.length - 1)];
|
||||||
newPlayer.race = newSquirrel_1[2];
|
newPlayer.race = newSquirrel_1[2];
|
||||||
newPlayer["class"] = newSquirrel_1[3];
|
newPlayer["class"] = newSquirrel_1[3];
|
||||||
fs.writeFileSync(playerFile, JSON.stringify(newPlayer));
|
fs.writeFileSync(file, JSON.stringify(newPlayer));
|
||||||
msg.reply("Squirrel " + newPlayer.name + " has been created");
|
msg.reply("Squirrel " + newPlayer.name + " has been created");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
65
main.ts
65
main.ts
|
|
@ -22,44 +22,43 @@ const processes: any = {
|
||||||
"help": {
|
"help": {
|
||||||
title: "Help",
|
title: "Help",
|
||||||
description: "Show a description of a command",
|
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)
|
run: (msg: any, data: player, args: string[], file: any) => help(msg, args)
|
||||||
},
|
},
|
||||||
"read": {
|
"read": {
|
||||||
title: "Read",
|
title: "Read",
|
||||||
description: "Read a description of your squirrel",
|
description: "Read a description of your squirrel",
|
||||||
|
args: "",
|
||||||
run: (msg: any, data: player, args: string[], file: any) => read(msg, data)
|
run: (msg: any, data: player, args: string[], file: any) => read(msg, data)
|
||||||
},
|
},
|
||||||
"create": {
|
"create": {
|
||||||
title: "Create",
|
title: "Create",
|
||||||
description: "Create a squirrel, can only be used once.",
|
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.`)
|
run: (msg: any, data: player, args: string[], file: any) => msg.reply(`You already have a squirrel silly.`)
|
||||||
},
|
},
|
||||||
"namechange": {
|
"namechange": {
|
||||||
title: "Namechange",
|
title: "Namechange",
|
||||||
description: "Change the name of your squirrel, can only be used once",
|
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)
|
run: (msg: any, data: player, args: string[], file: any) => nameChange(msg, data, file)
|
||||||
},
|
},
|
||||||
"races": {
|
"races": {
|
||||||
title: "Races",
|
title: "Races",
|
||||||
description: "List all the races",
|
description: "List all the races",
|
||||||
|
args: "",
|
||||||
run: (msg: any, data: player, args: string[], file: any) => printRaces(msg)
|
run: (msg: any, data: player, args: string[], file: any) => printRaces(msg)
|
||||||
},
|
},
|
||||||
"classes": {
|
"classes": {
|
||||||
title: "Classes",
|
title: "Classes",
|
||||||
description: "List all the classes",
|
description: "List all the classes",
|
||||||
|
args: "",
|
||||||
run: (msg: any, data: player, args: string[], file: any) => printClasses(msg)
|
run: (msg: any, data: player, args: string[], file: any) => printClasses(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const COLOR: string = "#E81B47";
|
const COLOR: string = "#E81B47";
|
||||||
|
|
||||||
let authorId: number;
|
// let encounterInProgress: boolean;
|
||||||
let playerFile: string;
|
|
||||||
let rawPlayerData: string;
|
|
||||||
let command: string;
|
|
||||||
let messageContent: Array<string>;
|
|
||||||
let args: Array<string> = [""];
|
|
||||||
let encounterInProgress: boolean;
|
|
||||||
let playerData: player;
|
|
||||||
|
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
console.log(`Logged in as ${client.user.tag}!`);
|
console.log(`Logged in as ${client.user.tag}!`);
|
||||||
|
|
@ -74,31 +73,34 @@ client.on('ready', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('message', msg => {
|
client.on('message', msg => {
|
||||||
|
|
||||||
if (msg.channel.type === "dm") return;
|
if (msg.channel.type === "dm") return;
|
||||||
|
|
||||||
messageContent = msg.content.split(" ");
|
let messageContent: Array<string> = msg.content.split(" ");
|
||||||
command = messageContent[0];
|
let command: string = messageContent[0];
|
||||||
if (messageContent.length > 1) args = messageContent.slice(1);
|
|
||||||
|
|
||||||
if (!command.startsWith(info.prefix)) return;
|
if (!command.startsWith(info.prefix)) return;
|
||||||
|
|
||||||
authorId = msg.author.id;
|
let args: Array<string> = [""];
|
||||||
playerFile = `./data/playerdata/${authorId}.json`;
|
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)) {
|
if (fs.existsSync(playerFile)) {
|
||||||
|
|
||||||
rawPlayerData = fs.readFileSync(playerFile);
|
let rawPlayerData: string = fs.readFileSync(playerFile);
|
||||||
playerData = JSON.parse(rawPlayerData);
|
let playerData: player = JSON.parse(rawPlayerData);
|
||||||
|
|
||||||
Object.keys(processes).forEach(process => {
|
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);
|
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}`);
|
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}`);
|
console.log(`Squirrel made succesfully for ${authorId}`);
|
||||||
else
|
else
|
||||||
console.log(`Failed to create quirrel for ${authorId}`);
|
console.log(`Failed to create quirrel for ${authorId}`);
|
||||||
|
|
@ -106,7 +108,7 @@ client.on('message', msg => {
|
||||||
});
|
});
|
||||||
|
|
||||||
client.login(info.key);
|
client.login(info.key);
|
||||||
|
/*
|
||||||
function encounter(encounterChannel: any): void {
|
function encounter(encounterChannel: any): void {
|
||||||
if (!encounterInProgress) { // randomInt(1,60) === 60 &&
|
if (!encounterInProgress) { // randomInt(1,60) === 60 &&
|
||||||
console.log("Starting Encounter");
|
console.log("Starting Encounter");
|
||||||
|
|
@ -122,6 +124,7 @@ function encounter(encounterChannel: any): void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
function randomInt(low: number, high: number): number {
|
function randomInt(low: number, high: number): number {
|
||||||
return Math.floor(Math.random() * (high - low + 1) + low)
|
return Math.floor(Math.random() * (high - low + 1) + low)
|
||||||
}
|
}
|
||||||
|
|
@ -141,8 +144,24 @@ function capitalize(toCaps: string): string {
|
||||||
|
|
||||||
// Commands:
|
// 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 {
|
function read(msg: any, data: player): void {
|
||||||
|
|
@ -162,7 +181,7 @@ function read(msg: any, data: player): void {
|
||||||
msg.channel.send(embed);
|
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) {
|
if (args.length === 3) {
|
||||||
let newSquirrel: Array<string> = msg.content.split(' ');
|
let newSquirrel: Array<string> = msg.content.split(' ');
|
||||||
let newPlayer: player = newPlayerTemplate;
|
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.name = newSquirrel[1] + " " + lastNames[randomInt(0, lastNames.length - 1)];
|
||||||
newPlayer.race = newSquirrel[2];
|
newPlayer.race = newSquirrel[2];
|
||||||
newPlayer.class = newSquirrel[3];
|
newPlayer.class = newSquirrel[3];
|
||||||
fs.writeFileSync(playerFile, JSON.stringify(newPlayer));
|
fs.writeFileSync(file, JSON.stringify(newPlayer));
|
||||||
msg.reply(`Squirrel ${newPlayer.name} has been created`);
|
msg.reply(`Squirrel ${newPlayer.name} has been created`);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue