-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
139 lines (109 loc) · 3.83 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(function () {
"use strict";
const _ = require("lodash");
const config = require("./config.json");
const Discord = require("discord.js");
let formals = require("./formal.json");
const client = new Discord.Client(),
prefix = "!formal";
const spellList = _.map(formals, function (o) {
return "_" + _.map(o.name.split(" "), _.capitalize).join(" ").replace(",", ":") + "_";
}).sort().join(", ");
formals = _.sortBy(formals, "name");
/**
* formatComponents
* @param components
* @returns {string}
*/
let formatComponents = function (components) {
return _.reduce(components, function (arr, value, key) {
arr.push(key.toUpperCase() + value);
return arr;
}, []).join(", ");
};
/**
* formatSchools
* @param schools
* @returns {string}
*/
let formatSchools = function (schools) {
return _.reduce(schools, function (arr, school) {
if (school === "scroll-specific") {
arr.push("_(Scroll-Specific)_");
} else {
arr.push(_.capitalize(school));
}
return arr;
}, []).join(", ");
};
/**
* formatResponse
* @param spellDetails
* @returns {string}
*/
let formatResponse = function (spellDetails) {
let response;
response = `\n__**${spellDetails.name}**__`;
response = `${response}\n**School**: ${formatSchools(spellDetails.schools)}`;
response = `${response}\n**Level**: ${spellDetails.level}`;
response = `${response}\n**Components**: _${formatComponents(spellDetails.components)}_`;
if (spellDetails.notes) {
response = `${response}\n${spellDetails.notes}`;
}
if (spellDetails.description) {
response = `${response}\n**Description**: ${spellDetails.description}`;
}
if (!spellDetails.cost) {
spellDetails.cost = getGoldCost(spellDetails.components);
}
response = `${response}\n*Est. Cost: ${spellDetails.cost} gold.*`;
return response;
};
let getGoldCost = function (components) {
return _.reduce(components, function (cost, num, type) {
if (type.toUpperCase() === "P") {
cost += 3 * num;
} else {
cost += 2 * num;
}
return cost;
}, 0);
};
/**
* handleMessage
* @param msg
*/
let handleMessage = function (msg) {
try {
let args,
results;
if (!msg.content.startsWith(prefix) || msg.author.bot) {
return;
}
args = msg.content.slice(prefix.length).trim();
console.log(`Fetching ${args}`);
if (args === "list") {
msg.reply(spellList);
return;
}
results = _.filter(formals, function (o) {
return o.name.toLowerCase().indexOf(args.toLowerCase()) !== -1;
//return o.name.toLowerCase() === args.toLowerCase();
});
if (results.length > 0) {
msg.reply(formatResponse(results[0]));
} else {
msg.reply(`Could NOT find formal named \`${args}\`.`);
}
} catch (e) {
console.log("**ERROR OCCURRED IN MESSAGE HANDLER!**");
console.log(e);
msg.reply(`CALL AN AMBULANCE! I REQUIRE MEDICAL ATTENTION!\n${JSON.stringify(e)}`);
}
};
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", handleMessage);
client.login(config.token);
})();