diff --git a/src/commands.ts b/src/commands.ts index 5d5f5db..10d03e3 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -45,8 +45,14 @@ const commands = [ { name: 'year', description: 'The year to view', - type: ApplicationCommandOptionType.Number, + type: ApplicationCommandOptionType.String, required: true + }, + { + name: 'state', + description: 'The state to view', + type: ApplicationCommandOptionType.String, + required: false } ], }, diff --git a/src/index.ts b/src/index.ts index 6a8515c..1a754e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -100,7 +100,12 @@ if (!config.token) { var country = i.options.get('country')?.value as string var election_type = i.options.get('type')?.value as string 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(); if (query.ok) { @@ -110,13 +115,17 @@ if (!config.token) { .setDescription("API test") .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})`, - 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 }))) .setTimestamp(); 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) { const arrayBuffer = await map.arrayBuffer(); const buffer = Buffer.from(arrayBuffer);