-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.js
206 lines (191 loc) · 4.91 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
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;
}
module.exports = {
recursivelyReadDirectory,
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);
})
},
genTicketEmbed: (ticket) => {
var users;
if(ticket.users.length > 20) {
users = ticket.users.slice(0, 21)
.map(u => `<@${u.id}>`)
.join("\n") +
`\nand ${ticket.users.length - 20} more`;
} else users = ticket.users.map(u => `<@${u.id}>`).join("\n");
return {
title: ticket.name ?? "Untitled Ticket",
description: ticket.description ?? "(no description)",
fields: [
{
name: "Ticket Opener",
value: `<@${ticket.opener.id}>`
},
{
name: "Ticket Users",
value: users
}
],
color: ticket.closed ? 0xaa5555 : 0x55aa55,
footer: {
text: `Ticket ID: ${ticket.hid}`
},
timestamp: ticket.timestamp
}
},
async checkTicketPerms(ctx) {
var {
msg,
ticket,
user,
cfg,
action
} = ctx;
var member = await msg.guild.members.fetch(user);
if(cfg?.mod_only?.includes(action) && !member.permissions.has("ManageChannels"))
return false;
if(member.id != ticket.opener && !member.permissions.has("ManageChannels"))
return false;
return true;
},
async awaitChannelSelect(ctx, opts = {}) {
var { message, max, min, types, placeholder, defaults } = opts;
var components = [{
type: 1,
components: [{
type: 8,
custom_id: 'channel-select',
max_values: max,
min_values: min,
channel_types: types,
placeholder,
default_values: defaults
}]
}]
var reply;
if(ctx.replied || ctx.deferred) {
reply = await ctx.followUp({
content: message,
components
});
} else {
reply = await ctx.reply({
content: message,
components
});
}
try {
var resp = await reply.awaitMessageComponent({
filter: (intr) => intr.user.id == ctx.user.id && intr.customId == 'channel-select',
time: 60000
});
} catch(e) { console.error(e) }
if(!resp) return 'Nothing was selected.';
await resp.update({
components: [{
type: 1,
components: components[0].components.map(c => ({
...c,
disabled: true
}))
}]
});
return resp.values;
},
async awaitRoleSelect(ctx, opts = {}) {
var { message, max, min, placeholder, defaults } = opts;
var components = [{
type: 1,
components: [{
type: 6,
custom_id: 'role-select',
max_values: max,
min_values: min,
placeholder,
default_values: defaults
}]
}]
var reply;
if(ctx.replied || ctx.deferred) {
reply = await ctx.followUp({
content: message,
components
});
} else {
reply = await ctx.reply({
content: message,
components
});
}
try {
var resp = await reply.awaitMessageComponent({
filter: (intr) => intr.user.id == ctx.user.id && intr.customId == 'role-select',
time: 60000
});
} catch(e) { console.error(e) }
if(!resp) return 'Nothing was selected.';
await resp.update({
components: [{
type: 1,
components: components[0].components.map(c => ({
...c,
disabled: true
}))
}]
});
return resp.values;
}
}