support viewing state results

This commit is contained in:
dartz 2024-07-28 01:45:10 -04:00
parent c0a3b8e05a
commit 3d8b63e525
2 changed files with 19 additions and 4 deletions

View file

@ -45,8 +45,14 @@ const commands = [
{ {
name: 'year', name: 'year',
description: 'The year to view', description: 'The year to view',
type: ApplicationCommandOptionType.Number, type: ApplicationCommandOptionType.String,
required: true required: true
},
{
name: 'state',
description: 'The state to view',
type: ApplicationCommandOptionType.String,
required: false
} }
], ],
}, },

View file

@ -100,7 +100,12 @@ if (!config.token) {
var country = i.options.get('country')?.value as string var country = i.options.get('country')?.value as string
var election_type = i.options.get('type')?.value as string var election_type = i.options.get('type')?.value as string
var year = Number(i.options.get('year')?.value) var year = Number(i.options.get('year')?.value)
let query = await fetch(`http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}`); // temp test url var state = i.options.get('state')?.value as string
// check if the state option exists when querying
const queryURL = state
? `http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}/${state}`
: `http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}`;
const query = await fetch(queryURL);
let results = await query.json(); let results = await query.json();
if (query.ok) { if (query.ok) {
@ -110,13 +115,17 @@ if (!config.token) {
.setDescription("API test") .setDescription("API test")
.addFields(Object.values(results[year][0].candidates).map((candidate: any) => ({ .addFields(Object.values(results[year][0].candidates).map((candidate: any) => ({
name: `${candidate.winner ? ":white_check_mark:" : ""} ${CandidateEmojis[candidate.party] ?? ""} ${candidate.name} ${candidate.incumbent ? "(I)" : ""} (${candidate.party})`, name: `${candidate.winner ? ":white_check_mark:" : ""} ${CandidateEmojis[candidate.party] ?? ""} ${candidate.name} ${candidate.incumbent ? "(I)" : ""} (${candidate.party})`,
value: `${results[year][0].election_type === "electoral" ? `${candidate.electoral_votes} electoral votes\n` : ""}${results[year][0].election_type === "parliament" ? `${candidate.seats_won} seats\n` : ""}${candidate.states_carried !== null && candidate.states_carried !== undefined ? `${candidate.states_carried} states carried\n` : ""}${candidate.delegates !== null && candidate.delegates !== undefined ? `${candidate.delegates} delegates\n` : ""}${candidate.votes !== null && candidate.votes !== undefined ? `${candidate.votes.toLocaleString()} votes (${candidate.percent}%)` : ""}`, value: `${results[year][0].election_type === "electoral" ? `${candidate.electoral_votes} electoral votes\n` : ""}${results[year][0].election_type === "parliament" ? `${candidate.seats_won} seats\n` : ""}${candidate.states_carried !== null && candidate.states_carried !== undefined ? `${candidate.states_carried} states carried\n` : ""}${candidate.delegates !== null && candidate.delegates !== undefined ? `${candidate.delegates.toLocaleString()} delegates\n` : ""}${candidate.delegates !== null && candidate.contests_won !== undefined ? `${candidate.contests_won} contests won\n` : ""}${candidate.votes !== null && candidate.votes !== undefined ? `${candidate.votes.toLocaleString()} votes (${candidate.percent}%)` : ""}`,
inline: true inline: true
}))) })))
.setTimestamp(); .setTimestamp();
if(results[year][0].has_map === true) { if(results[year][0].has_map === true) {
const map = await fetch(`http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}/map`); // temp test url const mapUrl = state
? `http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}/${state}/map`
: `http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}/map`;
const map = await fetch(mapUrl);
if (map.ok) { if (map.ok) {
const arrayBuffer = await map.arrayBuffer(); const arrayBuffer = await map.arrayBuffer();
const buffer = Buffer.from(arrayBuffer); const buffer = Buffer.from(arrayBuffer);