-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathelrond.js
246 lines (231 loc) · 5.72 KB
/
elrond.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const fetch = require('node-fetch');
const Discord = require('discord.js');
const Winston = require('winston');
const auth = require('./auth.json');
const getCommandList = require('./commands');
const logger = Winston.createLogger({
level: 'debug',
format: Winston.format.json(),
transports: [new Winston.transports.Console()],
});
async function getCardIndex() {
logger.info('Retrieving player cards');
try {
return fetch('http://ringsdb.com/api/public/cards/?_format=json').then(res => res.json());
} catch (err) {
logger.error(err);
return Promise.reject(err);
}
}
/**
* QC format =
* {
* quests: {
* cycle: {
* @attributes: {
* name
* },
* url,
* hoburl
* }
* }
* }
*
* This function extracts the name, QC url and hall of beorn url.
*/
async function getQCData() {
logger.info('Retrieving data from QC');
try {
return fetch('http://lotr-lcg-quest-companion.gamersdungeon.net/api.php?format=json&parse=discord').then(
res => res.json()
);
} catch (err) {
logger.error(err);
return Promise.reject(err);
}
}
function getNameAndFilters(args) {
return args.reduce(
(acc, arg) => {
if (arg.indexOf(':') > -1) {
const [filterKey, value] = arg.split(':');
return {
...acc,
filters: [...acc.filters, { filterKey, value }],
};
}
const name = arg
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.trim();
return {
...acc,
name: `${acc.name} ${name}`.trim(),
};
},
{
name: '',
filters: [],
}
);
}
function parseQCData(qcData) {
const {
quests: { cycle },
faq: { entry: faqEntries },
glossary: { entry: glossaryEntries },
carderratas: { card: cardErratas },
} = qcData;
const scenarios = cycle.reduce((acc, { quest }) => {
if (quest) {
return [
...acc,
...(Array.isArray(quest) ? quest.map(({ '@attributes': { name }, url, hoburl }) => ({
name,
url,
hoburl,
})) : [{
name: quest['@attributes'].name,
url: quest.url,
hoburl: quest.hoburl
}])
];
}
return acc;
}, []);
const faq = faqEntries.map(({ '@attributes': { title, id }, ruletext }) => ({
title,
id,
ruletext,
}));
const glossary = glossaryEntries.map(({ '@attributes': { title, id }, ruletext }) => ({
title,
id,
ruletext,
}));
const erratas = cardErratas.map(({ '@attributes': { title, id }, ruling, qa, errata }) => ({
title,
id,
ruling,
qa,
errata
}));
return {
scenarios,
faq,
glossary,
erratas,
};
}
// Initialize Discord Bot
Promise.all([getCardIndex(), getQCData()])
.then(([cardList, qcData]) => {
return [cardList, parseQCData(qcData)];
})
.then(([cardList, { scenarios, ...rulesRef }]) => {
const bot = new Discord.Client();
const emojiNames = [
'lore',
'spirit',
'leadership',
'tactics',
'neutral',
'fellowship',
'attack',
'defense',
'willpower',
'threat',
'hitpoints',
'attackblack',
'defenseblack',
'willpowerblack',
'threatblack',
'hitpointsblack'
];
let emojiSymbols;
bot.once('ready', evt => {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.user.username + ' - (' + bot.user.tag + ')');
emojiSymbols = bot.emojis.cache.reduce((acc, emoji) => {
if (emoji.guild.name === "COTR" && emojiNames.indexOf(emoji.name) > -1) {
return {
...acc,
[emoji.name]: emoji
}
}
return acc;
}, {});
});
bot.on('message', ({ author, content, channel }) => {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (content.startsWith('!')) {
let args = content.substring(1).split(' ');
const cmd = args[0];
args = args.splice(1);
const query = getNameAndFilters(args);
const commandConfig = {
author,
cardList,
scenarios,
rulesRef,
emojiSymbols,
bot,
channel,
logger,
};
const commands = getCommandList(commandConfig);
switch (cmd) {
case 'help':
return commands.help();
case 'rings':
return commands.rings(query);
case 'ringsimg':
return commands.ringsimg(query);
case 'quest':
return commands.quest();
case 'hero':
return commands.hero(query);
case 'card':
return commands.card(query);
case 'faq':
return commands.rr({
...query,
type: 'faq',
});
case 'glossary':
return commands.rr({
...query,
type: 'glossary',
});
case 'errata':
return commands.rr({
...query,
type: 'errata',
});
case 'myrings':
return commands.myrings();
default:
return null;
}
}
});
bot.on('error', e => console.error(e));
bot.on('warn', e => console.warn(e));
bot.on('debug', e => console.debug(e));
bot.login(auth.token);
})
.catch(err => {
logger.error(`Error getting indexes: ${err}`);
logger.error(err.stack);
});
process.on('SIGTERM', () => {
logger.info('SIGTERM received');
});
process.on('uncaughtException', err => {
logger.error(`Uncaught exception: ${err}`);
logger.error(err.stack);
process.exit(1);
});