From 5317b1e3e2c26c62b7179ddb3c9284fc1f7cfec3 Mon Sep 17 00:00:00 2001 From: Sam Gaus Date: Thu, 15 Aug 2024 11:31:38 -0700 Subject: [PATCH] Add raffle command --- packages/kol.js/src/Player.ts | 4 ++ packages/oaf/src/commands/kol/raffle.ts | 79 +++++++++++++++++++++++++ packages/oaf/src/commands/misc/daily.ts | 1 - packages/oaf/src/commands/wiki/item.ts | 21 ++++--- 4 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 packages/oaf/src/commands/kol/raffle.ts diff --git a/packages/kol.js/src/Player.ts b/packages/kol.js/src/Player.ts index 51e0bb2..7a3f824 100644 --- a/packages/kol.js/src/Player.ts +++ b/packages/kol.js/src/Player.ts @@ -208,4 +208,8 @@ export class Player { response?.output.includes("This player is currently online") ?? false ); } + + toString() { + return `${this.name} (#${this.id})`; + } } diff --git a/packages/oaf/src/commands/kol/raffle.ts b/packages/oaf/src/commands/kol/raffle.ts new file mode 100644 index 0000000..22d34cb --- /dev/null +++ b/packages/oaf/src/commands/kol/raffle.ts @@ -0,0 +1,79 @@ +import { + ChatInputCommandInteraction, + SlashCommandBuilder, + heading, +} from "discord.js"; + +import { createEmbed, discordClient } from "../../clients/discord.js"; +import { kolClient } from "../../clients/kol.js"; +import { wikiClient } from "../../clients/wiki.js"; +import { config } from "../../config.js"; +import { embedForItem } from "../wiki/item.js"; + +export const data = new SlashCommandBuilder() + .setName("raffle") + .setDescription("See today's raffle prizes and yesterday's winners"); + +const numberFormat = new Intl.NumberFormat(); + +async function getRaffleEmbeds() { + const raffle = await kolClient.getRaffle(); + + const embeds = []; + + let first = raffle.today.first && (await embedForItem(raffle.today.first)); + if (!first) + first = createEmbed().setTitle(`Unknown item (#${raffle.today.first})`); + first.setTitle(`First Prize: ${first.toJSON().title}`); + embeds.push(first); + + let second = raffle.today.second && (await embedForItem(raffle.today.second)); + if (!second) + second = createEmbed().setTitle(`Unknown item (#${raffle.today.second})`); + second.setTitle(`Second Prize: ${second.toJSON().title}`); + embeds.push(second); + + const winners = createEmbed().setTitle(`Yesterday's Winners`); + + winners.addFields( + raffle.yesterday.map((winner) => { + const itemName = + wikiClient.items.find((i) => i.id === winner.item)?.name ?? + `Unknown item (#${winner.item})`; + return { + name: itemName, + value: `${winner.player.toString()} (${numberFormat.format(winner.tickets)} tickets)`, + }; + }), + ); + + embeds.push(winners); + return embeds; +} + +export async function execute(interaction: ChatInputCommandInteraction) { + await interaction.deferReply(); + const embeds = await getRaffleEmbeds(); + await interaction.editReply({ content: null, embeds }); +} + +async function onRollover() { + const guild = await discordClient.guilds.fetch(config.GUILD_ID); + const announcementChannel = guild?.channels.cache.get( + config.ANNOUNCEMENTS_CHANNEL_ID, + ); + + if (!announcementChannel?.isTextBased()) { + return; + } + + await announcementChannel.send({ + content: heading("Raffle"), + embeds: await getRaffleEmbeds(), + allowedMentions: { users: [] }, + }); +} + +export async function init() { + kolClient.on("rollover", onRollover); +} diff --git a/packages/oaf/src/commands/misc/daily.ts b/packages/oaf/src/commands/misc/daily.ts index 9f88aae..006ae25 100644 --- a/packages/oaf/src/commands/misc/daily.ts +++ b/packages/oaf/src/commands/misc/daily.ts @@ -53,7 +53,6 @@ async function onRollover() { ); if (!announcementChannel?.isTextBased()) { - await discordClient.alert("No valid announcement channel"); return; } diff --git a/packages/oaf/src/commands/wiki/item.ts b/packages/oaf/src/commands/wiki/item.ts index acfb23e..a3e96ec 100644 --- a/packages/oaf/src/commands/wiki/item.ts +++ b/packages/oaf/src/commands/wiki/item.ts @@ -13,26 +13,29 @@ export const data = new SlashCommandBuilder() .setDescription("Get information about the given item.") .addIntegerOption(itemOption()); +export async function embedForItem(id: number) { + const item = wikiClient.items.find((i) => i.id === id); + if (!item) return null; + const embed = createEmbed(); + embed.setTitle(item.name).setURL(await wikiClient.getWikiLink(item)); + await item.addToEmbed(embed); + return embed; +} + export async function execute(interaction: ChatInputCommandInteraction) { const itemId = interaction.options.getInteger("item", true); await interaction.deferReply(); - const item = wikiClient.items.find((i) => i.id === itemId); + const embed = await embedForItem(itemId); - const embed = createEmbed(); - - if (!item) { + if (!embed) { await interaction.editReply({ content: null, - embeds: [embed.setDescription("Item could not be found.")], + embeds: [createEmbed().setDescription("Item could not be found.")], }); return; } - embed.setTitle(item.name).setURL(await wikiClient.getWikiLink(item)); - - await item.addToEmbed(embed); - await interaction.editReply({ content: null, embeds: [embed],