-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhubbot.js
363 lines (320 loc) · 11.9 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
const Eris = require("eris-additions")(require("eris"));
const Discord = require("discord.js");
const dblite = require("dblite");
const fs = require("fs");
const path = require("path");
require('dotenv').config();
const bot = new Eris(process.env.TOKEN, {
restMode: true,
intents: [
"allNonPrivileged",
"guildMessages"
]
});
bot.chars = process.env.CHARS;
bot.prefix = process.env.PREFIX;
bot.owner = process.env.OWNER;
bot.fetch = require('node-fetch');
bot.tc = require('tinycolor2');
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.get('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(), r.id].includes($1.toLowerCase()))?.id", regex: true}
]
bot.banVars = {
"$REASON": "${reason}",
"$SERVER.NAME": "${msg.guild.name}"
}
bot.logVars = {
"$SERVER.NAME": '${guild.name || "(no name)"}'
}
const updateStatus = async function(){
bot.stores.servers.getAll(process.env.HUB_ID).then(data => {
if(data && data.length > 0) bot.editStatus({name: `Listing ${data.length} servers and counting!`});
else bot.editStatus({ name: `${bot.prefix}h`})
});
setTimeout(()=> updateStatus(),600000)
}
bot.AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
const recursivelyReadDirectory = function(dir) {
var results = [];
var files = 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() {
var files;
bot.db = await require('./stores/__db.js')(bot);
files = fs.readdirSync("./events");
files.forEach(f => bot.on(f.slice(0,-3), (...args) => require("./events/"+f)(...args,bot)));
bot.utils = {};
files = fs.readdirSync("./utils");
files.forEach(f => Object.assign(bot.utils, require("./utils/"+f)));
files = recursivelyReadDirectory("./commands");
bot.modules = new Discord.Collection();
bot.mod_aliases = 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()})
bot.mod_aliases.set(mod.toLowerCase(), mod.toLowerCase());
if(mod_info.alias) mod_info.alias.forEach(a => bot.mod_aliases.set(a, mod.toLowerCase()));
}
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, command) {
if(!args[0]) return undefined;
var command = bot.commands.get(bot.aliases.get(args[0].toLowerCase()));
if(!command) return {command, args};
args.shift();
var permcheck = true;
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();
}
//will erroneously give true in dms even though perms don't exist
//guildOnly check is done first in actual command execution though,
//so that doesn't matter
if(command.permissions && msg.guild) permcheck = command.permissions.filter(x => msg.member.permission.has(x)).length == command.permissions.length;
return {command, args, permcheck};
}
bot.parseCustomCommand = async function(bot, msg, args) {
return new Promise(async res => {
if(!args || !args[0]) return res({});
if(!msg.guild) return res({});
var name = args.shift();
var cmd = await bot.stores.customCommands.get(msg.guild.id, name);
if(!cmd) return res({});
if(cmd.usage_list && Object.keys(cmd.usage_list).find(k => k.length)) {
var rlcheck = cmd.usage_list.roles?.length && msg.member.roles.find(r => cmd.usage_list.roles.includes(r));
var chcheck = cmd.usage_list.channels?.length && cmd.usage_list.channels.includes(msg.channel.id);
if(!cmd.usage_type && (rlcheck || chcheck)) return res({command: cmd, args, permcheck: false});
if(cmd.usage_type && !(rlcheck || chcheck)) return res({command: cmd, args, permcheck: false});
}
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) => {
let msgs = [];
await bot.asyncForEach(cmd.newActions, bot, msg, args, async (bot, msg, args, 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)
}
}
cmd.name = name;
res({command: cmd, args, permcheck: true})
})
}
bot.on("ready",()=>{
console.log("Ready");
updateStatus();
})
setup();
bot.connect();
process.on('unhandledRejection', error => {
//figure out better error handling
console.log(error);
});