-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
305 lines (278 loc) · 10.1 KB
/
bot.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
const needle = require('needle');
const Discord = require('discord.js');
const bot = new Discord.Client({intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.DIRECT_MESSAGES]});
const config = require('./bits/config.json');
const wikis = require('./bits/wikis.json');
const aliases = require('./bits/aliases.json');
const commands = require('./bits/commands.js');
const db = require('better-sqlite3')('./bits/db.sqlite');
const pj = require('./package.json');
needle.defaults({
user_agent: `GloopyBot/${pj.version}`
});
bot.once('ready', () => {
db.prepare('CREATE TABLE IF NOT EXISTS guilds (id TEXT, mainWiki TEXT)').run();
db.prepare('CREATE TABLE IF NOT EXISTS overrides (guildID TEXT, channelID TEXT, wiki TEXT)').run();
db.prepare('CREATE TABLE IF NOT EXISTS userOverride (userid TEXT, wiki TEXT)').run();
bot.guilds.cache.forEach(guild => {
const gRow = db.prepare('SELECT * FROM guilds WHERE id=?').get(guild.id);
if (!gRow) {
db.prepare('INSERT INTO guilds(id) VALUES (?)').run(guild.id);
}
});
bot.user.setActivity(`with gloop | ${config.prefix}help`);
console.log(`Ready: serving ${bot.guilds.cache.size} guilds.`);
});
bot.on('error', error => {
console.error('Websocket error! ' + error);
});
bot.on('guildCreate', guild => {
try {
const gRow = db.prepare('SELECT * FROM guilds WHERE id=?').get(guild.id);
if (!gRow) {
db.prepare('INSERT INTO guilds(id) VALUES (?)').run(guild.id);
}
} catch(e) {
db.prepare('CREATE TABLE IF NOT EXISTS guilds (id TEXT, mainWiki TEXT)').run();
db.prepare('INSERT INTO guilds(id) VALUES (?)').run(guild.id);
}
});
bot.on('messageCreate', msg => {
if (msg.author.bot) return;
if (msg.channel.type == 'GROUP_DM' || msg.channel.type == 'GUILD_CATEGORY') return;
if (msg.channel.type == 'DM') {
try {
const dRow = db.prepare('SELECT * FROM dms WHERE id=?').get(msg.channel.id);
if (!dRow) {
db.prepare('INSERT INTO dms(id) VALUES (?)').run(msg.channel.id);
}
} catch(e) {
db.prepare('CREATE TABLE IF NOT EXISTS dms (id TEXT, wiki TEXT)').run();
db.prepare('INSERT INTO dms(id) VALUES (?)').run(guild.id);
}
init(msg, true);
} else {
init(msg, false);
}
});
const init = (msg, isDM) => {
if (msg.cleanContent.startsWith(config.prefix)) {
let cmd = msg.cleanContent.split(' ')[0].substring(config.prefix.length);
let args = msg.cleanContent.split(' ').slice(1);
if (commands.commands.hasOwnProperty(cmd)) {
if (commands.commands[cmd].level !== 0) {
if (commands.commands[cmd].level === 1) {
if (!authorIsAnyAdmin(msg, isDM)) {
msg.reply('You have to be a server administrator to do this.');
} else {
commands.commands[cmd].process(bot, msg, args, isDM, db);
}
} else {
if (!authorIsBotCreator(msg, isDM)) {
msg.reply('Only the creator of the bot can do this.');
} else {
commands.commands[cmd].process(bot, msg, args, isDM, db);
}
}
} else {
commands.commands[cmd].process(bot, msg, args, isDM, db);
}
} else {
parseLinks(msg, isDM);
}
} else {
parseLinks(msg, isDM);
}
}
const authorIsServerAdmin = (msg, isDM) => {
if (isDM) return true;
if (msg.guild.id == config.testServer) return true;
return msg.member.permissions.has(Discord.Permissions.FLAGS.ADMINISTRATOR);
};
const authorIsBotCreator = (msg, isDM) => {
if (isDM) return config.adminID == msg.channel.recipient.id;
return config.adminID == msg.author.id;
};
const authorIsAnyAdmin = (msg, isDM) => {
return authorIsServerAdmin(msg, isDM) || authorIsBotCreator(msg, isDM);
};
const parseLinks = (msg, isDM) => {
let objArr = [];
const removeCodeBlocks = msg.cleanContent.replace(/`{3}[\S\s]*?`{3}/gm, '');
const removeInlineCode = removeCodeBlocks.replace(/`[\S\s]*?`/gm, '');
const removeLinks = removeInlineCode.replace(/(https?:\/\/[\w./#?&_-]*)/gm, '');
if (/\[\[(.*?)(?:\|.*?)?\]\]/g.test(removeLinks)) {
let queries = removeLinks.match(/\[\[(.*?)(?:\|.*?)?\]\]/g);
for (let i = 0; i < queries.length; i++) {
let query = queries[i].replace(/\[\[(.*?)(?:\|.*?)?\]\]/g, '$1');
let wiki = parseWikiFromQuery(query);
if (wiki !== 'default') query = query.replace(/^.*?:/g, '');
objArr.push({ 'type': 'search', 'query': query, 'wiki': wiki, 'id': isDM ? msg.channel.id : msg.channel.id + '@' + msg.guild.id, 'user': msg.author.id });
}
}
if (/{{(.*?)(?:\|.*?)?}}/g.test(removeLinks)) {
let queries = removeLinks.match(/{{(.*?)(?:\|.*?)?}}/g);
for (let i = 0; i < queries.length; i++) {
let query = queries[i].replace(/{{(.*?)(?:\|.*?)?}}/g, '$1');
let wiki = parseWikiFromQuery(query);
if (wiki !== 'default') query = query.replace(/^.*?:/g, '');
objArr.push({ 'type': 'template', 'query': query, 'wiki': wiki, 'id': isDM ? msg.channel.id : msg.channel.id + '@' + msg.guild.id, 'user': msg.author.id });
}
}
if (/--(.*?)(?:\|.*?)?--/g.test(removeLinks)) {
let queries = removeLinks.match(/--(.*?)(?:\|.*?)?--/g);
for (let i = 0; i < queries.length; i++) {
let query = queries[i].replace(/--(.*?)(?:\|.*?)?--/g, '$1');
let wiki = parseWikiFromQuery(query);
if (wiki !== 'default') query = query.replace(/^.*?:/g, '');
objArr.push({ 'type': 'raw', 'query': query, 'wiki': wiki, 'id': isDM ? msg.channel.id : msg.channel.id + '@' + msg.guild.id, 'user': msg.author.id });
}
}
if (objArr.length > 0) {
buildMessage(objArr, isDM).then(replString => {
msg.channel.send(replString);
}).catch(err => {
if (err === 'NVL') {
msg.channel.send('**No search results found for the attempted link(s).**\nTry using dashes instead to force-create a URL.');
} else if (err === 'NDW') {
msg.reply(`This server has no default wiki set. Please set one ${!isDM ? 'or have a server administrator set one ' : ''}using \`${config.prefix}wiki\`.`);
} else if (err === 'ERO') {
()=>{}; //noop - if it's only empty rawlinks just ignore the entire message altogether
} else {
console.error(err);
}
});
}
};
const parseWikiFromQuery = query => {
if (/^.*?:/g.test(query)) {
let potWiki = query.match(/^.*?:/g)[0].replace(':', '');
if (wikis.hasOwnProperty(potWiki)) {
return potWiki;
} else {
if (aliases.hasOwnProperty(potWiki) && !aliases[potWiki].setOnly) {
return aliases[potWiki].wiki;
} else {
return 'default';
}
}
} else {
return 'default';
}
};
const buildMessage = (objectArray, isDM) => {
return new Promise((resolve, reject) => {
let promiseArray = [];
for (let i = 0; i < objectArray.length; i++) {
promiseArray.push(requestLink(objectArray[i].query, objectArray[i].wiki, objectArray[i].type, objectArray[i].id, isDM, objectArray[i].user));
}
Promise.all(promiseArray).then(objects => {
let emptyRawsOnly = true;
let replyStringBegin = '**Links detected:**';
let replyString = '';
for (let j = 0; j < objects.length; j++) {
if (objects[j] === 404) continue;
if (objects[j][0][0] !== '' && objects[j][1][0] !== '') emptyRawsOnly = false;
else continue;
replyString += '\n<' + fixDiscordLink(objects[j][3][0]) + '>';
}
if (emptyRawsOnly) return reject('ERO');
if (replyString.length > 0) return resolve(replyStringBegin + replyString);
else return reject('NVL');
}).catch(err => {
if (err === 'NDW') {
return reject('NDW');
}
});
});
};
const getWiki = (objWiki, changuildID, isDM, user) => {
return new Promise((resolve, reject) => {
if (objWiki === 'default') {
if (isDM) {
const dRow = db.prepare('SELECT * FROM dms WHERE id=?').get(changuildID);
if (!dRow || !dRow.wiki) {
return reject('NDW');
}
return resolve(wikis[dRow.wiki].url);
} else {
const uRow = db.prepare('SELECT * FROM userOverride WHERE userID=?').get(user);
if (uRow && uRow.wiki) {
return resolve(wikis[uRow.wiki].url);
} else {
const gRow = db.prepare('SELECT * FROM guilds WHERE id=?').get(changuildID.split('@')[1]);
if (!gRow || !gRow.mainWiki) {
return reject('NDW');
}
const oRow = db.prepare('SELECT * FROM overrides WHERE guildID=? AND channelID=?').get(gRow.id, changuildID.split('@')[0]);
if (!oRow) {
return resolve(wikis[gRow.mainWiki].url);
} else {
return resolve(wikis[oRow.wiki].url);
}
}
}
} else {
return resolve(wikis[objWiki].url);
}
});
};
const requestLink = (query, wiki, type, changuildID, isDM, user) => {
return new Promise((resolve, reject) => {
getWiki(wiki, changuildID, isDM, user).then(wurl => {
if (type === 'template') {
query = 'Template:' + query;
}
if (type !== 'raw') {
if (wurl.includes('wikipedia')) wurl += '/w';
let url = `${wurl}/api.php?action=opensearch&search=${wikiUrlEncode(query)}&redirects=resolve`;
needle('get', url).then(response => {
if (response.body[1].length === 0) {
return resolve(404);
}
return resolve(response.body);
}).catch(err => {
return reject(err);
});
} else {
let url = `${wurl}/w/${query}`;
if (wurl.includes('wikipedia')) url = `${wurl}/wiki/${query}`;
return resolve([ query, [ query ], [ '' ], [ url ] ]);
}
}).catch(err => {
if (err === 'NDW') {
return reject('NDW');
}
});
});
};
const fixDiscordLink = (url) => url.replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/%23/g, '#').replace(/ /g, '_');
const wikiUrlEncode = (url) => encodeURIComponent(url)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A')
.replace(/~/g, '%7E')
.replace(/%20/g, '_')
.replace(/%3A/g, ':')
.replace(/%2F/g, '/')
.replace(/\+/g, '%2B')
.replace(/\@/g, '%40');
//String.prototype.padStart polyfill
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
padString = String(padString || ' ');
if (this.length > targetLength) {
return String(this);
} else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
}
bot.login(config.token);