1
1
import { ApplicationCommandOptionType , ApplicationCommandType , Colors } from 'discord.js' ;
2
- import fetch from 'node-fetch' ;
3
- import { constantsConfig , slashCommand , slashCommandStructure , makeEmbed , makeLines , Logger } from '../../lib' ;
2
+ import { Request } from 'node-fetch' ;
3
+ import { ZodError } from 'zod' ;
4
+ import { Logger , TAF , TafSchema , fetchData , makeEmbed , makeLines , slashCommand , slashCommandStructure } from '../../lib' ;
4
5
5
6
const data = slashCommandStructure ( {
6
7
name : 'taf' ,
@@ -22,6 +23,12 @@ const noQueryEmbed = makeEmbed({
22
23
color : Colors . Red ,
23
24
} ) ;
24
25
26
+ const errorEmbed = ( error : string ) => makeEmbed ( {
27
+ title : 'TAF Error' ,
28
+ description : error ,
29
+ color : Colors . Red ,
30
+ } ) ;
31
+
25
32
export default slashCommand ( data , async ( { interaction } ) => {
26
33
await interaction . deferReply ( ) ;
27
34
@@ -42,47 +49,32 @@ export default slashCommand(data, async ({ interaction }) => {
42
49
return interaction . editReply ( { embeds : [ noQueryEmbed ] } ) ;
43
50
}
44
51
52
+ let taf : TAF ;
45
53
try {
46
- const tafReport : any = await fetch ( `https://avwx.rest/api/taf/${ icao } ` , {
54
+ taf = await fetchData < TAF > ( new Request ( `https://avwx.rest/api/taf/${ icao } ` , {
47
55
method : 'GET' ,
48
56
headers : { Authorization : tafToken } ,
49
- } ) . then ( ( res ) => res . json ( ) ) ;
50
-
51
- if ( tafReport . error ) {
52
- const invalidEmbed = makeEmbed ( {
53
- title : `TAF Error | ${ icao . toUpperCase ( ) } ` ,
54
- description : tafReport . error ,
55
- color : Colors . Red ,
56
- } ) ;
57
- return interaction . editReply ( { embeds : [ invalidEmbed ] } ) ;
57
+ } ) , TafSchema ) ;
58
+ } catch ( e ) {
59
+ if ( e instanceof ZodError ) {
60
+ return interaction . editReply ( { embeds : [ errorEmbed ( 'The API returned unknown data.' ) ] } ) ;
58
61
}
59
- const getClouds = ( clouds : any ) => {
60
- const retClouds = [ ] ;
61
- for ( const cloud of clouds ) {
62
- retClouds . push ( cloud . repr ) ;
63
- }
64
- return retClouds . join ( ', ' ) ;
65
- } ;
66
- const tafEmbed = makeEmbed ( {
67
- title : `TAF Report | ${ tafReport . station } ` ,
68
- description : makeLines ( [
69
- '**Raw Report**' ,
70
- tafReport . raw ,
62
+ return interaction . editReply ( { embeds : [ errorEmbed ( `An error occurred while fetching the latest TAF for ${ icao . toUpperCase ( ) } .` ) ] } ) ;
63
+ }
71
64
72
- '' ,
73
- '**Basic Report:**' ,
74
- `**Time Forecasted:** ${ tafReport . time . dt } ` ,
75
- `**Forecast Start Time:** ${ tafReport . start_time . dt } ` ,
76
- `**Forecast End Time:** ${ tafReport . end_time . dt } ` ,
77
- `**Visibility:** ${ tafReport . forecast [ 0 ] . visibility . repr } ${ Number . isNaN ( + tafReport . forecast [ 0 ] . visibility . repr ) ? '' : tafReport . units . visibility } ` ,
78
- `**Wind:** ${ tafReport . forecast [ 0 ] . wind_direction . repr } ${ tafReport . forecast [ 0 ] . wind_direction . repr === 'VRB' ? '' : constantsConfig . units . DEGREES } at ${ tafReport . forecast [ 0 ] . wind_speed . repr } ${ tafReport . units . wind_speed } ` ,
79
- `**Clouds:** ${ getClouds ( tafReport . forecast [ 0 ] . clouds ) } ` ,
80
- `**Flight Rules:** ${ tafReport . forecast [ 0 ] . flight_rules } ` ,
81
- ] ) ,
65
+ try {
66
+ const tafEmbed = makeEmbed ( {
67
+ title : `TAF Report | ${ taf . station } ` ,
68
+ description : makeLines ( [ '**Raw Report**' , ...taf . forecast . map ( ( forecast , i ) => {
69
+ if ( i === 0 ) {
70
+ return `${ taf . station } ${ forecast . raw } ` ;
71
+ }
72
+ return forecast . raw ;
73
+ } ) ] ) ,
82
74
fields : [
83
75
{
84
- name : 'Unsure of how to read the raw report?' ,
85
- value : ' Please refer to our guide [here. ](https://docs.flybywiresim.com/pilots-corner/airliner-flying-guide/weather/#taf-example-decoded)' ,
76
+ name : 'Unsure of how to read the report?' ,
77
+ value : ` Please refer to our guide [here](https://docs.flybywiresim.com/pilots-corner/airliner-flying-guide/weather/#taf-example-decoded) or see above report decoded [here](https://e6bx.com/weather/ ${ taf . station } /?showDecoded=1&focuspoint=tafdecoder).` ,
86
78
inline : false ,
87
79
} ,
88
80
] ,
0 commit comments