-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
307 lines (276 loc) · 8.83 KB
/
utils.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
const { Collection } = require('discord.js');
const fs = require('fs');
const recursivelyReadDirectory = (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 loadCommands = (path) => {
var modules = new Collection();
var mod_aliases = new Collection();
var commands = new Collection();
var aliases = new Collection();
var files = recursivelyReadDirectory(path);
for(f of files) {
var path_frags = f.replace(path, "").split(/(?:\\|\/)/);
var mod = path_frags.length > 1 ? path_frags[path_frags.length - 2] : "Unsorted";
var file = path_frags[path_frags.length - 1];
if(!modules.get(mod.toLowerCase())) {
var mod_info = require(file == "__mod.js" ? f : f.replace(file, "__mod.js"));
modules.set(mod.toLowerCase(), {...mod_info, name: mod, commands: new Collection()})
mod_aliases.set(mod.toLowerCase(), mod.toLowerCase());
if(mod_info.alias) mod_info.alias.forEach(a => mod_aliases.set(a, mod.toLowerCase()));
}
if(file == "__mod.js") continue;
mod = modules.get(mod.toLowerCase());
if(!mod) {
console.log("Whoopsies");
continue;
}
var command = require(f);
command.module = mod;
command.name = file.slice(0, -3).toLowerCase();
command = registerSubcommands(command, mod);
commands.set(command.name, command);
mod.commands.set(command.name, command);
aliases.set(command.name, command.name);
if(command.alias) command.alias.forEach(a => aliases.set(a, command.name));
}
return {modules, mod_aliases, commands, aliases};
}
const registerSubcommands = function(command, module, name) {
if(command.subcommands) {
var subcommands = command.subcommands;
command.subcommands = new 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 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;
}
module.exports = {
recursivelyReadDirectory,
loadCommands,
registerSubcommands,
genEmbeds: async (bot, arr, genFunc, info = {}, fieldnum, extras = {}) => {
return new Promise(async res => {
var embeds = [];
var current = { embed: {
title: typeof info.title == "function" ?
info.title(arr[0], 0) : info.title,
description: typeof info.description == "function" ?
info.description(arr[0], 0) : info.description,
color: typeof info.color == "function" ?
info.color(arr[0], 0) : info.color,
footer: info.footer,
fields: []
}};
for(let i=0; i<arr.length; i++) {
if(current.embed.fields.length < (fieldnum || 10)) {
current.embed.fields.push(await genFunc(arr[i], bot));
} else {
embeds.push(current);
current = { embed: {
title: typeof info.title == "function" ?
info.title(arr[i], i) : info.title,
description: typeof info.description == "function" ?
info.description(arr[i], i) : info.description,
color: typeof info.color == "function" ?
info.color(arr[i], i) : info.color,
footer: info.footer,
fields: [await genFunc(arr[i], bot)]
}};
}
}
embeds.push(current);
if(extras.order && extras.order == 1) {
if(extras.map) embeds = embeds.map(extras.map);
if(extras.filter) embeds = embeds.filter(extras.filter);
} else {
if(extras.filter) embeds = embeds.filter(extras.filter);
if(extras.map) embeds = embeds.map(extras.map);
}
if(embeds.length > 1) {
for(let i = 0; i < embeds.length; i++)
embeds[i].embed.title += (extras.addition != null ? eval("`"+extras.addition+"`") : ` (page ${i+1}/${embeds.length}, ${arr.length} total)`);
}
res(embeds);
})
},
paginateEmbeds: async function(bot, m, reaction) {
switch(reaction.emoji.name) {
case "⬅️":
if(this.index == 0) {
this.index = this.data.length-1;
} else {
this.index -= 1;
}
await m.edit(this.data[this.index]);
await reaction.users.remove(this.user)
bot.menus[m.id] = this;
break;
case "➡️":
if(this.index == this.data.length-1) {
this.index = 0;
} else {
this.index += 1;
}
await m.edit(this.data[this.index]);
await reaction.users.remove(this.user)
bot.menus[m.id] = this;
break;
case "⏹️":
await m.delete();
delete bot.menus[m.id];
break;
}
},
cleanText: function(text){
if (typeof(text) === "string") {
return text.replace(/`/g, "`" + String.fromCharCode(8203)).replace(/@/g, "@" + String.fromCharCode(8203));
} else {
return text;
}
},
checkPermissions: async (bot, msg, cmd, cfg)=>{
return new Promise((res)=> {
if(!cmd.permissions) return res(true);
if(cfg?.opped?.includes(msg.author.id) ||
msg.member.roles.cache.find(r => cfg?.opped?.includes(r.id)))
return res(true);
res(msg.member.permissions.has(cmd.permissions))
})
},
//accessible reactions
getConfirmation: async (bot, msg, user) => {
return new Promise(res => {
function msgListener(message) {
if(message.channel.id != msg.channel.id ||
message.author.id != user.id) return;
clearTimeout(timeout);
bot.removeListener('message', msgListener);
bot.removeListener('messageReactionAdd', reactListener);
switch(message.content.toLowerCase()) {
case 'y':
case 'yes':
case '✅':
return res({confirmed: true, message});
break;
default:
return res({confirmed: false, message, msg: 'Action cancelled.'});
break;
}
}
function reactListener(react, ruser) {
if(react.message.channel.id != msg.channel.id ||
ruser.id != user.id) return;
clearTimeout(timeout);
bot.removeListener('message', msgListener);
bot.removeListener('messageReactionAdd', reactListener);
switch(react.emoji.name) {
case '✅':
return res({confirmed: true, react});
break;
default:
return res({confirmed: false, react, msg: 'Action cancelled.'});
break;
}
}
const timeout = setTimeout(async () => {
bot.removeListener('message', msgListener);
bot.removeListener('messageReactionAdd', reactListener);
res({confirmed: false, msg: 'Action timed out.'})
}, 30000);
bot.on('message', msgListener);
bot.on('messageReactionAdd', reactListener);
})
},
handleChoices: async (bot, msg, user, choices) => {
/*
example usage pseudo-code:
choices = [
{
accepted: ['y', 'yes', 'yeah', '✅'],
name: 'yes',
msg: 'You picked `yes`.'
},
{
accepted: ['n', 'no', 'nah', '❌'],
name: 'no',
msg: 'You picked `no`.'
}
]
chosen = await handleChoices(...args);
switch(chosen.name) {
case 'yes':
case 'no':
return chosen.msg;
break;
case 'invalid':
return 'You picked something else.';
break;
default:
return 'You picked nothing.'
break;
}
*/
return new Promise(res => {
function msgListener(message) {
if(message.channel.id != msg.channel.id ||
message.author.id != user.id) return;
clearTimeout(timeout);
bot.removeListener('message', msgListener);
bot.removeListener('messageReactionAdd', reactListener);
var choice = choices.find(c => c.accepted.includes(message.content.toLowerCase()));
if(choice) return res({...choice, message});
else return res({choice: 'invalid', message});
}
function reactListener(react, ruser) {
if(react.message.channel.id != msg.channel.id ||
ruser.id != user.id) return;
clearTimeout(timeout);
bot.removeListener('message', msgListener);
bot.removeListener('messageReactionAdd', reactListener);
var choice = choices.find(c => c.accepted.includes(react.emoji.name));
if(choice) return res({...choice, react});
else return res({choice: 'invalid', react});
}
const timeout = setTimeout(async () => {
bot.removeListener('message', msgListener);
bot.removeListener('messageReactionAdd', reactListener);
res({choice: 'none', msg: 'Action timed out.'})
}, 30000);
bot.on('message', msgListener);
bot.on('messageReactionAdd', reactListener);
})
},
getMessage: async (bot, server, channel, message) => {
return new Promise(async (res, rej) => {
try {
var guild = bot.guilds.resolve(server);
var chan = await guild.channels.resolve(channel)?.fetch();
var msg = await chan?.messages.fetch(message);
} catch(e) {
return rej(e);
}
res(msg);
})
}
}