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', name: 'test',
description: "election api test", description: "election api test",
options: [ 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', name: 'year',
description: 'The year to view', description: 'The year to view',

View file

@ -59,19 +59,14 @@ if (!config.token) {
break; break;
case "test": 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) 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(); let results = await query.json();
if (query.ok) { if (query.ok) {
if(results[year] != undefined) { 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() var embed = new EmbedBuilder()
.setTitle(` ${results[year][0].election_name}`) .setTitle(` ${results[year][0].election_name}`)
.setDescription("API test") .setDescription("API test")
@ -80,10 +75,20 @@ if (!config.token) {
value: `${candidate.votes.toLocaleString()} votes (${candidate.percent}%)`, value: `${candidate.votes.toLocaleString()} votes (${candidate.percent}%)`,
inline: true inline: true
}))) })))
.setImage("attachment://map.png")
.setTimestamp(); .setTimestamp();
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"}]}); await i.editReply({embeds: [embed], files: [{attachment: buffer, name: "map.png"}]});
break; break;
} else {
await i.editReply({embeds: [embed]});
break;
}
break;
} else { } else {
await i.editReply("Election not found"); await i.editReply("Election not found");
break; break;