Skip to content

Commit 698074e

Browse files
migrate live-flight cmd to zod
1 parent c841fea commit 698074e

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/commands/utils/liveFlights.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ApplicationCommandType, Colors } from 'discord.js';
2-
import { slashCommand, slashCommandStructure, makeEmbed, Logger } from '../../lib';
2+
import { Request } from 'node-fetch';
3+
import { ZodError } from 'zod';
4+
import { slashCommand, slashCommandStructure, makeEmbed, Logger, fetchData, TelexCountSchema } from '../../lib';
35

46
const data = slashCommandStructure({
57
name: 'live-flights',
@@ -11,24 +13,34 @@ const FBW_WEB_MAP_URL = 'https://flybywiresim.com/map';
1113
const FBW_API_BASE_URL = 'https://api.flybywiresim.com';
1214

1315
export default slashCommand(data, async ({ interaction }) => {
16+
await interaction.deferReply();
17+
1418
try {
15-
const flights = await fetch(`${FBW_API_BASE_URL}/txcxn/_count`).then((res) => res.json());
19+
const flights = await fetchData(new Request(`${FBW_API_BASE_URL}/txcxn/_count`), TelexCountSchema);
1620
const flightsEmbed = makeEmbed({
1721
title: 'Live Flights',
1822
description: `There are currently **${flights}** active flights with TELEX enabled.`,
1923
footer: { text: 'Note: This includes the A32NX, and other aircraft using FlyByWire systems' },
2024
url: FBW_WEB_MAP_URL,
2125
timestamp: new Date().toISOString(),
2226
});
23-
return interaction.reply({ embeds: [flightsEmbed] });
27+
return interaction.editReply({ embeds: [flightsEmbed] });
2428
} catch (e) {
29+
if (e instanceof ZodError) {
30+
const errorEmbed = makeEmbed({
31+
title: 'TELEX Error',
32+
description: 'The API returned unknown data.',
33+
color: Colors.Red,
34+
});
35+
return interaction.editReply({ embeds: [errorEmbed] });
36+
}
2537
const error = e as Error;
2638
Logger.error(error);
2739
const errorEmbed = makeEmbed({
2840
title: 'Error | Live Flights',
2941
description: error.message,
3042
color: Colors.Red,
3143
});
32-
return interaction.reply({ embeds: [errorEmbed] });
44+
return interaction.editReply({ embeds: [errorEmbed] });
3345
}
3446
});

src/commands/utils/metar.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'discord.js';
22
import { Request } from 'node-fetch';
33
import { ZodError } from 'zod';
4-
import { constantsConfig, fetchData, makeEmbed, makeLines, slashCommand, slashCommandStructure } from '../../lib';
5-
import { Metar, MetarSchema } from '../../lib/apis/zodSchemas/avwx/metarSchemas';
4+
import { constantsConfig, fetchData, makeEmbed, makeLines, slashCommand, slashCommandStructure, Metar, MetarSchema } from '../../lib';
65

76
const data = slashCommandStructure({
87
name: 'metar',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const TelexCountSchema = z.number();
4+
5+
export type TelexCount = z.infer<typeof TelexCountSchema>;

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ export * from './apis/zodSchemas/avwx/tafSchemas';
3131
export * from './apis/zodSchemas/avwx/stationSchemas';
3232
export * from './apis/zodSchemas/simbrief/simbriefSchemas';
3333
export * from './apis/zodSchemas/wolframAlpha/wolframAlphaSchemas';
34+
export * from './apis/zodSchemas/flybywire/telex';

0 commit comments

Comments
 (0)