Skip to content

Commit

Permalink
add /nextvote command
Browse files Browse the repository at this point in the history
  • Loading branch information
benjick committed Oct 11, 2024
1 parent 21dfba1 commit a411e5e
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"license": "ISC",
"dependencies": {
"@types/node-cron": "^3.0.11",
"cron-schedule": "^5.0.4",
"dayjs": "^1.11.13",
"discord.js": "^14.16.2",
"dotenv": "^16.4.5",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function runMigrations() {
console.log("✅ Migrations complete!");
} else {
console.log(
"🏃‍♂️ Skipping migrations in dev mode - run manually with 'pnpm db:push'",
"🏃 Skipping migrations in dev mode - run manually with 'pnpm db:push'",
);
}
}
3 changes: 2 additions & 1 deletion src/discord/commands/deleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { db } from "../../db";
import { itemsToDelete } from "../../db/schema/voting";
import { discordClient } from "../client";

dayjs.extend(relativeTime);

discordClient.once("ready", () => {
discordClient.application?.commands.create({
name: "listdeleted",
Expand Down Expand Up @@ -89,4 +91,3 @@ async function handleListToBeDeleted(interaction: CommandInteraction) {
ephemeral: true,
});
}
dayjs.extend(relativeTime);
1 change: 1 addition & 0 deletions src/discord/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./config";
import "./deleted";
import "./help";
import "./next-vote";
import "./subscribe";
import "./whitelist";
15 changes: 15 additions & 0 deletions src/discord/commands/next-vote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { parseCronExpression } from "cron-schedule";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import { describe, expect, it } from "vitest";

dayjs.extend(relativeTime);

describe("next vote", () => {
it("should calculate the correct time until the next vote", () => {
const cron = parseCronExpression("30 * * * *");
const nextVote = cron.getNextDate(new Date("2024-10-11T12:00:00Z"));
const timeLeft = dayjs(nextVote).fromNow();
expect(timeLeft).toBe("in 5 hours");
});
});
38 changes: 38 additions & 0 deletions src/discord/commands/next-vote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { parseCronExpression } from "cron-schedule";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import { ApplicationCommandType, CommandInteraction } from "discord.js";

import { config } from "../../config";
import { discordClient } from "../client";

dayjs.extend(relativeTime);

discordClient.once("ready", () => {
discordClient.application?.commands.create({
name: "nextvote",
description: "Show the time until the next vote",
type: ApplicationCommandType.ChatInput,
});
console.log("✅ Next vote command registered");
});

discordClient.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;

const { commandName } = interaction;

if (commandName === "nextvote") {
await handleNextVote(interaction);
}
});

async function handleNextVote(interaction: CommandInteraction) {
const cron = parseCronExpression(config.cron.findMedia);
const nextVote = cron.getNextDate();
const timeLeft = dayjs(nextVote).fromNow();
await interaction.reply({
content: `The next vote is ${timeLeft} (${nextVote.toISOString()})`,
ephemeral: true,
});
}
1 change: 1 addition & 0 deletions src/envSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("envSchema", () => {
const res = envSchema.parse({
DISCORD_BOT_TOKEN: "process.env.DISCORD_BOT_TOKEN",
CONFIG_PATH: "process.env.CONFIG_PATH",
DATABASE_URL: "postgresql://postgres:postgres@db:5432/postgres",
});
expect(res.NODE_ENV).toBe("development");
expect(res.DRY_RUN).toBe(false);
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
reporters: ["default", "html"],
env: {
NODE_ENV: "test",
DATABASE_URL: "postgresql://postgres:postgres@db:5432/postgres",
},
},
});

0 comments on commit a411e5e

Please sign in to comment.