only attach map if it actually exists

This commit is contained in:
dartz 2024-07-23 17:24:05 -04:00
parent a2a5eccdfa
commit a5e824d5f0
2 changed files with 27 additions and 10 deletions

View file

@ -30,6 +30,18 @@ const commands = [
name: 'test',
description: "election api test",
options: [
{
name: 'country',
description: 'The country to view',
type: ApplicationCommandOptionType.String,
required: true
},
{
name: 'type',
description: 'The type of election',
type: ApplicationCommandOptionType.String,
required: true
},
{
name: 'year',
description: 'The year to view',

View file

@ -59,19 +59,14 @@ if (!config.token) {
break;
case "test":
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/us/governor/${year}/mn`); // temp test url
let query = await fetch(`http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}`); // temp test url
let results = await query.json();
if (query.ok) {
if(results[year] != undefined) {
const map = await fetch(`http://127.0.0.1:3000/api/v1/us/governor/${year}/mn/map`); // temp test url
if (!map.ok) {
throw new Error('Failed to fetch map');
}
const arrayBuffer = await map.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
var embed = new EmbedBuilder()
.setTitle(` ${results[year][0].election_name}`)
.setDescription("API test")
@ -80,9 +75,19 @@ if (!config.token) {
value: `${candidate.votes.toLocaleString()} votes (${candidate.percent}%)`,
inline: true
})))
.setImage("attachment://map.png")
.setTimestamp();
await i.editReply({embeds: [embed], files: [{attachment: buffer, name: "map.png"}]});
const map = await fetch(`http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}/map`); // temp test url
if (map.ok) {
const arrayBuffer = await map.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
embed.setImage("attachment://map.png");
await i.editReply({embeds: [embed], files: [{attachment: buffer, name: "map.png"}]});
break;
} else {
await i.editReply({embeds: [embed]});
break;
}
break;
} else {
await i.editReply("Election not found");