-
Notifications
You must be signed in to change notification settings - Fork 11
/
functions.js
283 lines (274 loc) · 7.73 KB
/
functions.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
const {
Interaction,
Collection,
Client,
GuildMember,
Permissions,
MessageActionRow,
MessageButton,
} = require("discord.js");
const { embed: ee, emoji, PREFIX } = require("../settings/config");
const client = require("../index");
const { Song } = require("distube");
/**
*
* @param {Interaction} interaction
* @param {String} cmd
*/
function cooldown(interaction, cmd) {
if (!interaction || !cmd) return;
let { client, member } = interaction;
if (!client.cooldowns.has(cmd.name)) {
client.cooldowns.set(cmd.name, new Collection());
}
const now = Date.now();
const timestamps = client.cooldowns.get(cmd.name);
const cooldownAmount = cmd.cooldown * 1000;
if (timestamps.has(member.id)) {
const expirationTime = timestamps.get(member.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000; //get the lefttime
//return true
return timeLeft;
} else {
timestamps.set(member.id, now);
setTimeout(() => timestamps.delete(member.id), cooldownAmount);
return false;
}
} else {
timestamps.set(member.id, now);
setTimeout(() => timestamps.delete(member.id), cooldownAmount);
return false;
}
}
/**
*
* @param {Client} client
* @param {GuildMember} member
* @param {Song} song
* @returns
*/
async function check_dj(client, member, song = null) {
//if no message added return
if (!client) return false;
//get the adminroles
let roleid = await client.music.get(`${member.guild.id}.djrole`);
//if no dj roles return false, so that it continues
var isdj = false;
if (!roleid) return false;
//if the role does not exist, then skip this current loop run
if (!member.guild.roles.cache.get(roleid)) {
await client.music.set(`${member.guild.id}.djrole`, null);
return;
}
//if he has role set var to true
if (member.roles.cache.has(roleid)) isdj = true;
//if he is a dj or admin, then return false, which will continue the cmd
if (
!isdj &&
!member.permissions.has(Permissions.FLAGS.ADMINISTRATOR) &&
song?.user.id !== member.id
) {
return `<@${roleid}>`;
} else {
return false;
}
}
async function databasing(guildID, userID) {
await client.music.ensure(guildID, {
prefix: PREFIX,
djrole: null,
vc: {
enable: false,
channel: null,
},
music: {
channel: null,
pmsg: null,
qmsg: null,
},
autoresume : false,
});
await client.queue.ensure(userID, {
TEMPLATEQUEUEINFORMATION: ["queue", "sadasd"],
});
}
async function swap_pages(interaction, embeds) {
let currentPage = 0;
let allbuttons = new MessageActionRow().addComponents([
new MessageButton().setStyle("SECONDARY").setCustomId("0").setLabel("<<"),
// .setEmoji(`⏪`),
new MessageButton().setStyle("SECONDARY").setCustomId("1").setLabel("<"),
// .setEmoji(`◀️`),
new MessageButton().setStyle("DANGER").setCustomId("2").setLabel("⛔️"),
// .setEmoji(`🗑`),
new MessageButton().setStyle("SECONDARY").setCustomId("3").setLabel(">"),
// .setEmoji(`▶️`),
new MessageButton().setStyle("SECONDARY").setCustomId("4").setLabel(">>"),
// .setEmoji(`⏩`),
]);
if (embeds.length === 1) {
if (interaction.deferred) {
return interaction.followUp({
embeds: [embeds[0]],
fetchReply: true,
});
} else {
return interaction.reply({
embeds: [embeds[0]],
fetchReply: true,
});
}
}
//Send message with buttons
embeds = embeds.map((embed, index) => {
return embed.setColor(client.config.embed.color).setFooter({
text: `Page ${index + 1} / ${embeds.length}`,
iconURL: interaction.guild.iconURL({ dynamic: true }),
});
});
let swapmsg;
if (interaction.deferred) {
swapmsg = await interaction.followUp({
embeds: [embeds[0]],
components: [allbuttons],
});
} else {
swapmsg = await interaction.reply({
embeds: [embeds[0]],
components: [allbuttons],
});
}
//create a collector for the thinggy
const collector = swapmsg.createMessageComponentCollector({
time: 2000 * 60,
});
collector.on("collect", async (b) => {
if (b.isButton()) {
await b.deferUpdate().catch((e) => {});
// page first
if (b.customId == "0") {
if (currentPage !== 0) {
currentPage = 0;
await swapmsg
.edit({
embeds: [embeds[currentPage]],
components: [allbuttons],
})
.catch((e) => null);
}
}
//page forward
if (b.customId == "1") {
if (currentPage !== 0) {
currentPage -= 1;
await swapmsg
.edit({
embeds: [embeds[currentPage]],
components: [allbuttons],
})
.catch((e) => null);
} else {
currentPage = embeds.length - 1;
await swapmsg
.edit({
embeds: [embeds[currentPage]],
components: [allbuttons],
})
.catch((e) => null);
}
}
//go home
else if (b.customId == "2") {
try {
allbuttons.components.forEach((btn) => btn.setDisabled(true));
swapmsg
.edit({
embeds: [embeds[currentPage]],
components: [allbuttons],
})
.catch((e) => null);
} catch (e) {}
}
//go forward
else if (b.customId == "3") {
if (currentPage < embeds.length - 1) {
currentPage++;
await swapmsg
.edit({
embeds: [embeds[currentPage]],
components: [allbuttons],
})
.catch((e) => null);
} else {
currentPage = 0;
await swapmsg
.edit({
embeds: [embeds[currentPage]],
components: [allbuttons],
})
.catch((e) => null);
}
}
// page last
if (b.customId == "4") {
currentPage = embeds.length - 1;
await swapmsg
.edit({
embeds: [embeds[currentPage]],
components: [allbuttons],
})
.catch((e) => null);
}
}
});
collector.on("end", () => {
allbuttons.components.forEach((btn) => btn.setDisabled(true));
swapmsg.edit({ components: [allbuttons] }).catch((e) => null);
});
}
function shuffle(array) {
try {
var j, x, i;
for (i = array.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = array[i];
array[i] = array[j];
array[j] = x;
}
return array;
} catch (e) {
console.log(String(e.stack).bgRed);
}
}
function createBar(total, current, size = 25, line = "▬", slider = "🔷") {
try {
if (!total) throw "MISSING MAX TIME";
if (!current) return `**[${slider}${line.repeat(size - 1)}]**`;
let bar =
current > total
? [line.repeat((size / 2) * 2), (current / total) * 100]
: [
line
.repeat(Math.round((size / 2) * (current / total)))
.replace(/.$/, slider) +
line.repeat(size - Math.round(size * (current / total)) + 1),
current / total,
];
if (!String(bar).includes(slider)) {
return `**[${slider}${line.repeat(size - 1)}]**`;
} else {
return `**[${bar[0]}]**`;
}
} catch (e) {
console.log(String(e.stack).bgRed);
}
}
module.exports = {
cooldown,
check_dj,
databasing,
swap_pages,
shuffle,
createBar,
};