1
1
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' ;
3
5
4
6
const data = slashCommandStructure ( {
5
7
name : 'live-flights' ,
@@ -11,24 +13,34 @@ const FBW_WEB_MAP_URL = 'https://flybywiresim.com/map';
11
13
const FBW_API_BASE_URL = 'https://api.flybywiresim.com' ;
12
14
13
15
export default slashCommand ( data , async ( { interaction } ) => {
16
+ await interaction . deferReply ( ) ;
17
+
14
18
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 ) ;
16
20
const flightsEmbed = makeEmbed ( {
17
21
title : 'Live Flights' ,
18
22
description : `There are currently **${ flights } ** active flights with TELEX enabled.` ,
19
23
footer : { text : 'Note: This includes the A32NX, and other aircraft using FlyByWire systems' } ,
20
24
url : FBW_WEB_MAP_URL ,
21
25
timestamp : new Date ( ) . toISOString ( ) ,
22
26
} ) ;
23
- return interaction . reply ( { embeds : [ flightsEmbed ] } ) ;
27
+ return interaction . editReply ( { embeds : [ flightsEmbed ] } ) ;
24
28
} 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
+ }
25
37
const error = e as Error ;
26
38
Logger . error ( error ) ;
27
39
const errorEmbed = makeEmbed ( {
28
40
title : 'Error | Live Flights' ,
29
41
description : error . message ,
30
42
color : Colors . Red ,
31
43
} ) ;
32
- return interaction . reply ( { embeds : [ errorEmbed ] } ) ;
44
+ return interaction . editReply ( { embeds : [ errorEmbed ] } ) ;
33
45
}
34
46
} ) ;
0 commit comments