-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloobot.js
390 lines (343 loc) · 15.5 KB
/
bloobot.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const Discord = require('discord.js');
const client = new Discord.Client();
const transactions = require('./modules/transactions');
const util = require('./util');
const cmd = require('./commands');
const scv = require('./modules/scv');
const defaults = require('./modules/defaults');
const config = require('./config.json');
const cmdData = require('./data/commands.json');
// If the command line argument 'test' is given, log in to the test account
const test = process.argv[2] === 'test',
loginToken = test ? config.test_token : config.token;
// Global variables
let variablesLoaded = {},
allPrefixes = {},
allCustomAliases = {};
/**
* A function to deal with retrieving channel variables. If the variable is undefined, this function returns its
* default value.
*
* @param channelID The current channel ID.
* @param variable The name of the channel variable to be fetched.
* @returns {Promise} A promise that resolves with the value to be assigned to this variable in the current context.
*/
const getVariableWithFallback = async (channelID, variable) => {
let val = await scv.get(channelID, variable);
if (val) {
return val;
} else {
return defaults.get(variable);
}
};
/**
* Updates all channel variables.
* TODO: fix this so it doesn't just update prefix but can iterate over a list of variables to update. Maybe use Promise.all?
*
* @param channelID The current channel ID.
* @returns {Promise} A promise that resolves when the variables have been updated.
*/
const updateVariables = async (channelID) => {
let val = await getVariableWithFallback(channelID, 'prefix');
allPrefixes[channelID + ''] = val;
};
/**
* Checks if the string is an alias for some command. This checks the default aliases in commands.json, and the custom
* channel aliases from SCV.
* TODO: make this return a special string in case of no applicable command being found, for consistency
*
* @param channelID The current channel ID.
* @param keyword The string to be cross-checked.
* @returns {String} The relevant command name if the string is an alias for some command, or null if not.
*/
const checkForAlias = async (channelID, keyword) => {
const cmdNames = Object.keys(cmdData);
let command = null;
// first, check the default aliases
for (let i in cmdNames) {
let cmdName = cmdNames[i],
cmdObj = cmdData[cmdName],
aliases = cmdObj.aliases;
if (Array.isArray(aliases)) {
if (aliases.indexOf(keyword) !== -1) {
aliasFound = true;
command = cmdName;
}
}
}
if (!command) {
// haven't found any matching default aliases, so now check custom aliases
let customAliasCommand = await transactions.commandForAlias(channelID, keyword);
console.log(customAliasCommand);
if (customAliasCommand) {
// a command was found for this alias
command = customAliasCommand.command;
}
}
return command;
};
client.once('ready', async () => {
console.log('Bot is online!');
console.log(`Ready: serving ${client.guilds.size} guilds, in ${client.channels.size} channels, for ${client.users.size} users.`);
await transactions.sync();
});
/*
* This triggers on every message. Use this to listen to commands and master commands.
*/
client.on('message', async msg => {
const channelID = msg.channel.id;
//scv.listTable();
// scv.get(channelID, 'aliases').then(val => {
// //console.log(val);
// });
// Update all variables if this is the first command for this channel since starting up
if (!variablesLoaded[channelID]) {
await updateVariables(channelID);
variablesLoaded[channelID] = true;
}
// once we've updated our variables (if we need to), try to parse a command
// TODO: find some way to update this after setprefix
let prefix = allPrefixes[channelID + ''];
if (typeof prefix === 'undefined') {
let p = defaults.get('prefix');
allPrefixes[channelID + ''] = p;
prefix = p;
}
switch (msg.content) {
// Master commands. These commands do not depend on the prefix
case 'bloobotprefix':
// Master command that lists the prefix. This command must be independent of
// the current prefix and therefore cannot be handled by regular command logic.
msg.channel.send(`**Command prefix currently in use**: ${prefix}`);
break;
case 'resetprefix':
// Master command that resets the prefix to ~.
// admins only (and me)
if (util.sentByAdminOrMe(msg)) {
const sendingFunction = (text) => msg.channel.send.call(msg.channel, text);
await cmd.setPrefix(client, msg, sendingFunction, '~');
await updateVariables(channelID);
// done
// TODO: maybe change this so that execution is paused until promise is complete
}
break;
default:
if (msg.author === client.user) {
// make sure the bot doesn't respond to its own messages
} else if (!msg.content.startsWith(prefix)) {
// message doesn't start with the prefix so do nothing
} else {
console.log(msg.content); // for debugging and just making sure it works
let parsedCmd = await cmdParse(msg, prefix), // parse out the command and args
output;
if (parsedCmd) {
try {
let out = await cmdExe(msg, parsedCmd.cmdName, parsedCmd.cmdArgs, prefix);
output = out;
console.log(parsedCmd); // same as above
// if we need to output something that was returned from the command, then do so
if (output.length !== 0) {
// send the message
if (!util.safeSendMsg(msg.channel, output.join('\n'), '```')) {
msg.channel.send(`Outbound message length greater than ${util.MY_CHAR_LIMIT} character limit.`);
}
}
} catch(err) {
// an error was thrown by cmdExe (most likely a permissions error)
console.log(err);
console.log(err.stack);
util.sendErrorMessage(msg.channel, err);
};
} else {
// do nothing? idk
}
}
break;
}
});
// Create an event listener for new guild members
client.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
const channel = member.guild.channels.find('name', 'member-log');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send(`Welcome to the server, ${member}`);
});
client.login(loginToken);
/**
* Executes a command using the function reference found in the command documentation file. This function is supposed to
* be called after cmdParse, and take the command name and command args from its output.
*
* @async
* @param msg The message that requested the command to be executed.
* @param cmdName The name of the command specified.
* @param args The arguments given to the command.
* @param prefix The command prefix currently in use.
* @returns {Promise} A promise that resolves with an array of the text to be messaged inside ``, if any, line by line.
*/
async function cmdExe(msg, cmdName, args, prefix) {
const currCmd = cmdData[cmdName],
// note: paramsCount DOES include default parameters!!!
paramsCount = typeof currCmd.params === 'undefined' ? 0 : Object.keys(currCmd.params).length,
defaultsCount = typeof currCmd.defaults === 'undefined' ? 0 : currCmd.defaults,
update = typeof currCmd.update === 'undefined' ? false : currCmd.update,
argsCount = args.length,
channelID = msg.channel.id;
let outText = [];
console.log(args);
if (args.length === 0 && paramsCount !== 0) {
return await cmd.descString(channelID, cmdName);
} else {
// Check for a permissions error, and if positive reject the promise:
if (currCmd.permissions === 'admin' && !util.sentByAdminOrMe(msg)) { // check for privileges if the command requires them
throw new Error(`The command "${cmdName}" requires administrator privileges.`);
} else if (currCmd.permissions === 'me' && !util.sentByMe(msg)) { // check for privileges if the command requires them
throw new Error(`The command "${cmdName}" can only be run by the bot admin.`);
} else if (args.length < paramsCount - defaultsCount) { // check if the number of args is correct
throw new Error(`The command "${cmdName}" requires at least ${paramsCount - defaultsCount} arguments; received ${args.length}.`);
} else {
const func = cmd[currCmd.fn],
fnParams = currCmd.params,
fullArgs = args.slice(0),
sendingFunction = (text) => msg.channel.send.call(msg.channel, text);
// for some reason this is necessary, instead of just msg.channel.send :(
// type checking:
let typeMismatch = false,
typeMismatchDesc = {}; // initialising so WebStorm doesn't complain
if (typeof fnParams !== 'undefined') {
let i = 0, // TODO: this seems to work, but just in case, rewrite the API to not rely on key ordering
paramNames = Object.keys(fnParams);
// using every instead of forEach lets me break out of the loop when a mismatch is detected
paramNames.every(key => {
if (i === argsCount) {
// If a default argument has not been provided, we don't need to util.TypeCheck. That means,
// we need to break out of the loop as soon as we have processed all provided arguments.
return false;
}
const value = fnParams[key].type,
typesArray = Array.isArray(value) ? value : [value],
argument = fullArgs[i++],
t = typesArray.filter(elem => util.TypeCheck[elem](argument));
// There is a type mismatch if and only if the argument input does not match any of the
// types specified for it. In this case, the filter above will trim all elements from the
// array, and return [].
typeMismatch = t.length === 0;
if (typeMismatch) {
typeMismatchDesc = {
argument: argument,
parameter: key,
expected: typesArray
};
return false;
} else {
return true;
}
});
}
if (!typeMismatch) {
// input passed type checking
fullArgs.unshift(sendingFunction);
fullArgs.unshift(msg);
fullArgs.unshift(client);
// call the command function:
const moreText = func.apply(this, fullArgs);
// process result
if (typeof moreText === 'string') {
outText.push(moreText);
} else if (Array.isArray(moreText)) {
outText = outText.concat(moreText);
} else if (moreText instanceof Promise) {
let out = await moreText;
if (typeof out === 'string') {
outText.push(out);
} else if (Array.isArray(out)) {
outText = outText.concat(out);
}
}
} else {
// input failed type checking, handle the error
const p = typeMismatchDesc.parameter,
a = typeMismatchDesc.argument,
expected = typeMismatchDesc.expected,
t = Array.isArray(expected) ? expected.join(', ') : expected;
msg.channel.send(`Invalid input "${a}" for parameter **${p}** (${t} expected).`);
}
}
if (update) await updateVariables(channelID); // update variables if necessary
return outText;
}
// no errors thrown
// command execution successful
// update global variables if required, then return
if (currCmd.update) {
console.log('updating for ' + channelID);
await updateVariables(channelID);
}
return outText;
}
/**
* Parses a command from a message into the command name and a list of arguments.
*
* @param msg The message that requested the command to be executed.
* @param prefix The command prefix currently in use.
* @returns {*} An object containing the command name and command arguments, to be passed to cmdExe, or false if no such
* command exists.
*/
async function cmdParse(msg, prefix) {
const cmdString = msg.content,
cmdText = cmdString.slice(prefix.length), // take out the prefix
firstSpace = cmdText.indexOf(' ');
let commandName,
commandArgs;
if (firstSpace !== -1) {
commandName = cmdText.slice(0, firstSpace).toLowerCase(); // get the command name
if (typeof cmdData[commandName] === 'undefined') {
const aliasCommand = await checkForAlias(msg.channel.id, commandName);
if (aliasCommand) {
commandName = aliasCommand;
} else {
msg.channel.send('**Undefined command name** "' + commandName + '"');
return false;
}
}
// parse out the command args
commandArgs = cmdText.slice(firstSpace).match(/"(?:\\"|\\\\|[^"])*"|\S+/g)
.map((elem) => {
let out;
// check if it's in quotes
if (elem.charAt(0) === '"' && elem.charAt(elem.length - 1) === '"') {
out = elem.slice(1, elem.length - 1);
} else {
out = elem;
}
// convert to number if numerical
return isNaN(out) ? out : +out;
});
// if too many args have been received:
let paramsCount = Object.keys(cmdData[commandName].params).length;
if (commandArgs.length > paramsCount) {
// too many args received, so condense the remainder into one argument
const remainingArgs = commandArgs.slice(paramsCount - 1, commandArgs.length);
commandArgs = commandArgs.slice(0, paramsCount - 1);
commandArgs.push(remainingArgs.join(' '));
}
} else {
commandName = cmdText.toLowerCase();
commandArgs = [];
if (typeof cmdData[commandName] === 'undefined') {
const aliasCommand = await checkForAlias(msg.channel.id, commandName);
console.log(commandName, aliasCommand);
if (aliasCommand) {
commandName = aliasCommand;
} else {
msg.channel.send('**Undefined command name** "' + commandName + '"');
return false;
}
}
}
return {
'cmdName': commandName,
'cmdArgs': commandArgs
};
}