@@ -31,7 +31,8 @@ const command: Command = {
3131 opt
3232 . setName ( "query" )
3333 . setDescription ( "Enter the phrase you'd like to search for. Example: Array.filter" )
34- . setRequired ( true ) ,
34+ . setRequired ( true )
35+ . setAutocomplete ( true ) ,
3536 ) ,
3637 async execute ( interaction ) {
3738 const deleteButtonRow = new MessageActionRow ( ) . addComponents ( [ deleteButton ( interaction . user . id ) ] ) ;
@@ -45,10 +46,15 @@ const command: Command = {
4546
4647 if ( ! search . length ) {
4748 embed . setColor ( 0xff0000 ) . setDescription ( "No results found..." ) ;
48- await interaction . reply ( { embeds : [ embed ] , ephemeral : true } ) ;
49+ await interaction . reply ( { embeds : [ embed ] , ephemeral : true } ) . catch ( console . error ) ;
4950 return ;
5051 } else if ( search . length === 1 ) {
5152 const resultEmbed = await getSingleMDNSearchResults ( search [ 0 ] ) ;
53+ if ( ! resultEmbed ) {
54+ await interaction . reply ( { content : "Couldn't find any results" , ephemeral : true } ) . catch ( console . error ) ;
55+ return ;
56+ }
57+
5258 await interaction
5359 . reply ( {
5460 embeds : [ resultEmbed ] ,
@@ -59,15 +65,12 @@ const command: Command = {
5965 return ;
6066 } else {
6167 // If there are multiple results, send a select menu from which the user can choose which one to send
62- const results = search . map ( ( path ) => `**• [${ path . replace ( / _ | - / g, " " ) } ](${ MDN_BASE_URL } ${ path } )**` ) ;
63-
64- embed . setDescription ( results . join ( "\n" ) ) ;
6568 const selectMenuRow = new MessageActionRow ( ) . addComponents (
6669 new MessageSelectMenu ( )
6770 . setCustomId ( "mdnselect/" + interaction . user . id )
6871 . addOptions (
6972 search . map ( ( val ) => {
70- const parsed = val . length >= 99 ? val . split ( "/" ) . at ( - 1 ) : val ;
73+ const parsed = val . length >= 99 ? val . split ( "/" ) . slice ( - 2 ) . join ( "/" ) : val ;
7174 return { label : parsed , value : parsed } ;
7275 } ) ,
7376 )
@@ -87,8 +90,16 @@ const command: Command = {
8790
8891// Export to reuse on the select menu handler
8992export async function getSingleMDNSearchResults ( searchQuery : string ) {
90- const res = await fetch ( `${ MDN_BASE_URL + searchQuery } /index.json` ) ;
91- const doc : MdnDoc = ( await res . json ( ) ) . doc ;
93+ // Search for the match once again
94+ const { index, sitemap } = await getSources ( ) ;
95+ const secondSearch = index . search ( searchQuery , { limit : 10 } ) . map ( ( id ) => sitemap [ < number > id ] . loc ) [ 0 ] ;
96+
97+ const res = await fetch ( `${ MDN_BASE_URL + secondSearch } /index.json` ) . catch ( console . error ) ;
98+ if ( ! res || ! res ?. ok ) return null ;
99+ const resJSON = await res . json ?.( ) . catch ( console . error ) ;
100+ if ( ! res . json ) return null ;
101+
102+ const doc : MdnDoc = resJSON . doc ;
92103
93104 return new MessageEmbed ( )
94105 . setColor ( MDN_BLUE_COLOR )
@@ -99,7 +110,7 @@ export async function getSingleMDNSearchResults(searchQuery: string) {
99110 . setThumbnail ( MDN_ICON_URL )
100111 . setDescription ( doc . summary ) ;
101112}
102- async function getSources ( ) : Promise < typeof sources > {
113+ export async function getSources ( ) : Promise < typeof sources > {
103114 if ( sources . lastUpdated && Date . now ( ) - sources . lastUpdated < 43200000 /* 12 hours */ ) return sources ;
104115
105116 const res = await fetch ( "https://developer.mozilla.org/sitemaps/en-us/sitemap.xml.gz" ) ;
@@ -109,7 +120,6 @@ async function getSources(): Promise<typeof sources> {
109120 loc : entry . loc . slice ( MDN_BASE_URL . length ) ,
110121 lastmod : new Date ( entry . lastmod ) . valueOf ( ) ,
111122 } ) ) ;
112-
113123 const index = new flexsearch . Index ( ) ;
114124 sitemap . forEach ( ( entry , idx ) => index . add ( idx , entry . loc ) ) ;
115125
0 commit comments