Upcoming elections command

This commit is contained in:
dartz 2024-07-28 21:43:41 -04:00
parent a0a5c37faf
commit 05a2e2a138
2 changed files with 82 additions and 1 deletions

View file

@ -207,6 +207,17 @@ const commands = [
})
},
],
},
{
name: 'upcoming',
description: "View upcoming things",
options: [
{
name: 'elections',
description: 'View upcoming elections',
type: ApplicationCommandOptionType.Subcommand
}
],
}
];

View file

@ -1,4 +1,4 @@
import {REST, Routes, Client, Guild, GatewayIntentBits, CommandInteraction, EmbedBuilder, CommandInteractionOption, CommandInteractionOptionResolver } from "discord.js";
import {ActionRowBuilder, ButtonBuilder, ButtonStyle, REST, Routes, Client, Guild, GatewayIntentBits, CommandInteraction, EmbedBuilder, CommandInteractionOption, CommandInteractionOptionResolver } from "discord.js";
import * as fs from "node:fs";
import commands from "./commands.js";
import {MakePrediction} from "./predictor.js";
@ -285,6 +285,76 @@ if (!config.token) {
.setImage("attachment://election.png")
.setTimestamp();
await i.editReply({embeds: [embed], files: [{attachment: result.png, name: "election.png"}]});
case "upcoming":
console.log('\x1b[33m', `Upcoming elections command executed by ${i.user.username} (${i.user.id})`, '\x1b[0m');
function parseDate(dateStr: string) {
return new Date(Date.parse(dateStr + " 2024"));
}
const elections = JSON.parse(fs.readFileSync(`data/upcoming_elections.json`, "utf-8"));
const currentDate = new Date();
const electionsArray = Object.keys(elections).map(key => ({
name: key,
date: parseDate(elections[key].date),
dateString: elections[key].date
})).filter(election => election.date >= currentDate);
electionsArray.sort((a, b) => a.date.getTime() - b.date.getTime());
function generatePageEmbed(electionsArray: { name: string; date: Date; dateString: string }[], page: number): EmbedBuilder {
return new EmbedBuilder()
.setTitle("Upcoming elections")
.setFooter({ text: `Page ${page + 1}/${Math.ceil(electionsArray.length / 6)}`})
.setDescription(
electionsArray.slice(page * 6, page * 6 + 6)
.map((election) => `:green_circle: ${election.dateString} - **${election.name}**`)
.join('\n')
)
.setTimestamp();
}
const maxPage = Math.ceil(electionsArray.length / 6) - 1;
let currentPage = 0;
const pageButtons = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder().setCustomId('electionsPrev').setLabel('⏮️').setStyle(ButtonStyle.Primary),
new ButtonBuilder().setCustomId('electionsNext').setLabel('⏭️').setStyle(ButtonStyle.Primary)
);
const message = await i.editReply({
embeds: [generatePageEmbed(electionsArray, currentPage)],
components: [pageButtons]
});
const buttonCollector = message.createMessageComponentCollector({
filter: i => i.user.id === i.user.id,
time: 1000 * 60 * 2
});
buttonCollector.on('collect', async (i) => {
switch (i.customId) {
case 'electionsPrev':
currentPage = currentPage === 0 ? maxPage : currentPage - 1;
break;
case 'electionsNext':
currentPage = currentPage === maxPage ? 0 : currentPage + 1;
break;
}
await i.update({embeds: [generatePageEmbed(electionsArray, currentPage)], components: [pageButtons]});
});
buttonCollector.on('end', async () => {
try {
await message.edit({ components: [] });
} catch (e) {
// this seems to fail in DMs for some reason...?
console.log('\x1b[31m', 'Could not delete buttons in DM!', '\x1b[0m');
}
});
break;
}
}
});