Skip to content

Commit

Permalink
Fix error handling and use safeReply utility
Browse files Browse the repository at this point in the history
  • Loading branch information
eritislami committed Mar 7, 2024
1 parent 7bd0938 commit 7ecf9f6
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 69 deletions.
8 changes: 3 additions & 5 deletions commands/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";
import { safeReply } from "../utils/safeReply";

export default {
data: new SlashCommandBuilder().setName("loop").setDescription(i18n.__("loop.description")),
Expand All @@ -17,11 +18,8 @@ export default {

queue.loop = !queue.loop;

const content = {
content: i18n.__mf("loop.result", { loop: queue.loop ? i18n.__("common.on") : i18n.__("common.off") })
};
const content = i18n.__mf("loop.result", { loop: queue.loop ? i18n.__("common.on") : i18n.__("common.off") });

if (interaction.replied) interaction.followUp(content).catch(console.error);
else interaction.reply(content).catch(console.error);
safeReply(interaction, content);
}
};
6 changes: 3 additions & 3 deletions commands/pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";
import { safeReply } from "../utils/safeReply";

export default {
data: new SlashCommandBuilder().setName("pause").setDescription(i18n.__("pause.description")),
Expand All @@ -14,10 +15,9 @@ export default {
if (!canModifyQueue(guildMemer!)) return i18n.__("common.errorNotChannel");

if (queue.player.pause()) {
const content = { content: i18n.__mf("pause.result", { author: interaction.user.id }) };
const content = i18n.__mf("pause.result", { author: interaction.user.id });

if (interaction.replied) interaction.followUp(content).catch(console.error);
else interaction.reply(content).catch(console.error);
safeReply(interaction, content);

return true;
}
Expand Down
118 changes: 71 additions & 47 deletions commands/queue.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ChatInputCommandInteraction,
CommandInteraction,
EmbedBuilder,
MessageReaction,
PermissionsBitField,
SlashCommandBuilder,
TextChannel,
User
Interaction,
SlashCommandBuilder
} from "discord.js";
import { bot } from "../index";
import { Song } from "../structs/Song";
Expand All @@ -15,65 +15,89 @@ import { i18n } from "../utils/i18n";
export default {
data: new SlashCommandBuilder().setName("queue").setDescription(i18n.__("queue.description")),
cooldown: 5,
permissions: [PermissionsBitField.Flags.AddReactions, PermissionsBitField.Flags.ManageMessages],
permissions: [],
async execute(interaction: ChatInputCommandInteraction) {
const queue = bot.queues.get(interaction.guild!.id);
if (!queue || !queue.songs.length) return interaction.reply({ content: i18n.__("queue.errorNotQueue") });

let currentPage = 0;
const embeds = generateQueueEmbed(interaction, queue.songs);

const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().setCustomId("previous").setLabel("⬅️").setStyle(ButtonStyle.Secondary),
new ButtonBuilder().setCustomId("stop").setLabel("⏹").setStyle(ButtonStyle.Secondary),
new ButtonBuilder().setCustomId("next").setLabel("➡️").setStyle(ButtonStyle.Secondary)
);

await interaction.reply("⏳ Loading queue...");

if (interaction.replied)
await interaction.editReply({
content: `**${i18n.__mf("queue.currentPage")} ${currentPage + 1}/${embeds.length}**`,
embeds: [embeds[currentPage]]
embeds: [embeds[currentPage]],
components: [row]
});

const queueEmbed = await interaction.fetchReply();

try {
await queueEmbed.react("⬅️");
await queueEmbed.react("⏹");
await queueEmbed.react("➡️");
} catch (error: any) {
console.error(error);
(interaction.channel as TextChannel).send(error.message).catch(console.error);
}

const filter = (reaction: MessageReaction, user: User) =>
["⬅️", "⏹", "➡️"].includes(reaction.emoji.name!) && interaction.user.id === user.id;

const collector = queueEmbed.createReactionCollector({ filter, time: 60000 });

collector.on("collect", async (reaction, user) => {
try {
if (reaction.emoji.name === "➡️") {
if (currentPage < embeds.length - 1) {
currentPage++;
queueEmbed.edit({
content: i18n.__mf("queue.currentPage", { page: currentPage + 1, length: embeds.length }),
embeds: [embeds[currentPage]]
});
}
} else if (reaction.emoji.name === "⬅️") {
if (currentPage !== 0) {
--currentPage;
queueEmbed.edit({
content: i18n.__mf("queue.currentPage", { page: currentPage + 1, length: embeds.length }),
embeds: [embeds[currentPage]]
});
}
} else {
collector.stop();
reaction.message.reactions.removeAll();
}
await reaction.users.remove(interaction.user.id);
} catch (error: any) {
console.error(error);
return (interaction.channel as TextChannel).send(error.message).catch(console.error);
const filter = (buttonInteraction: Interaction) =>
buttonInteraction.isButton() && buttonInteraction.user.id === interaction.user.id;

const collector = queueEmbed.createMessageComponentCollector({ filter, time: 60000 });

const buttonHandlers = {
next: async () => {
if (currentPage >= embeds.length - 1) return;

currentPage++;

await interaction.editReply({
content: `**${i18n.__mf("queue.currentPage", {
page: currentPage + 1,
length: embeds.length
})}**`,
embeds: [embeds[currentPage]],
components: [row]
});
},
previous: async () => {
if (currentPage === 0) return;

currentPage--;
await interaction.editReply({
content: `**${i18n.__mf("queue.currentPage", {
page: currentPage + 1,
length: embeds.length
})}**`,
embeds: [embeds[currentPage]],
components: [row]
});
},
stop: async () => {
await interaction.editReply({
components: []
});

collector.stop();
}
};

collector.on("collect", async (buttonInteraction) => {
buttonInteraction.deferUpdate();

const handler = buttonHandlers[buttonInteraction.customId as keyof typeof buttonHandlers];

if (handler) {
await handler();
}
});

collector.on("end", () => {
queueEmbed
.edit({
components: []
})
.catch(console.error);
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export default {
if (!removeArgs)
return interaction.reply({ content: i18n.__mf("remove.usageReply", { prefix: bot.prefix }), ephemeral: true });

const songs = removeArgs.split(",").map((arg: any) => parseInt(arg));
const songs = removeArgs.split(",").map((arg) => parseInt(arg));

let removed: Song[] = [];

if (pattern.test(removeArgs)) {
queue.songs = queue.songs.filter((item, index) => {
if (songs.find((songIndex: any) => songIndex - 1 === index)) removed.push(item);
if (songs.find((songIndex) => songIndex - 1 === index)) removed.push(item);
else return true;
});

Expand Down
12 changes: 6 additions & 6 deletions commands/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";
import { safeReply } from "../utils/safeReply";

export default {
data: new SlashCommandBuilder().setName("resume").setDescription(i18n.__("resume.description")),
Expand All @@ -15,18 +16,17 @@ export default {
if (!canModifyQueue(guildMemer!)) return i18n.__("common.errorNotChannel");

if (queue.player.unpause()) {
const content = { content: i18n.__mf("resume.resultNotPlaying", { author: interaction.user.id }) };
const content = i18n.__mf("resume.resultNotPlaying", { author: interaction.user.id });

if (interaction.replied) interaction.followUp(content).catch(console.error);
else interaction.reply(content).catch(console.error);
safeReply(interaction, content);

return true;
}

const content = { content: i18n.__("resume.errorPlaying") };
const content = i18n.__("resume.errorPlaying");

safeReply(interaction, content);

if (interaction.replied) interaction.followUp(content).catch(console.error);
else interaction.reply(content).catch(console.error);
return false;
}
};
2 changes: 1 addition & 1 deletion commands/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {

try {
results = await youtube.search(search, { limit: 10, type: "video" });
} catch (error: any) {
} catch (error) {
console.error(error);
interaction.editReply({ content: i18n.__("common.errorCommand") }).catch(console.error);
return;
Expand Down
6 changes: 3 additions & 3 deletions commands/shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";
import { safeReply } from "../utils/safeReply";

export default {
data: new SlashCommandBuilder().setName("shuffle").setDescription(i18n.__("shuffle.description")),
Expand All @@ -23,9 +24,8 @@ export default {

queue.songs = songs;

const content = { content: i18n.__mf("shuffle.result", { author: interaction.user.id }) };
const content = i18n.__mf("shuffle.result", { author: interaction.user.id });

if (interaction.replied) interaction.followUp(content).catch(console.error);
else interaction.reply(content).catch(console.error);
safeReply(interaction, content);
}
};
3 changes: 2 additions & 1 deletion commands/skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";
import { safeReply } from "../utils/safeReply";

export default {
data: new SlashCommandBuilder().setName("skip").setDescription(i18n.__("skip.description")),
Expand All @@ -15,6 +16,6 @@ export default {

queue.player.stop(true);

interaction.reply({ content: i18n.__mf("skip.result", { author: interaction.user.id }) }).catch(console.error);
safeReply(interaction, i18n.__mf("skip.result", { author: interaction.user.id }));
}
};
3 changes: 2 additions & 1 deletion commands/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";
import { safeReply } from "../utils/safeReply";

export default {
data: new SlashCommandBuilder().setName("stop").setDescription(i18n.__("stop.description")),
Expand All @@ -14,6 +15,6 @@ export default {

queue.stop();

interaction.reply({ content: i18n.__mf("stop.result", { author: interaction.user.id }) }).catch(console.error);
safeReply(interaction, i18n.__mf("stop.result", { author: interaction.user.id }));
}
};

0 comments on commit 7ecf9f6

Please sign in to comment.