-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (80 loc) · 3.64 KB
/
index.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
const Discord = require('discord.js');
const Environment = require('./src/context/Environment');
const Command = require('./src/commands/Command');
const BotRequest = require('./src/commands/BotRequest');
const Parser = require('./src/utils/Parser');
const client = new Discord.Client();
const environments = {};
const COMMAND_PREFIX = process.env.COMMAND_PREFIX;
client.on('ready', () => {
console.log('Bot online.');
});
client.on('message', message => {
if (message.content.startsWith(COMMAND_PREFIX)) {
const id = message.channel.guild.id.toString();
let environment = environments[id];
if (!environment) {
environment = new Environment(initialiseBaseCommands(), {});
environments[id] = environment;
console.log('Initialised environment for server: ' + message.channel.guild.name);
}
processCommand(environment, message);
}
});
function initialiseBaseCommands() {
return {
'hello': new Command('hello', function(localClient, request, environment) {
request.message.channel.send('Hello, world!');
}, 'Prints \'Hello, world!\'. It\'s that simple'),
'commands': new Command('commands', function(localClient, request, environment) {
let response = '';
const commands = environment.commands;
for (const name in commands) {
const command = commands[name];
response += ' - `' + COMMAND_PREFIX + command.name + '`';
if (command.description) {
response += ':\n\t' + command.description;
}
response += '\n';
}
request.message.channel.send(response);
}, 'Prints a list of the currently registered (base and custom) commands along with a description, if any'),
'register': new Command('register', function(_client, request, environment) {
registerFunctionFromInlineSource(request.message.channel, request.arguments, request.terms, environment);
}, `
Registers the given JavaScript function source code as a command. Parameter 1 is the desired command name, the rest of the message should be
the function source, not surrounded in quotes.
Example: \`!register example function(client, request, environment) { request.message.channel.send("Example"); }\`
`),
};
}
function registerFunctionFromInlineSource(channel, messageContent, terms, environment) {
const newFunctionName = terms[1];
const newFunctionSource = messageContent.substring(messageContent.indexOf(terms[2]), messageContent.length);
try {
registerFunction(Parser.parseSource(newFunctionName, newFunctionSource), environment);
channel.send('Added command: ' + newFunctionName);
} catch (error) {
handleError(error, newFunctionSource, channel);
}
}
function registerFunction(command, environment) {
environment.commands[command.name] = command;
environment.context[command.name] = {};
}
function processCommand(environment, message) {
const request = new BotRequest(COMMAND_PREFIX, message);
const command = environment.commands[request.commandName];
if (command) {
try {
command.action(client, request, environment);
} catch (error) {
handleError(error, command.action, message.channel);
}
}
}
function handleError(e, backendMessage, channel) {
channel.send(`Error: ${e.message}`);
console.log(`Error (${e.message}): ${backendMessage}`);
}
client.login(process.env.BOT_TOKEN);