forked from syntaxbullet/AuroraBot-discord
refactor: add quest view layer
Create quest.view.ts with UI logic extracted from quests command: - getQuestListEmbed() for quest log display - formatQuestRewards() helper for reward formatting - getQuestStatus() helper for status display Updated quests.ts to use view functions instead of inline embed building.
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { createCommand } from "@/lib/utils";
|
import { createCommand } from "@/lib/utils";
|
||||||
import { SlashCommandBuilder, MessageFlags } from "discord.js";
|
import { SlashCommandBuilder, MessageFlags } from "discord.js";
|
||||||
import { questService } from "@/modules/quest/quest.service";
|
import { questService } from "@/modules/quest/quest.service";
|
||||||
import { createWarningEmbed, createBaseEmbed } from "@lib/embeds";
|
import { createWarningEmbed } from "@lib/embeds";
|
||||||
|
import { getQuestListEmbed } from "@/modules/quest/quest.view";
|
||||||
|
|
||||||
export const quests = createCommand({
|
export const quests = createCommand({
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@@ -17,21 +18,7 @@ export const quests = createCommand({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const embed = createBaseEmbed("📜 Quest Log", undefined, "Blue");
|
const embed = getQuestListEmbed(userQuests);
|
||||||
|
|
||||||
userQuests.forEach(entry => {
|
|
||||||
const status = entry.completedAt ? "✅ Completed" : "In Progress";
|
|
||||||
const rewards = entry.quest.rewards as { xp?: number, balance?: number };
|
|
||||||
const rewardStr = [];
|
|
||||||
if (rewards?.xp) rewardStr.push(`${rewards.xp} XP`);
|
|
||||||
if (rewards?.balance) rewardStr.push(`${rewards.balance} 🪙`);
|
|
||||||
|
|
||||||
embed.addFields({
|
|
||||||
name: `${entry.quest.name} (${status})`,
|
|
||||||
value: `${entry.quest.description}\n**Rewards:** ${rewardStr.join(", ")}\n**Progress:** ${entry.progress}%`,
|
|
||||||
inline: false
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await interaction.editReply({ embeds: [embed] });
|
await interaction.editReply({ embeds: [embed] });
|
||||||
}
|
}
|
||||||
|
|||||||
54
src/modules/quest/quest.view.ts
Normal file
54
src/modules/quest/quest.view.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import { EmbedBuilder } from "discord.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quest entry with quest details and progress
|
||||||
|
*/
|
||||||
|
interface QuestEntry {
|
||||||
|
progress: number | null;
|
||||||
|
completedAt: Date | null;
|
||||||
|
quest: {
|
||||||
|
name: string;
|
||||||
|
description: string | null;
|
||||||
|
rewards: any;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats quest rewards object into a human-readable string
|
||||||
|
*/
|
||||||
|
function formatQuestRewards(rewards: { xp?: number, balance?: number }): string {
|
||||||
|
const rewardStr: string[] = [];
|
||||||
|
if (rewards?.xp) rewardStr.push(`${rewards.xp} XP`);
|
||||||
|
if (rewards?.balance) rewardStr.push(`${rewards.balance} 🪙`);
|
||||||
|
return rewardStr.join(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the quest status display string
|
||||||
|
*/
|
||||||
|
function getQuestStatus(completedAt: Date | null): string {
|
||||||
|
return completedAt ? "✅ Completed" : "📝 In Progress";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an embed displaying a user's quest log
|
||||||
|
*/
|
||||||
|
export function getQuestListEmbed(userQuests: QuestEntry[]): EmbedBuilder {
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle("📜 Quest Log")
|
||||||
|
.setColor(0x3498db); // Blue
|
||||||
|
|
||||||
|
userQuests.forEach(entry => {
|
||||||
|
const status = getQuestStatus(entry.completedAt);
|
||||||
|
const rewards = entry.quest.rewards as { xp?: number, balance?: number };
|
||||||
|
const rewardsText = formatQuestRewards(rewards);
|
||||||
|
|
||||||
|
embed.addFields({
|
||||||
|
name: `${entry.quest.name} (${status})`,
|
||||||
|
value: `${entry.quest.description}\n**Rewards:** ${rewardsText}\n**Progress:** ${entry.progress}%`,
|
||||||
|
inline: false
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return embed;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user