|
| 1 | +import dayjs from "dayjs"; |
| 2 | +import relativeTime from "dayjs/plugin/relativeTime"; |
| 3 | +import { ApplicationCommandType, CommandInteraction } from "discord.js"; |
| 4 | +import { desc, isNotNull, isNull } from "drizzle-orm"; |
| 5 | + |
| 6 | +import { db } from "../../db"; |
| 7 | +import { itemsToDelete } from "../../db/schema/voting"; |
| 8 | +import { discordClient } from "../client"; |
| 9 | + |
| 10 | +discordClient.once("ready", () => { |
| 11 | + console.log("Creating whitelist commands"); |
| 12 | + discordClient.application?.commands.create({ |
| 13 | + name: "listdeleted", |
| 14 | + description: "List the latest deleted items", |
| 15 | + type: ApplicationCommandType.ChatInput, |
| 16 | + }); |
| 17 | + discordClient.application?.commands.create({ |
| 18 | + name: "listtobedeleted", |
| 19 | + description: "List the items that are scheduled to be deleted", |
| 20 | + type: ApplicationCommandType.ChatInput, |
| 21 | + }); |
| 22 | +}); |
| 23 | + |
| 24 | +discordClient.on("interactionCreate", async (interaction) => { |
| 25 | + if (!interaction.isCommand()) return; |
| 26 | + |
| 27 | + const { commandName } = interaction; |
| 28 | + |
| 29 | + if (commandName === "listdeleted") { |
| 30 | + await handleListDeleted(interaction); |
| 31 | + } |
| 32 | + if (commandName === "listtobedeleted") { |
| 33 | + await handleListToBeDeleted(interaction); |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +async function handleListDeleted(interaction: CommandInteraction) { |
| 38 | + const latestDeletedItems = await db.query.itemsToDelete.findMany({ |
| 39 | + limit: 10, |
| 40 | + orderBy: [desc(itemsToDelete.createdAt)], |
| 41 | + where: isNotNull(itemsToDelete.deletedAt), |
| 42 | + with: { |
| 43 | + mediaItem: true, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + if (latestDeletedItems.length === 0) { |
| 48 | + await interaction.reply({ |
| 49 | + content: "There are no deleted items.", |
| 50 | + ephemeral: true, |
| 51 | + }); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const itemList = latestDeletedItems |
| 56 | + .map((item, index) => `${index + 1}. ${item.mediaItem.title}`) |
| 57 | + .join("\n"); |
| 58 | + |
| 59 | + await interaction.reply({ |
| 60 | + content: `Latest deleted items:\n${itemList}`, |
| 61 | + ephemeral: true, |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +async function handleListToBeDeleted(interaction: CommandInteraction) { |
| 66 | + const itemsToBeDeleted = await db.query.itemsToDelete.findMany({ |
| 67 | + where: isNull(itemsToDelete.deletedAt), |
| 68 | + with: { |
| 69 | + mediaItem: true, |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + if (itemsToBeDeleted.length === 0) { |
| 74 | + await interaction.reply({ |
| 75 | + content: "There are no items scheduled to be deleted.", |
| 76 | + ephemeral: true, |
| 77 | + }); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const itemList = itemsToBeDeleted |
| 82 | + .map((item, index) => { |
| 83 | + const timeLeft = dayjs(item.deleteAfter).fromNow(); |
| 84 | + return `${index + 1}. ${item.mediaItem.title} (${timeLeft})`; |
| 85 | + }) |
| 86 | + .join("\n"); |
| 87 | + |
| 88 | + await interaction.reply({ |
| 89 | + content: `Items scheduled to be deleted:\n${itemList}`, |
| 90 | + ephemeral: true, |
| 91 | + }); |
| 92 | +} |
| 93 | +dayjs.extend(relativeTime); |
0 commit comments