1
1
import { ApplicationCommandOptionType , ApplicationCommandType , Colors } from 'discord.js' ;
2
- import { slashCommand , slashCommandStructure , makeEmbed , makeLines , Logger } from '../../lib' ;
2
+ import { Request } from 'node-fetch' ;
3
+ import { z , ZodError } from 'zod' ;
4
+ import { fetchData , Logger , makeEmbed , makeLines , slashCommand , slashCommandStructure , WolframAlphaData , WolframAlphaDataSchema , WolframAlphaPodSchema , WolframAlphaSubpodSchema } from '../../lib' ;
5
+
6
+ type Pod = z . infer < typeof WolframAlphaPodSchema > ;
7
+ type Subpod = z . infer < typeof WolframAlphaSubpodSchema > ;
3
8
4
9
const data = slashCommandStructure ( {
5
10
name : 'wolframalpha' ,
@@ -19,6 +24,12 @@ const noQueryEmbed = makeEmbed({
19
24
color : Colors . Red ,
20
25
} ) ;
21
26
27
+ const errorEmbed = ( errorMessage : string ) => makeEmbed ( {
28
+ title : 'Wolfram Alpha Error' ,
29
+ description : errorMessage ,
30
+ color : Colors . Red ,
31
+ } ) ;
32
+
22
33
const WOLFRAMALPHA_API_URL = 'https://api.wolframalpha.com/v2/query?' ;
23
34
const WOLFRAMALPHA_QUERY_URL = 'https://www.wolframalpha.com/input/?' ;
24
35
@@ -33,12 +44,12 @@ export default slashCommand(data, async ({ interaction }) => {
33
44
description : 'Wolfram Alpha token not found.' ,
34
45
color : Colors . Red ,
35
46
} ) ;
36
- return interaction . followUp ( { embeds : [ noTokenEmbed ] , ephemeral : true } ) ;
47
+ return interaction . editReply ( { embeds : [ noTokenEmbed ] } ) ;
37
48
}
38
49
39
50
const query = interaction . options . getString ( 'query' ) ;
40
51
41
- if ( ! query ) return interaction . followUp ( { embeds : [ noQueryEmbed ] , ephemeral : true } ) ;
52
+ if ( ! query ) return interaction . editReply ( { embeds : [ noQueryEmbed ] } ) ;
42
53
43
54
const params = {
44
55
appid : wolframAlphaToken ,
@@ -49,72 +60,62 @@ export default slashCommand(data, async ({ interaction }) => {
49
60
50
61
const searchParams = new URLSearchParams ( params ) ;
51
62
63
+ let response : WolframAlphaData ;
52
64
try {
53
- const response = await fetch ( `${ WOLFRAMALPHA_API_URL } ${ searchParams . toString ( ) } ` )
54
- . then ( ( res ) => res . json ( ) ) ;
55
-
56
- if ( response . error ) {
57
- const errorEmbed = makeEmbed ( {
58
- title : 'Wolfram Alpha Error' ,
59
- description : response . error ,
60
- color : Colors . Red ,
61
- } ) ;
62
- return interaction . followUp ( { embeds : [ errorEmbed ] , ephemeral : true } ) ;
65
+ response = await fetchData < WolframAlphaData > ( new Request ( `${ WOLFRAMALPHA_API_URL } ${ searchParams . toString ( ) } ` ) , WolframAlphaDataSchema ) ;
66
+ } catch ( e ) {
67
+ if ( e instanceof ZodError ) {
68
+ return interaction . editReply ( { embeds : [ errorEmbed ( 'Wolfram Alpha returned unknown data.' ) ] } ) ;
63
69
}
70
+ Logger . error ( `Error while fetching from Wolfram Alpha: ${ String ( e ) } ` ) ;
71
+ return interaction . editReply ( { embeds : [ errorEmbed ( 'An error occurred while fetching from Wolfram Alpha.' ) ] } ) ;
72
+ }
64
73
65
- if ( response . queryresult . success === true ) {
66
- const podTexts : string [ ] = [ ] ;
67
- response . queryresult . pods . forEach ( ( pod : any ) => {
68
- if ( pod . id !== 'Input' && pod . primary === true ) {
69
- const results : string [ ] = [ ] ;
70
- pod . subpods . forEach ( ( subpod : any ) => {
71
- results . push ( subpod . plaintext ) ;
72
- } ) ;
73
- if ( results . length > 0 ) {
74
- podTexts . push ( `**${ pod . title } :** \n${ results . join ( '\n' ) } ` ) ;
75
- }
76
- }
77
- } ) ;
78
- if ( podTexts . length > 0 ) {
79
- const result = podTexts . join ( '\n\n' ) ;
80
- const queryParams = new URLSearchParams ( { i : query } ) ;
81
-
82
- const waEmbed = makeEmbed ( {
83
- description : makeLines ( [
84
- `**Query:** ${ query } ` ,
85
- '' ,
86
- result ,
87
- '' ,
88
- `[Web Result](${ WOLFRAMALPHA_QUERY_URL } ${ queryParams . toString ( ) } )` ,
89
- ] ) ,
74
+ if ( response . queryresult . success === true ) {
75
+ const podTexts : string [ ] = [ ] ;
76
+ response . queryresult . pods . forEach ( ( pod : Pod ) => {
77
+ if ( pod . id !== 'Input' && pod . primary === true ) {
78
+ const results : string [ ] = [ ] ;
79
+ pod . subpods . forEach ( ( subpod : Subpod ) => {
80
+ results . push ( subpod . plaintext ) ;
90
81
} ) ;
91
-
92
- return interaction . followUp ( { embeds : [ waEmbed ] } ) ;
82
+ if ( results . length > 0 ) {
83
+ podTexts . push ( `**${ pod . title } :** \n${ results . join ( '\n' ) } ` ) ;
84
+ }
93
85
}
94
- const noResultsEmbed = makeEmbed ( {
95
- title : 'Wolfram Alpha Error | No Results' ,
86
+ } ) ;
87
+ if ( podTexts . length > 0 ) {
88
+ const result = podTexts . join ( '\n\n' ) ;
89
+ const queryParams = new URLSearchParams ( { i : query } ) ;
90
+
91
+ const waEmbed = makeEmbed ( {
96
92
description : makeLines ( [
97
- 'No results were found for your query.' ,
93
+ `**Query:** ${ query } ` ,
94
+ '' ,
95
+ result ,
96
+ '' ,
97
+ `[Web Result](${ WOLFRAMALPHA_QUERY_URL } ${ queryParams . toString ( ) } )` ,
98
98
] ) ,
99
- color : Colors . Red ,
100
99
} ) ;
101
- return interaction . followUp ( { embeds : [ noResultsEmbed ] , ephemeral : true } ) ;
100
+
101
+ return interaction . editReply ( { embeds : [ waEmbed ] } ) ;
102
102
}
103
- const obscureQueryEmbed = makeEmbed ( {
104
- title : 'Wolfram Alpha Error | Could not understand query ' ,
103
+ const noResultsEmbed = makeEmbed ( {
104
+ title : 'Wolfram Alpha Error | No Results ' ,
105
105
description : makeLines ( [
106
- 'Wolfram Alpha could not understand your query.' ,
106
+ 'No results were found for your query.' ,
107
107
] ) ,
108
108
color : Colors . Red ,
109
109
} ) ;
110
- return interaction . followUp ( { embeds : [ obscureQueryEmbed ] , ephemeral : true } ) ;
111
- } catch ( e ) {
112
- Logger . error ( 'wolframalpha:' , e ) ;
113
- const fetchErrorEmbed = makeEmbed ( {
114
- title : 'Wolfram Alpha | Fetch Error' ,
115
- description : 'There was an error fetching your request. Please try again later.' ,
116
- color : Colors . Red ,
117
- } ) ;
118
- return interaction . followUp ( { embeds : [ fetchErrorEmbed ] , ephemeral : true } ) ;
110
+ return interaction . editReply ( { embeds : [ noResultsEmbed ] } ) ;
119
111
}
112
+
113
+ const obscureQueryEmbed = makeEmbed ( {
114
+ title : 'Wolfram Alpha Error | Could not understand query' ,
115
+ description : makeLines ( [
116
+ 'Wolfram Alpha could not understand your query.' ,
117
+ ] ) ,
118
+ color : Colors . Red ,
119
+ } ) ;
120
+ return interaction . editReply ( { embeds : [ obscureQueryEmbed ] } ) ;
120
121
} ) ;
0 commit comments