Skip to content

Commit b372cc5

Browse files
committed
refactor: clean code for better readability
1 parent 656fd2d commit b372cc5

28 files changed

Lines changed: 346 additions & 132 deletions

admin/config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ module.exports = {
1717
DISTUBE_SEARCH_MAX_RESULTS: 20, // Max. number of search results
1818
DISTUBE_NSFW: true, // Play age-restricted content
1919
DISTUBE_SAVE_PREVIOUS_SONGS: false, // Save previous songs in queue
20-
DISTUBE_CUSTOM_FILTERS: { "8d": "apulsator=hz=0.08", "purebass": "bass=g=20,dynaudnorm=f=200,asubboost", "subboost": "asubboost", "fast": "atempo=1.3", "vibrato": "vibrato=f=6.5", "pulsator": "apulsator=hz=1" }, // Custom ffmpeg filters
20+
DISTUBE_CUSTOM_FILTERS: { // Custom ffmpeg filters
21+
"8d": "apulsator=hz=0.08",
22+
"purebass": "bass=g=20,dynaudnorm=f=200,asubboost",
23+
"subboost": "asubboost",
24+
"fast": "atempo=1.3",
25+
"vibrato": "vibrato=f=6.5",
26+
"pulsator": "apulsator=hz=1"
27+
},
2128
DISTUBE_ERROR_MAPPING: [ // Map of distube error messages
2229
{
2330
keywords: ["I do not have permission to join this voice channel", "Cannot connect to the voice channel", "The voice channel is full"],

commands/autoplay.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ module.exports = {
1010
run: async (client, interaction, guildData, queue, lang) => {
1111
const { guild, member } = interaction;
1212

13-
if (!queue?.songs[0]) return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_NO_PLAYING}`);
14-
if (!client.checkMemberIsInMyVoiceChannel(guild, member)) return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
15-
if (!client.handleCooldown("autoplayCommand", guild.id, 2000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
13+
if (!queue?.songs[0])
14+
return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_NO_PLAYING}`);
15+
if (!client.checkMemberIsInMyVoiceChannel(guild, member))
16+
return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
17+
if (!client.handleCooldown("autoplayCommand", guild.id, 2000))
18+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
1619
await interaction.deferReply().catch((error) => { });
1720
try {
1821
// Toggle autoplay

commands/filter.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@ module.exports = {
3535
switch (subcommand) {
3636
case "toggle":
3737
const name = options.getString("name");
38-
if (!queue?.songs[0]) return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_NO_PLAYING}`);
39-
if (!client.checkMemberIsInMyVoiceChannel(guild, member)) return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
40-
if (!client.handleCooldown("filterCommand", guild.id, 2000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
38+
if (!queue?.songs[0])
39+
return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_NO_PLAYING}`);
40+
if (!client.checkMemberIsInMyVoiceChannel(guild, member))
41+
return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
42+
if (!client.handleCooldown("filterCommand", guild.id, 2000))
43+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
4144
await interaction.deferReply().catch((error) => { });
4245
try {
4346
// Toggle filter
44-
if (filters.has(name)) await filters.remove(name);
45-
else await filters.add(name);
47+
if (filters.has(name))
48+
await filters.remove(name);
49+
else
50+
await filters.add(name);
4651
// Resume queue if paused
47-
if (queue.paused) client.distube.resume(queue);
52+
if (queue.paused)
53+
client.distube.resume(queue);
4854
// Update dashboard message and send notification
4955
const filterNames = filters.names.map((filterName, i) => { return `\`${filterName}\`` }).join(", ");
5056
client.updateDashboardMessage(guild, queue, lang);
@@ -55,15 +61,19 @@ module.exports = {
5561
}
5662
break;
5763
case "reset":
58-
if (!filters?.size) return client.sendErrorNotification(interaction, `${lang.ERROR_FILTER_NO_ACTIVE}`);
59-
if (!client.checkMemberIsInMyVoiceChannel(guild, member)) return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
60-
if (!client.handleCooldown("filterCommand", guild.id, 2000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
64+
if (!filters?.size)
65+
return client.sendErrorNotification(interaction, `${lang.ERROR_FILTER_NO_ACTIVE}`);
66+
if (!client.checkMemberIsInMyVoiceChannel(guild, member))
67+
return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
68+
if (!client.handleCooldown("filterCommand", guild.id, 2000))
69+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
6170
await interaction.deferReply().catch((error) => { });
6271
try {
6372
// Clear filters
6473
await filters.clear();
6574
// Resume queue if paused
66-
if (queue.paused) client.distube.resume(queue);
75+
if (queue.paused)
76+
client.distube.resume(queue);
6777
// Update dashboard message and send notification
6878
client.updateDashboardMessage(guild, queue, lang);
6979
client.sendNotification(interaction, `${lang.MESSAGE_FILTERS_ACTIVE} ${lang.MESSAGE_FILTERS_NONE}`, { editReply: true });

commands/first.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ module.exports = {
1818
const { guild, channel, member, options } = interaction;
1919
const query = options.getString("query");
2020

21-
if (!member.voice.channel) return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_VOICE_CHANNEL}`);
22-
if (!client.checkMemberIsInMyVoiceChannel(guild, member) && queue) return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
23-
if (!client.handleCooldown("playQuery", member.id, 2000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
21+
if (!member.voice.channel)
22+
return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_VOICE_CHANNEL}`);
23+
if (!client.checkMemberIsInMyVoiceChannel(guild, member) && queue)
24+
return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
25+
if (!client.handleCooldown("playQuery", member.id, 2000))
26+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
2427
await interaction.deferReply().catch((error) => { });
2528
try {
2629
// Play or add item to the queue at position 1 using query

commands/info.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ module.exports = {
1212
const member = interaction.member;
1313
const currentSong = queue?.songs[0]?.stream?.song || queue?.songs[0];
1414

15-
if (!currentSong) return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_NO_PLAYING}`);
16-
if (!client.handleCooldown("infoCommand", member.id, 2000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
15+
if (!currentSong)
16+
return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_NO_PLAYING}`);
17+
if (!client.handleCooldown("infoCommand", member.id, 2000))
18+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
1719
// Create duration bar
1820
const durationBar = client.createDurationBar(queue, currentSong);
1921
// Create info embed
20-
const infoEmbed = new EmbedBuilder().setAuthor({ name: `${currentSong.name}`, url: currentSong.url, iconURL: elements.ICON_FLOPY }).setThumbnail(currentSong.thumbnail).addFields({ name: `**${lang.MESSAGE_ITEM_AUTHOR}**`, value: `${currentSong.uploader?.name || "-"}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_VIEWS}**`, value: `${currentSong.views.toString().replace(/(.)(?=(\d{3})+$)/g, "$1,")}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_LIKES}**`, value: `${currentSong.likes.toString().replace(/(.)(?=(\d{3})+$)/g, "$1,")}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_DURATION}**`, value: `${durationBar}` }).setColor(elements.COLOR_FLOPY);
22+
const infoEmbed = new EmbedBuilder()
23+
.setAuthor({ name: `${currentSong.name}`, url: currentSong.url, iconURL: elements.ICON_FLOPY })
24+
.setThumbnail(currentSong.thumbnail)
25+
.addFields({ name: `**${lang.MESSAGE_ITEM_AUTHOR}**`, value: `${currentSong.uploader?.name || "-"}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_VIEWS}**`, value: `${currentSong.views.toString().replace(/(.)(?=(\d{3})+$)/g, "$1,")}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_LIKES}**`, value: `${currentSong.likes.toString().replace(/(.)(?=(\d{3})+$)/g, "$1,")}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_DURATION}**`, value: `${durationBar}` })
26+
.setColor(elements.COLOR_FLOPY);
2127
// Send song informaton and then delete reply
2228
interaction.reply({ embeds: [infoEmbed], ephemeral: true }).catch(error => { });
2329
setTimeout(() => interaction.deleteReply().catch((error) => { }), 60000);

commands/join.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ module.exports = {
1111
const { guild, member } = interaction
1212
const voiceChannel = member.voice.channel;
1313

14-
if (!voiceChannel) return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_VOICE_CHANNEL}`);
15-
if (client.checkMemberIsInMyVoiceChannel(guild, member)) return client.sendErrorNotification(interaction, `${lang.ERROR_VOICE_CHANNEL_ALREADY_JOINED}`);
16-
if (!client.checkMyVoiceChannelIsEmpty(guild) && queue) return client.sendErrorNotification(interaction, `${lang.ERROR_VOICE_CHANNEL_UNABLE_JOIN}`);
17-
if (!client.handleCooldown("joinCommand", member.id, 2000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
14+
if (!voiceChannel)
15+
return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_VOICE_CHANNEL}`);
16+
if (client.checkMemberIsInMyVoiceChannel(guild, member))
17+
return client.sendErrorNotification(interaction, `${lang.ERROR_VOICE_CHANNEL_ALREADY_JOINED}`);
18+
if (!client.checkMyVoiceChannelIsEmpty(guild) && queue)
19+
return client.sendErrorNotification(interaction, `${lang.ERROR_VOICE_CHANNEL_UNABLE_JOIN}`);
20+
if (!client.handleCooldown("joinCommand", member.id, 2000))
21+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
1822
await interaction.deferReply().catch((error) => { });
1923
try {
2024
// Join voice channel

commands/jump.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ module.exports = {
1818
const { guild, member, options } = interaction;
1919
const position = options.getInteger("position");
2020

21-
if (!queue?.songs[position] || position === 0) return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_INVALID_POSITION}`);
22-
if (!client.checkMemberIsInMyVoiceChannel(guild, member)) return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
23-
if (!client.handleCooldown("jumpCommand", guild.id, 2000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
21+
if (!queue?.songs[position] || position === 0)
22+
return client.sendErrorNotification(interaction, `${lang.ERROR_SONG_INVALID_POSITION}`);
23+
if (!client.checkMemberIsInMyVoiceChannel(guild, member))
24+
return client.sendErrorNotification(interaction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
25+
if (!client.handleCooldown("jumpCommand", guild.id, 2000))
26+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
2427
await interaction.deferReply().catch((error) => { });
2528
try {
2629
// Jump to position
2730
const song = await client.distube.jump(queue, position);
2831
// Resume queue if paused
29-
if (queue.paused) client.distube.resume(queue);
32+
if (queue.paused)
33+
client.distube.resume(queue);
3034
// Send advanced notification
3135
client.sendAdvancedNotification(interaction, `${lang.MESSAGE_SONG_SKIPPED.replace("$position", `#${position}`)}`, `${song.name}`, song.thumbnail, { editReply: true });
3236
} catch (error) {

commands/library.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ module.exports = {
4141

4242
switch (subcommand) {
4343
case "view":
44-
if (library.length === 0) return client.sendErrorNotification(interaction, `${lang.ERROR_LIBRARY_NO_ITEM}`);
45-
if (!client.handleCooldown("libraryCommand", user.id, 4000)) return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
44+
if (library.length === 0)
45+
return client.sendErrorNotification(interaction, `${lang.ERROR_LIBRARY_NO_ITEM}`);
46+
if (!client.handleCooldown("libraryCommand", user.id, 4000))
47+
return client.sendErrorNotification(interaction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
4648
await interaction.deferReply({ ephemeral: true }).catch((error) => { });
4749
try {
4850
// Update response
@@ -66,9 +68,12 @@ module.exports = {
6668
const type = options.getString("type") || "video";
6769
const isPlaylist = type === "playlist";
6870
const currentItem = isPlaylist ? queue?.songs[0]?.playlist : queue?.songs[0]?.stream?.song || queue?.songs[0];
69-
if (!currentItem) return client.sendErrorNotification(interaction, `${isPlaylist ? lang.ERROR_PLAYLIST_NO_PLAYING : lang.ERROR_SONG_NO_PLAYING}`);
70-
if (library.find((item) => item.url === currentItem.url)) return client.sendErrorNotification(interaction, `${isPlaylist ? lang.ERROR_LIBRARY_PLAYLIST_ALREADY_ADDED : lang.ERROR_LIBRARY_SONG_ALREADY_ADDED}`);
71-
if (library.length >= config.LIBRARY_MAX_LENGTH) return client.sendErrorNotification(interaction, `${lang.ERROR_LIBRARY_LIMIT_REACHED}`);
71+
if (!currentItem)
72+
return client.sendErrorNotification(interaction, `${isPlaylist ? lang.ERROR_PLAYLIST_NO_PLAYING : lang.ERROR_SONG_NO_PLAYING}`);
73+
if (library.find((item) => item.url === currentItem.url))
74+
return client.sendErrorNotification(interaction, `${isPlaylist ? lang.ERROR_LIBRARY_PLAYLIST_ALREADY_ADDED : lang.ERROR_LIBRARY_SONG_ALREADY_ADDED}`);
75+
if (library.length >= config.LIBRARY_MAX_LENGTH)
76+
return client.sendErrorNotification(interaction, `${lang.ERROR_LIBRARY_LIMIT_REACHED}`);
7277
// Add item to library
7378
library.push({ name: currentItem.name, author: currentItem.uploader?.name, thumbnail: currentItem.thumbnail, url: currentItem.url, isPlaylist: isPlaylist });
7479
await interaction.deferReply({ ephemeral: true }).catch((error) => { });
@@ -107,9 +112,12 @@ module.exports = {
107112
}
108113
break;
109114
case "play":
110-
if (!member.voice.channel) return client.sendErrorNotification(subinteraction, `${lang.ERROR_MEMBER_MUST_JOIN_VOICE_CHANNEL}`);
111-
if (!client.checkMemberIsInMyVoiceChannel(guild, member) && queue) return client.sendErrorNotification(subinteraction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
112-
if (!client.handleCooldown("playQuery", member.id, 2000)) return client.sendErrorNotification(subinteraction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
115+
if (!member.voice.channel)
116+
return client.sendErrorNotification(subinteraction, `${lang.ERROR_MEMBER_MUST_JOIN_VOICE_CHANNEL}`);
117+
if (!client.checkMemberIsInMyVoiceChannel(guild, member) && queue)
118+
return client.sendErrorNotification(subinteraction, `${lang.ERROR_MEMBER_MUST_JOIN_MY_VOICE_CHANNEL}`);
119+
if (!client.handleCooldown("playQuery", member.id, 2000))
120+
return client.sendErrorNotification(subinteraction, `${lang.ERROR_ACTION_NOT_POSSIBLE}`);
113121
await subinteraction.deferReply().catch((error) => { });
114122
try {
115123
// Play or add item to the queue using selected item url
@@ -121,13 +129,15 @@ module.exports = {
121129
break;
122130
case "remove":
123131
const position = library.findIndex((item) => item.url === selectedItem.url);
124-
if (position === -1) return client.sendErrorNotification(subinteraction, `${selectedItem.isPlaylist ? lang.ERROR_LIBRARY_PLAYLIST_NOT_FOUND : lang.ERROR_LIBRARY_SONG_NOT_FOUND}`);
132+
if (position === -1)
133+
return client.sendErrorNotification(subinteraction, `${selectedItem.isPlaylist ? lang.ERROR_LIBRARY_PLAYLIST_NOT_FOUND : lang.ERROR_LIBRARY_SONG_NOT_FOUND}`);
125134
// Remove item from library
126135
library.splice(position, 1);
127136
try {
128137
// Update user data in database
129138
await client.updateUserData(member, { library: library });
130-
if (library.length === 0) return interaction.deleteReply().catch((error) => { });
139+
if (library.length === 0)
140+
return interaction.deleteReply().catch((error) => { });
131141
// Update response
132142
await module.exports.updateResponse(interaction, lang, library, library[0]);
133143
selections[message.id] = library[0];
@@ -142,9 +152,28 @@ module.exports = {
142152
updateResponse: async (interaction, lang, items, selectedItem) => {
143153
// Update library embed, menu and buttons
144154
const options = items.map((item, i) => { return { label: `${i + 1}. ${item.name.length > config.ITEM_NAME_MAX_LENGTH_DISPLAY ? item.name.substr(0, config.ITEM_NAME_MAX_LENGTH_DISPLAY).concat("...") : item.name}`, description: `${item.author || "-"}`, emoji: item.isPlaylist ? elements.EMOJI_PLAYLIST : elements.EMOJI_SONG, value: `${i}`, default: item === selectedItem } });
145-
const libraryEmbed = new EmbedBuilder().setAuthor({ name: `${lang.MESSAGE_LIBRARY_TITLE} (${items.length}/${config.LIBRARY_MAX_LENGTH})`, iconURL: interaction.user.displayAvatarURL({ forceStatic: true }) }).addFields({ name: `**${lang.MESSAGE_ITEM_TITLE}**`, value: `[${selectedItem.name}](${selectedItem.url})` }, { name: `**${lang.MESSAGE_ITEM_AUTHOR}**`, value: `${selectedItem.author || "-"}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_TYPE}**`, value: `${selectedItem.isPlaylist ? lang.ITEM_PLAYLIST : lang.ITEM_SONG}`, inline: true }).setThumbnail(selectedItem.thumbnail).setColor(elements.COLOR_FLOPY);
146-
const libraryMenu = new ActionRowBuilder().addComponents(new StringSelectMenuBuilder().setCustomId("select").setOptions(options));
147-
const libraryButtons = new ActionRowBuilder().addComponents(new ButtonBuilder().setCustomId("play").setStyle(ButtonStyle.Primary).setLabel(`${lang.BUTTON_PLAY}`), new ButtonBuilder().setCustomId("remove").setStyle(ButtonStyle.Danger).setLabel(`${lang.BUTTON_LIBRARY_REMOVE}`));
155+
const libraryEmbed = new EmbedBuilder()
156+
.setAuthor({ name: `${lang.MESSAGE_LIBRARY_TITLE} (${items.length}/${config.LIBRARY_MAX_LENGTH})`, iconURL: interaction.user.displayAvatarURL({ forceStatic: true }) })
157+
.addFields({ name: `**${lang.MESSAGE_ITEM_TITLE}**`, value: `[${selectedItem.name}](${selectedItem.url})` }, { name: `**${lang.MESSAGE_ITEM_AUTHOR}**`, value: `${selectedItem.author || "-"}`, inline: true }, { name: `**${lang.MESSAGE_ITEM_TYPE}**`, value: `${selectedItem.isPlaylist ? lang.ITEM_PLAYLIST : lang.ITEM_SONG}`, inline: true })
158+
.setThumbnail(selectedItem.thumbnail)
159+
.setColor(elements.COLOR_FLOPY);
160+
const libraryMenu = new ActionRowBuilder()
161+
.addComponents(
162+
new StringSelectMenuBuilder()
163+
.setCustomId("select")
164+
.setOptions(options)
165+
);
166+
const libraryButtons = new ActionRowBuilder()
167+
.addComponents(
168+
new ButtonBuilder()
169+
.setCustomId("play")
170+
.setStyle(ButtonStyle.Primary)
171+
.setLabel(`${lang.BUTTON_PLAY}`),
172+
new ButtonBuilder()
173+
.setCustomId("remove")
174+
.setStyle(ButtonStyle.Danger)
175+
.setLabel(`${lang.BUTTON_LIBRARY_REMOVE}`)
176+
);
148177
await interaction.editReply({ embeds: [libraryEmbed], components: [libraryMenu, libraryButtons] });
149178
}
150179
}

0 commit comments

Comments
 (0)