Files
discord-rpg-concept/bot/commands/quest/quests.ts

75 lines
2.9 KiB
TypeScript

import { createCommand } from "@shared/lib/utils";
import { SlashCommandBuilder, MessageFlags, ComponentType } from "discord.js";
import { questService } from "@shared/modules/quest/quest.service";
import { createSuccessEmbed, createWarningEmbed } from "@lib/embeds";
import { getQuestListEmbed, getAvailableQuestsEmbed, getQuestActionRows } from "@/modules/quest/quest.view";
export const quests = createCommand({
data: new SlashCommandBuilder()
.setName("quests")
.setDescription("View your active and available quests"),
execute: async (interaction) => {
const response = await interaction.deferReply({ flags: MessageFlags.Ephemeral });
const userId = interaction.user.id;
const updateView = async (viewType: 'active' | 'available') => {
const userQuests = await questService.getUserQuests(userId);
const availableQuests = await questService.getAvailableQuests(userId);
const embed = viewType === 'active'
? getQuestListEmbed(userQuests)
: getAvailableQuestsEmbed(availableQuests);
const components = getQuestActionRows(viewType, availableQuests);
await interaction.editReply({
embeds: [embed],
components: components
});
};
// Initial view
await updateView('active');
const collector = response.createMessageComponentCollector({
time: 60000,
componentType: undefined // Allow both buttons and select menu
});
collector.on('collect', async (i) => {
if (i.user.id !== interaction.user.id) return;
try {
if (i.customId === "quest_view_active") {
await i.deferUpdate();
await updateView('active');
} else if (i.customId === "quest_view_available") {
await i.deferUpdate();
await updateView('available');
} else if (i.customId === "quest_accept_select") {
const questId = parseInt((i as any).values[0]);
await questService.assignQuest(userId, questId);
await i.reply({
embeds: [createSuccessEmbed(`You have accepted a new quest!`, "Quest Accepted")],
flags: MessageFlags.Ephemeral
});
await updateView('active');
}
} catch (error) {
console.error("Quest interaction error:", error);
await i.followUp({
content: "Something went wrong while processing your quest interaction.",
flags: MessageFlags.Ephemeral
});
}
});
collector.on('end', () => {
interaction.editReply({ components: [] }).catch(() => {});
});
}
});