This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
TheAwesomeBot.js
159 lines (134 loc) · 4.55 KB
/
TheAwesomeBot.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
/* eslint-disable class-methods-use-this */
const path = require('path');
const Discord = require('discord.js');
const Settings = require(path.join(__dirname, 'settings.json')); // eslint-disable-line import/no-dynamic-require
let Tokens;
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
Tokens = require(path.join(__dirname, 'tokens.json'));
} catch (e) {
Tokens = {};
}
class TheAwesomeBot {
constructor(token, discordOpt) {
this.bootTime = new Date();
this.token = token;
this.client = new Discord.Client(discordOpt || { autoReconnect: true });
this.settings = Settings;
this.settings.tokens = Tokens; // insert tokens into our settings obj
this.commands = {};
this.usageList = '';
// store the RE as they're expensive to create
this.cmd_re = new RegExp(`^${this.settings.bot_cmd}\\s+([^\\s]+)\\s*([^]*)\\s*`, 'i');
// flags if connected and client is ready
this.isReady = false;
}
onMessage() {
return (message) => {
// don't respond to own messages
if (this.client.user.username === message.author.username) {
return;
}
// check if message is a command
const cmdMatch = message.cleanContent.match(this.cmd_re);
// not a known command
if (!cmdMatch || Object.keys(this.commands).indexOf(cmdMatch[1]) === -1) {
if (message.content.match(new RegExp(`^${this.settings.bot_cmd}[\\s]*( .*)?$`, 'i'))) {
let helpText = 'maybe try these valid commands? *kthnxbye!*\n\n```';
helpText += this.usageList;
helpText += '```';
message.channel.sendMessage(helpText);
}
return;
}
// process commands
const cmd = cmdMatch[1];
const cmdArgs = cmdMatch[2].trim();
let showUsage;
try {
showUsage = this.commands[cmd].run(this, message, cmdArgs);
} catch (err) {
message.channel.sendMessage('There was an error running the command:\n' +
'```\n' + err.toString() + '\n```');
console.error(err);
console.error(err.stack);
}
if (showUsage === true) {
let usage = this.commands[cmd].usage;
if (typeof usage !== 'string') {
usage = usage.join('\n');
}
message.channel.sendMessage('```\n' + usage + '\n```');
}
};
}
onReady() {
return (() => {
console.log('\nConnected to discord server!');
console.log('Running initializations...');
Object.keys(this.commands).filter(cmd =>
typeof this.commands[cmd].init === 'function')
.forEach(cmd => this.commands[cmd].init(this));
this.isReady = true;
});
}
serverNewMember() {
return ((server, user) => this.client.sendMessage(user, this.usageList));
}
onDisconnected() {
return () =>
console.warn('Bot has been disconnected from server...');
}
onError() {
return ((err) => {
console.error('error: ', err);
console.error(err.trace);
});
}
loadCommands(cmdList) {
this.usageList = '';
cmdList.forEach((cmd) => {
const fullpath = path.join(__dirname, 'commands', cmd, `${cmd}.js`);
const script = require(fullpath); // eslint-disable-line global-require, import/no-dynamic-require
this.commands[cmd] = script;
const usageObj = script.usage;
if (usageObj) {
const usageStrs = [];
if (Array.isArray(usageObj)) {
usageObj.forEach(u => usageStrs.push(u));
} else {
usageStrs.push(usageObj.toString());
}
this.usageList += usageStrs.reduce((list, str) => list + `\n- ${this.settings.bot_cmd} ${str}`, '');
}
});
}
init() {
// load commands
console.log('Loading commands...');
this.loadCommands(this.settings.commands);
// setup events
console.log('Setting up event bindings...');
this.client
.on('ready', this.onReady())
.on('serverNewMember', this.serverNewMember())
.on('message', this.onMessage())
.on('error', this.onError());
console.log('Connecting...');
// return the promise from "login()"
return this.client.login(this.token);
}
deinit() {
// disconnect gracefully
this.isReady = false;
// return the promise from "destroy()"
return this.client.destroy();
}
isAdminOrMod(member) {
const immuneRoles = new Set(this.settings.voting.immuneRoles);
const userRoles = new Set(member.roles.array().map(r => r.name));
const setIntersection = [...userRoles].filter(r => immuneRoles.has(r));
return setIntersection.length > 0;
}
}
module.exports = TheAwesomeBot;