Skip to content

Commit f0ed358

Browse files
committed
project structure
1 parent 5b3a2d2 commit f0ed358

13 files changed

+451
-255
lines changed

src/determine-deletions.ts

-61
This file was deleted.

src/discord/commands/config.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ApplicationCommandType, CommandInteraction } from "discord.js";
2+
3+
import { config } from "../../config";
4+
import { discordClient } from "../client";
5+
6+
discordClient.once("ready", () => {
7+
console.log("Creating config commands");
8+
discordClient.application?.commands.create({
9+
name: "config",
10+
description: "Show the current config",
11+
type: ApplicationCommandType.ChatInput,
12+
});
13+
});
14+
15+
discordClient.on("interactionCreate", async (interaction) => {
16+
if (!interaction.isCommand()) return;
17+
18+
const { commandName } = interaction;
19+
20+
if (commandName === "config") {
21+
await handleConfig(interaction);
22+
}
23+
});
24+
25+
async function handleConfig(interaction: CommandInteraction) {
26+
const configToShow = {
27+
votingPeriod: config.votingPeriod,
28+
gracePeriod: config.gracePeriod,
29+
immunityPeriod: config.immunityPeriod,
30+
onDraw: config.onDraw,
31+
dryRun: config.dryRun,
32+
};
33+
await interaction.reply({
34+
content: `Current config:\n${JSON.stringify(configToShow, null, 2)}`,
35+
ephemeral: true,
36+
});
37+
}

src/discord/commands/deleted.ts

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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);

src/discord/commands/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import "./config";
2+
import "./deleted";
3+
import "./subscribe";
4+
import "./whitelist";

src/discord/command-subscribe.ts src/discord/commands/subscribe.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ApplicationCommandType, CommandInteraction } from "discord.js";
22
import { eq } from "drizzle-orm";
33

4-
import { db } from "../db";
5-
import { subscribers } from "../db/schema/subscribers";
6-
import { discordClient } from "./client";
4+
import { db } from "../../db";
5+
import { subscribers } from "../../db/schema/subscribers";
6+
import { discordClient } from "../client";
77

88
discordClient.once("ready", () => {
99
console.log("Creating subscribe commands");

src/discord/commands/whitelist.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { ApplicationCommandType, CommandInteraction } from "discord.js";
2+
import { desc } from "drizzle-orm";
3+
4+
import { db } from "../../db";
5+
import { whitelist } from "../../db/schema/voting";
6+
import { discordClient } from "../client";
7+
8+
discordClient.once("ready", () => {
9+
console.log("Creating whitelist commands");
10+
discordClient.application?.commands.create({
11+
name: "listwhitelist",
12+
description: "List the latest whitelisted items",
13+
type: ApplicationCommandType.ChatInput,
14+
});
15+
});
16+
17+
discordClient.on("interactionCreate", async (interaction) => {
18+
if (!interaction.isCommand()) return;
19+
20+
const { commandName } = interaction;
21+
22+
if (commandName === "listwhitelist") {
23+
await handleListWhitelist(interaction);
24+
}
25+
});
26+
27+
async function handleListWhitelist(interaction: CommandInteraction) {
28+
const latestWhitelistedItems = await db.query.whitelist.findMany({
29+
limit: 10,
30+
orderBy: [desc(whitelist.createdAt)],
31+
with: {
32+
mediaItem: true,
33+
},
34+
});
35+
36+
if (latestWhitelistedItems.length === 0) {
37+
await interaction.reply({
38+
content: "There are no whitelisted items.",
39+
ephemeral: true,
40+
});
41+
return;
42+
}
43+
44+
const itemList = latestWhitelistedItems
45+
.map((item, index) => `${index + 1}. ${item.mediaItem.title}`)
46+
.join("\n");
47+
48+
await interaction.reply({
49+
content: `Latest whitelisted items:\n${itemList}`,
50+
ephemeral: true,
51+
});
52+
}

src/discord/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from "./command-subscribe";
1+
import "./commands";
2+
23
export * from "./client";

0 commit comments

Comments
 (0)