Skip to content

Commit

Permalink
Add raffle command
Browse files Browse the repository at this point in the history
  • Loading branch information
gausie committed Aug 15, 2024
1 parent 16e38b7 commit 5317b1e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/kol.js/src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,8 @@ export class Player<IsFull extends boolean = boolean> {
response?.output.includes("This player is currently online") ?? false
);
}

toString() {
return `${this.name} (#${this.id})`;
}
}
79 changes: 79 additions & 0 deletions packages/oaf/src/commands/kol/raffle.ts
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion packages/oaf/src/commands/misc/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ async function onRollover() {
);

if (!announcementChannel?.isTextBased()) {
await discordClient.alert("No valid announcement channel");
return;
}

Expand Down
21 changes: 12 additions & 9 deletions packages/oaf/src/commands/wiki/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 5317b1e

Please sign in to comment.