From 3eed1857bec1d90d4b46f8eb36f41921ce64ec32 Mon Sep 17 00:00:00 2001 From: MattIPv4 Date: Thu, 16 Nov 2023 00:11:03 +0000 Subject: [PATCH] Add total command with core stats --- src/commands/total.ts | 102 ++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 3 +- tsup.config.ts | 3 +- 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/commands/total.ts diff --git a/src/commands/total.ts b/src/commands/total.ts new file mode 100644 index 0000000..f0e775e --- /dev/null +++ b/src/commands/total.ts @@ -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 = { + 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 " + }`, + ].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; diff --git a/src/index.ts b/src/index.ts index 1726724..ef2846e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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>; @@ -11,7 +12,7 @@ const worker: ExportedHandler = { fetch: async (request, env, ctx) => { // Create the handler if it doesn't exist yet handler ??= createHandler( - [pingCommand, statsCommand], + [pingCommand, statsCommand, totalCommand], [pingComponent], env.DISCORD_PUBLIC_KEY, true, diff --git a/tsup.config.ts b/tsup.config.ts index e20ea12..a1864bb 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -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" }); @@ -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, );