import { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, } from "discord.js"; import { moderatorRoles } from "../../../../config.js"; import dayjs from "dayjs"; const ns = "custom"; const buildCustomHistoryMessage = async ({ client, data, page, lng, userId, }) => { const perPage = 5; const totalPages = Math.ceil(data.length / perPage) || 1; if (page < 1) page = 1; if (page > totalPages) page = totalPages; const slice = data .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)) .slice((page - 1) * perPage, page * perPage); const embed = new EmbedBuilder() .setTitle("Auto-Approval History") .setColor("Blue"); if (!slice.length) { embed.setDescription("No data available."); } else { embed.setDescription( slice .map( (entry) => `**${entry.username || `<@${entry.userId}>`}** • ${dayjs( entry.createdAt ).format("DD MMM YYYY")} - ${ entry.status === "pass" ? "✔️ Pass" : "❌ Fail" }\n` + `\`\`\`Total point: ${entry.minScore}\nPercentage upvote: ${entry.percentageUpVotes}%\`\`\`` ) .join("\n\n") ); } const components = []; if (totalPages > 1) { const prev = new ButtonBuilder() .setCustomId(`custom-page-${page - 1}-${userId || "all"}`) .setEmoji("⬅️") .setStyle(ButtonStyle.Secondary) .setDisabled(page === 1); const next = new ButtonBuilder() .setCustomId(`custom-page-${page + 1}-${userId || "all"}`) .setEmoji("➡️") .setStyle(ButtonStyle.Secondary) .setDisabled(page === totalPages); components.push(new ActionRowBuilder().addComponents(prev, next)); } return { embeds: [embed], components }; }; export const slash = async ({ client, interaction, lng }) => { if ( !interaction.member.permissions.has("Administrator") && !interaction.member.roles.cache.some((r) => moderatorRoles.includes(r.id)) ) return interaction.reply("No permission", { ephemeral: true }); await interaction.deferReply(); const targetUser = interaction.options.getUser("user"); let query = { guildId: interaction.guildId }; if (targetUser) query.userId = targetUser.id; let data = await client.db.AutoApprovals.find(query); const oneYearAgo = dayjs().subtract(1, "year").toDate(); data = data.filter((d) => new Date(d.createdAt) >= oneYearAgo); if (!data.length) return interaction.editReply("No data found"); const messagePayload = await buildCustomHistoryMessage({ client, data, page: 1, lng, userId: targetUser?.id || null, }); await interaction.editReply(messagePayload); }; export const button = async ({ client, interaction, customId, lng }) => { if (!customId.startsWith("custom-page-")) return; try { if ( !interaction.member.permissions.has("Administrator") && !interaction.member.roles.cache.some((r) => moderatorRoles.includes(r.id)) ) { return interaction.reply({ content: "No permission", ephemeral: true }); } const [, , pageStr, userId] = customId.split("-"); const page = parseInt(pageStr, 10); if (isNaN(page) || page < 1) { return interaction.reply({ content: "Invalid page number.", ephemeral: true, }); } const query = { guildId: interaction.guildId }; if (userId && userId !== "all") query.userId = userId; let data = await client.db.AutoApprovals.find(query); const oneYearAgo = dayjs().subtract(1, "year").toDate(); data = data.filter((d) => new Date(d.createdAt) >= oneYearAgo); if (!data.length) { return interaction.reply({ content: "No data found.", ephemeral: true }); } const messagePayload = await buildCustomHistoryMessage({ client, data, page, lng, userId: userId !== "all" ? userId : null, }); console.log("Updating page", page, "for user", userId); await interaction.update(messagePayload); } catch (err) { console.error("Custom button error:", err); try { if (!interaction.replied && !interaction.deferred) { await interaction.reply({ content: "Something went wrong.", ephemeral: true, }); } else { await interaction.followUp({ content: "Something went wrong.", ephemeral: true, }); } } catch (followupError) { console.error("Error handling failed:", followupError); } } }; export const data = new SlashCommandBuilder() .setName("custom") .setDescription("View auto-approval history") .addUserOption((opt) => opt .setName("user") .setDescription("Check auto approval history of a specific user") .setRequired(false) );