forked from greys-bots/hubbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhubbot.js
496 lines (442 loc) · 14.3 KB
/
hubbot.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
const Eris = require("eris-additions")(require("eris"));
const Discord = require("discord.js");
const dblite = require("dblite");
require('dotenv').config();
const bot = new Eris(process.env.TOKEN, {restMode: true});
bot.fetch = require('node-fetch');
bot.fs = require('fs');
bot.db = dblite('data.sqlite',"-header");
bot.chars = process.env.CHARS;
bot.prefix = process.env.PREFIX;
bot.log_channel = process.env.LOG_CHANNEL;
bot.customActions = [
{name: "member.hr", replace: "msg.member.hasRole"},
{name: "member.rr", replace: "await msg.member.removeRole"},
{name: "member.ar", replace: "await msg.member.addRole"},
{name: "member.bl", replace: "await bot.commands.blacklist.execute(bot, msg, [msg.member.id])"},
{name: "args.hr", replace: (arg) => "msg.guild.members.find(m => m.id == "+arg+").hasRole"},
{name: "args.rr", replace: (arg) => "await msg.guild.members.find(m => m.id == "+arg+").removeRole"},
{name: "args.ar", replace: (arg) => "await msg.guild.members.find(m => m.id == "+arg+").addRole"},
{name: "args.bl", replace: (arg) => "await bot.commands.blacklist.subcommands.add.execute(bot, msg, [msg.guild.members.find(m => m.id == "+arg+").id])"},
{name: "rf\\(('.*')\\)", replace: "msg.guild.roles.find(r => r.name.toLowerCase() == $1.toLowerCase()).id", regex: true}
]
bot.banVars = {
"$REASON": "${reason}",
"$SERVER.NAME": "${msg.guild.name}"
}
bot.logVars = {
"$SERVER.NAME": '${guild.name || "(no name)"}'
}
bot.AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
const recursivelyReadDirectory = function(dir) {
var results = [];
var files = bot.fs.readdirSync(dir, {withFileTypes: true});
for(file of files) {
if(file.isDirectory()) {
results = results.concat(recursivelyReadDirectory(dir+"/"+file.name));
} else {
results.push(dir+"/"+file.name);
}
}
return results;
}
const registerCommand = function({command, module, name} = {}) {
if(!command) return;
command.module = module;
command.name = name;
module.commands.set(name, command);
bot.commands.set(name, command);
bot.aliases.set(name, name);
if(command.alias) command.alias.forEach(a => bot.aliases.set(a, name));
if(command.subcommands) {
var subcommands = command.subcommands;
command.subcommands = new Discord.Collection();
Object.keys(subcommands).forEach(c => {
var cmd = subcommands[c];
cmd.name = `${command.name} ${c}`;
cmd.parent = command;
cmd.module = command.module;
if(!command.sub_aliases) command.sub_aliases = new Discord.Collection();
command.sub_aliases.set(c, c);
if(cmd.alias) cmd.alias.forEach(a => command.sub_aliases.set(a, c));
if(command.permissions && !cmd.permissions) cmd.permissions = command.permissions;
if(command.guildOnly != undefined && cmd.guildOnly == undefined)
cmd.guildOnly = command.guildOnly;
command.subcommands.set(c, cmd);
})
}
return command;
}
async function setup() {
//database schema
//FINALLY alphabetized
bot.db.query(`CREATE TABLE IF NOT EXISTS ban_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hid TEXT,
server_id TEXT,
channel_id TEXT,
message_id TEXT,
users TEXT,
reason TEXT,
timestamp TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS commands (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id BIGINT,
name TEXT,
actions TEXT,
target TEXT,
del INTEGER
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS configs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id BIGINT,
banlog_channel BIGINT,
ban_message TEXT,
reprole BIGINT,
delist_channel BIGINT,
starboard TEXT,
blacklist TEXT,
feedback TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS feedback (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hid TEXT,
server_id TEXT,
sender_id TEXT,
message TEXT,
anon INTEGER
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS listing_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hid TEXT,
server_id TEXT,
channel_id TEXT,
message_id TEXT,
server_name TEXT,
reason TEXT,
timestamp TEXT,
type INTEGER
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
host_id BIGINT,
server_id BIGINT,
channel_id BIGINT,
message_id BIGINT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS reactcategories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hid TEXT,
server_id BIGINT,
name TEXT,
description TEXT,
roles TEXT,
posts TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS reactroles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id BIGINT,
role_id BIGINT,
emoji TEXT,
description TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS reactposts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id TEXT,
channel_id TEXT,
message_id TEXT,
roles TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS receipts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hid TEXT,
server_id TEXT,
message TEXT,
link TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS servers(
id INTEGER PRIMARY KEY AUTOINCREMENT,
host_id BIGINT,
server_id BIGINT,
contact_id TEXT,
name TEXT,
description TEXT,
invite TEXT,
pic_url TEXT,
visibility INTEGER
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS starboards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id TEXT,
channel_id TEXT,
emoji TEXT,
override INTEGER,
tolerance INTEGER
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS starred_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id TEXT,
channel_id TEXT,
message_id TEXT,
original_id TEXT,
emoji TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS sync (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id TEXT,
sync_id TEXT,
confirmed INTEGER,
syncable INTEGER,
sync_notifs TEXT,
ban_notifs TEXT,
enabled INTEGER
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS sync_menus (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id TEXT,
channel_id TEXT,
message_id TEXT,
type INTEGER,
reply_guild TEXT,
reply_channel TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS ticket_configs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id TEXT,
category_id TEXT,
archives_id TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS ticket_posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_id TEXT,
channel_id TEXT,
message_id TEXT
)`);
bot.db.query(`CREATE TABLE IF NOT EXISTS tickets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hid TEXT,
server_id TEXT,
channel_id TEXT,
first_message TEXT,
opener TEXT,
users TEXT,
timestamp TEXT
)`);
files = bot.fs.readdirSync("./events");
files.forEach(f => {
bot.on(f.slice(0,-3), (...args) => require("./events/"+f)(...args,bot));
});
bot.utils = {};
files = bot.fs.readdirSync("./utils");
files.forEach(f => Object.assign(bot.utils, require("./utils/"+f)));
files = recursivelyReadDirectory("./commands");
bot.modules = new Discord.Collection();
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
for(f of files) {
var path_frags = f.replace("./commands/","").split(/(?:\\|\/)/);
var mod = path_frags.length > 1 ? path_frags[path_frags.length - 2] : "Unsorted";
var file = path_frags[path_frags.length - 1];
if(!bot.modules.get(mod.toLowerCase())) {
var mod_info = require(file == "__mod.js" ? f : f.replace(file, "__mod.js"));
bot.modules.set(mod.toLowerCase(), {...mod_info, name: mod, commands: new Discord.Collection()})
}
if(file == "__mod.js") continue;
mod = bot.modules.get(mod.toLowerCase());
if(!mod) {
console.log("Whoopsies");
continue;
}
registerCommand({command: require(f), module: mod, name: file.slice(0, -3).toLowerCase()})
}
}
bot.formatTime = (date) => {
if(typeof date == "string") date = new Date(date);
return `${(date.getMonth()+1) < 10 ? "0"+(date.getMonth()+1) : (date.getMonth()+1)}.${(date.getDate()) < 10 ? "0"+(date.getDate()) : (date.getDate())}.${date.getFullYear()} at ${date.getHours() < 10 ? "0"+date.getHours() : date.getHours()}:${date.getMinutes() < 10 ? "0"+date.getMinutes() : date.getMinutes()}`
}
bot.asyncForEach = async (arr, bot, msg, args, cb) => {
for (let i = 0; i < arr.length; i++) {
await cb(bot, msg, args, arr[i], i, arr);
}
}
bot.parseCommand = async function(bot, msg, args) {
if(!args[0]) return undefined;
var command = bot.commands.get(bot.aliases.get(args[0].toLowerCase()));
if(!command) return {command, nargs: args};
args.shift();
if(args[0] && command.subcommands && command.subcommands.get(command.sub_aliases.get(args[0].toLowerCase()))) {
command = command.subcommands.get(command.sub_aliases.get(args[0].toLowerCase()));
args.shift();
}
return {command, nargs: args};
}
bot.parseCustomCommand = async function(bot, msg, args) {
return new Promise(async res => {
if(!args || !args[0]) return res(undefined);
if(!msg.guild) return res(undefined);
var name = args.shift().toLowerCase();
var cmd = await bot.utils.getCustomCommand(bot, msg.guild.id, name);
if(!cmd) return res(undefined);
cmd.newActions = [];
cmd.actions.forEach(action => {
if(cmd.target == "member") {
switch(action.type) {
case "if":
var condition = action.condition;
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
condition = condition.replace(n, ca.replace)
ac = ac.replace(n, ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`if(${condition}) ${ac};`
), action.success, action.fail]);
break;
case "if:else":
var condition = action.condition;
var tr = action.action[0];
var fls = action.action[1];
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
condition = condition.replace(n, ca.replace)
tr = tr.replace(n, ca.replace);
fls = fls.replace(n, ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`if(${condition}) ${tr};
else ${fls}`
), action.success, action.fail]);
break;
case "rr":
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
ac = ac.replace(n, ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`${ac}`
), action.success, action.fail]);
break;
case "ar":
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
ac = ac.replace(n, ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`${ac}`
), action.success, action.fail]);
break;
case "bl":
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
ac = ac.replace(n, ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`${ac}`
), action.success, action.fail]);
break;
}
} else {
switch(action.type) {
case "if":
var condition = action.condition;
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
condition = condition.replace(n, ca.replace)
ac = ac.replace(n, ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`if(${condition}) ${ac};`
), action.success, action.fail]);
break;
case "if:else":
var condition = action.condition;
var tr = action.action[0];
var fls = action.action[1];
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
condition = condition.replace(n, ca.replace)
tr = tr.replace(n, ca.replace);
fls = fls.replace(n, ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`if(${condition}) ${tr};
else ${fls}`
), action.success, action.fail]);
break;
case "rr":
args.forEach(arg => {
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
ac = ac.replace(n, typeof ca.replace == "function" ? ca.replace(arg) : ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`${ac}`
), action.success, action.fail]);
})
break;
case "ar":
args.forEach(arg => {
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
ac = ac.replace(n, typeof ca.replace == "function" ? ca.replace(arg) : ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`${ac}`
), action.success, action.fail]);
})
break;
case "bl":
args.forEach(arg => {
var ac = action.action;
bot.customActions.forEach(ca => {
var n = ca.regex ? new RegExp(ca.name) : ca.name;
ac = ac.replace(n, typeof ca.replace == "function" ? ca.replace(arg) : ca.replace);
})
cmd.newActions.push([new bot.AsyncFunction("bot", "msg", "args",
`${ac}`
), action.success, action.fail]);
})
break;
}
}
})
cmd.execute = async (bot, msg, args, cmd) => {
console.log("executing...");
let msgs = [];
await bot.utils.asyncForEach(cmd.newActions, async (a) => {
try {
await a[0].call(null, bot, msg, args);
} catch (e) {
if(e) console.log(e);
if(a[2]) return await msg.channel.createMessage(a[2] +`\n${e.message}`).then(message => {msgs.push(message)})
}
if(a[1]) await msg.channel.createMessage(a[1]).then(message => {
msgs.push(message);
})
})
if(cmd.del) {
setTimeout(async ()=> {
await msg.delete();
await Promise.all(msgs.map(async m => {
await m.delete()
return new Promise(res => res(""))
}))
}, 2000)
}
}
res({command: cmd, nargs: args, name})
})
}
bot.on("ready",()=>{
console.log("Ready");
})
setup();
bot.connect();