Skip to content

Commit

Permalink
Add total command with core stats
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Nov 16, 2023
1 parent 0e81072 commit 3eed185
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
102 changes: 102 additions & 0 deletions src/commands/total.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { InteractionResponseType } from "discord-api-types/payloads";
import type { Command } from "workers-discord";

import getStats from "../util/stats";
import checkDate from "../util/check";
import { bold, italic, money, number } from "../util/format";
import type { CtxWithEnv } from "../env";

const totalCommand: Command<CtxWithEnv> = {
name: "total",
description:
"Check the total raised for Jingle Jam, and how many bundles have been claimed.",
execute: ({ response, wait, edit, context }) => {
wait(
(async () => {
const stats = await getStats(context.env.STATS_API_ENDPOINT);

// Check if Jingle Jam is running
const start = new Date(stats.event.start);
const check = checkDate(start);
// TODO: Re-enable this check once testing is done
// if (check) return edit({ content: check });

// Check if Jingle Jam has finished
const end = new Date(stats.event.end);
if (isNaN(+end)) throw new Error("Invalid end date");

// Time since launch
// TODO: Switch back to using the current time once testing is done
// const now = new Date();
const now = end;
const ended = now >= end;

// Format some stats
const totalRaised = bold(
money(
"£",
stats.raised.yogscast + stats.raised.fundraisers,
),
);
const totalYogscast = bold(money("£", stats.raised.yogscast));
const totalFundraisers = bold(
money("£", stats.raised.fundraisers),
);

const historyRaised = bold(
money(
"£",
stats.raised.yogscast +
stats.raised.fundraisers +
stats.history.reduce(
(total, year) => total + year.total.pounds,
0,
),
),
);

const bundles = bold(number(stats.collections.redeemed));

await edit({
content: [
`:snowflake: ${totalRaised} ${
ended ? "was" : "has been"
} raised for charity (Yogscast: ${totalYogscast}, fundraisers: ${totalFundraisers}) during Jingle Jam ${
stats.event.year
}${ended ? "!" : " so far!"} `,
`:package: ${bundles} bundles ${
ended ? "were" : "have been"
} redeemed, and over all the years, we've now raised ${historyRaised} for charity!`,
`:heart: Thank you for supporting some wonderful causes! ${
ended
? `We look forward to seeing you again for Jingle Jam ${
stats.event.year + 1
}.`
: "Get involved at <https://jinglejam.tiltify.com>"
}`,
].join("\n"),
});
})().catch(async (error) => {
console.error(error);

await edit({
content: [
":pensive: Sorry, an error occurred while fetching the stats.",
italic(
"An ~~angry~~ polite message has been sent to the team letting them know.",
),
].join("\n"),
});
}),
);

return response({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: ":mag: Fetching the latest Jingle Jam stats...",
},
});
},
};

export default totalCommand;
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createHandler } from "workers-discord";
import pingCommand from "./commands/ping";
import pingComponent from "./components/ping";
import statsCommand from "./commands/stats";
import totalCommand from "./commands/total";
import type { CtxWithEnv, Env } from "./env";

let handler: ReturnType<typeof createHandler<CtxWithEnv>>;
Expand All @@ -11,7 +12,7 @@ const worker: ExportedHandler<Env> = {
fetch: async (request, env, ctx) => {
// Create the handler if it doesn't exist yet
handler ??= createHandler<CtxWithEnv>(
[pingCommand, statsCommand],
[pingCommand, statsCommand, totalCommand],
[pingComponent],
env.DISCORD_PUBLIC_KEY,
true,
Expand Down
3 changes: 2 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dotenv from "dotenv";

import pingCommand from "./src/commands/ping";
import statsCommand from "./src/commands/stats";
import totalCommand from "./src/commands/total";

dotenv.config({ path: ".dev.vars" });

Expand All @@ -19,7 +20,7 @@ export default defineConfig({
await registerCommands(
process.env.DISCORD_CLIENT_ID!,
process.env.DISCORD_CLIENT_SECRET!,
[pingCommand, statsCommand],
[pingCommand, statsCommand, totalCommand],
true,
process.env.DISCORD_GUILD_ID,
);
Expand Down

0 comments on commit 3eed185

Please sign in to comment.