+Commands{change name, help, read} better error catching.
This commit is contained in:
parent
16fc473a2a
commit
bd6374c1c7
6 changed files with 135 additions and 87 deletions
23
commands/change name.ts
Normal file
23
commands/change name.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { squirrel } from "../scripts/enums";
|
||||||
|
import { capitalize, getPlayerData, updateSquirrelFile } from "../scripts/functions";
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: "namechange",
|
||||||
|
description: "Change the name of your squirrel, can only be used at level 1",
|
||||||
|
args: "**name** - the new name for your squirrel",
|
||||||
|
execute(msg, args) {
|
||||||
|
let data: squirrel = getPlayerData(msg.author.id);
|
||||||
|
|
||||||
|
if (data.level == 1) {
|
||||||
|
if (args.length >= 1) {
|
||||||
|
data.name = capitalize(args[0]) + " " + data.name.split(' ')[1];
|
||||||
|
updateSquirrelFile(msg.author.id, 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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,30 +11,12 @@ let processes: any = {
|
||||||
args: "",
|
args: "",
|
||||||
run: (msg: any, data: player, args: string[], file: any) => encounter()
|
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": {
|
"list": {
|
||||||
title: "List",
|
title: "List",
|
||||||
description: "List all commands",
|
description: "List all commands",
|
||||||
args: "",
|
args: "",
|
||||||
run: (msg: any, data: player, args: string[], file: any) => list(msg)
|
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": {
|
"races": {
|
||||||
title: "Races",
|
title: "Races",
|
||||||
description: "List all the races",
|
description: "List all the races",
|
||||||
|
|
@ -51,26 +33,6 @@ let processes: any = {
|
||||||
|
|
||||||
//===COMMAND FUNCTIONS==
|
//===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 {
|
function list(msg: any): void {
|
||||||
let embed = new Discord.RichEmbed()
|
let embed = new Discord.RichEmbed()
|
||||||
.setTitle("List of Commands")
|
.setTitle("List of Commands")
|
||||||
|
|
@ -83,39 +45,6 @@ function list(msg: any): void {
|
||||||
msg.author.send(embed);
|
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 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 {
|
function printRaces(msg: any): void {
|
||||||
let print = "The races are: ";
|
let print = "The races are: ";
|
||||||
for (let i = 0; i < races.length; i++) {
|
for (let i = 0; i < races.length; i++) {
|
||||||
|
|
|
||||||
33
commands/help.ts
Normal file
33
commands/help.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { client } from "../main";
|
||||||
|
import { capitalize, COLOR } from "../scripts/functions";
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: 'help',
|
||||||
|
description: "Show a description of a command",
|
||||||
|
args: "**command** - the command you'd like to know more about",
|
||||||
|
execute(msg, args) {
|
||||||
|
const command = args[0].toLowerCase();
|
||||||
|
if (command === "") {
|
||||||
|
msg.reply(`Please provide a command, like "!help help", or "!help races"\nAlternatively type "!list" for a list of commands`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (client.commands.has(command)) {
|
||||||
|
const commandData: any = client.commands.get(command);
|
||||||
|
const helpEmbed: object = {
|
||||||
|
color: COLOR,
|
||||||
|
title: capitalize(commandData.name),
|
||||||
|
description: commandData.description,
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'Arguments',
|
||||||
|
value: commandData.args
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
msg.channel.send("{ embed: helpEmbed }");
|
||||||
|
} else {
|
||||||
|
msg.reply(`That command does not exist`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
commands/read.ts
Normal file
41
commands/read.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { squirrel } from "../scripts/enums";
|
||||||
|
import { classText, COLOR, getPlayerData, raceText } from "../scripts/functions";
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: "read",
|
||||||
|
description: "Read a description of your squirrel",
|
||||||
|
args: "",
|
||||||
|
execute(msg, args) {
|
||||||
|
const data: squirrel = getPlayerData(msg.author.id);
|
||||||
|
const squirrelEmbed: object = {
|
||||||
|
color: COLOR,
|
||||||
|
title: msg.author.username,
|
||||||
|
thumbnail: {
|
||||||
|
url: msg.author.avatarURL()
|
||||||
|
},
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'Name',
|
||||||
|
value: data.name
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Race',
|
||||||
|
value: raceText(data.race)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Class',
|
||||||
|
value: classText(data.class)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Nuts',
|
||||||
|
value: data.nuts
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Level',
|
||||||
|
value: data.level
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
msg.channel.send({ embed: squirrelEmbed });
|
||||||
|
}
|
||||||
|
}
|
||||||
20
main.ts
20
main.ts
|
|
@ -2,12 +2,12 @@
|
||||||
|
|
||||||
//===Requirements===
|
//===Requirements===
|
||||||
export const fs = require('fs');
|
export const fs = require('fs');
|
||||||
export const Discord = require('discord.js');
|
const Discord = require('discord.js');
|
||||||
|
|
||||||
const info = JSON.parse(fs.readFileSync(`./data/info.json`));
|
const info = JSON.parse(fs.readFileSync(`./data/info.json`));
|
||||||
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
|
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
|
||||||
|
|
||||||
const client = new Discord.Client();
|
export const client = new Discord.Client();
|
||||||
client.commands = new Discord.Collection();
|
client.commands = new Discord.Collection();
|
||||||
|
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
|
|
@ -15,6 +15,8 @@ for (const file of commandFiles) {
|
||||||
client.commands.set(command.name, command);
|
client.commands.set(command.name, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//===Initalization===
|
//===Initalization===
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
console.log(`Logged in as ${client.user.tag}!`);
|
console.log(`Logged in as ${client.user.tag}!`);
|
||||||
|
|
@ -37,12 +39,18 @@ function executeCommand(msg: any) {
|
||||||
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);
|
||||||
|
|
||||||
if (!client.commands.has(command)) return;
|
if (!client.commands.has(command)) {
|
||||||
|
msg.reply(`That command: "${command}" doesn't exist. Sad.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
client.commands.get(command).execute(msg, args);
|
client.commands.get(command).execute(msg, args);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
if (e.code === 'ENOENT')
|
||||||
msg.reply(`Failed to execute the given command for some reason. Sad.`);
|
msg.reply(`Please create a squirrel using "!create" first, before trying that command.`);
|
||||||
|
else {
|
||||||
|
console.error(e);
|
||||||
|
msg.reply(`Failed to execute the given command for some reason. Sad.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
import { squirrel } from "../scripts/enums";
|
|
||||||
import { fs } from "../main";
|
import { fs } from "../main";
|
||||||
|
import { squirrel } from "../scripts/enums";
|
||||||
|
|
||||||
const races: Array<string> = ["tree", "ground", "chipmunk"];
|
const races: Array<string> = ["tree", "ground", "chipmunk"];
|
||||||
const classes: Array<string> = ["rogue", "berserker", "knight", "ranger", "priest"];
|
const classes: Array<string> = ["rogue", "berserker", "knight", "ranger", "priest"];
|
||||||
const lastNames: Array<string> = ["Nutcrack", "Nutmeg", "Seedsower", "McScuiri", "Rodentia", "Arbora", "Patagi"];
|
const lastNames: Array<string> = ["Nutcrack", "Nutmeg", "Seedsower", "McScuiri", "Rodentia", "Arbora", "Patagi"];
|
||||||
// const COLOR: string = "#E81B47";
|
export const COLOR: string = "#E81B47";
|
||||||
|
|
||||||
|
export function getPlayerData(playerId: string): squirrel {
|
||||||
|
return JSON.parse(fs.readFileSync(`./data/playerdata/${playerId}.json`))
|
||||||
|
}
|
||||||
|
|
||||||
export function newSquirrel(name: string, race: string, classtype: string): squirrel {
|
export function newSquirrel(name: string, race: string, classtype: string): squirrel {
|
||||||
let raceNum: number = -1;
|
let raceNum: number = -1;
|
||||||
|
|
@ -44,14 +48,24 @@ export 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// export function raceText(race: number): string {
|
export function raceText(raceIndex: number): string {
|
||||||
// if (race === 2) return capitalize(races[race]);
|
if (raceIndex >= races.length) {
|
||||||
// else if (race === 0 || race === 1) return `${capitalize(races[race])} Squirrel`;
|
console.log(`Error ${raceIndex} is outside of bounds`);
|
||||||
// else {
|
return "";
|
||||||
// console.log(`Error ${race} is not a valid race`);
|
}
|
||||||
// return "";
|
|
||||||
// }
|
if (raceIndex === 2) return capitalize(races[raceIndex]);
|
||||||
// }
|
else if (raceIndex === 0 || raceIndex === 1) return `${capitalize(races[raceIndex])} Squirrel`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function classText(classIndex: number): string {
|
||||||
|
if (classIndex >= classes.length) {
|
||||||
|
console.log(`Error ${classIndex} is outside of bounds`);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return capitalize(classes[classIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
export function capitalize(toCaps: string): string {
|
export function capitalize(toCaps: string): string {
|
||||||
return toCaps.charAt(0).toUpperCase() + toCaps.slice(1).toLowerCase();
|
return toCaps.charAt(0).toUpperCase() + toCaps.slice(1).toLowerCase();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue