search.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const {MessageEmbed, Util} = require('discord.js');
  2. const {limit: {search: searchLimit}} = require('../../util/default.json');
  3. /**
  4. * Searches a Gamepedia wiki.
  5. * @param {import('../../util/i18n.js')} lang - The user language.
  6. * @param {import('discord.js').Message} msg - The Discord message.
  7. * @param {String} searchterm - The searchterm.
  8. * @param {import('../../util/wiki.js')} wiki - The wiki for the search.
  9. * @param {Object} query - The siteinfo from the wiki.
  10. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  11. * @param {String} spoiler - If the response is in a spoiler.
  12. */
  13. function gamepedia_search(lang, msg, searchterm, wiki, query, reaction, spoiler) {
  14. if ( searchterm.length > 250 ) {
  15. searchterm = searchterm.substring(0, 250);
  16. msg.reactEmoji('⚠️');
  17. }
  18. var pagelink = wiki.toLink('Special:Search', {search:searchterm,fulltext:1});
  19. var embed = new MessageEmbed().setAuthor( query.general.sitename ).setTitle( '`' + searchterm + '`' ).setURL( pagelink );
  20. if ( !searchterm.trim() ) {
  21. pagelink = wiki.toLink('Special:Search');
  22. embed.setTitle( 'Special:Search' ).setURL( pagelink );
  23. }
  24. var querypage = ( Object.values(( query.pages || {} ))?.[0] || {title:'',ns:0,invalid:''} );
  25. var description = [];
  26. var limit = searchLimit[( msg?.guild?.id in patreons ? 'patreon' : 'default' )];
  27. got.get( wiki + 'api.php?action=query&titles=Special:Search&list=search&srinfo=totalhits&srprop=redirecttitle|sectiontitle&srnamespace=4|12|14|' + querypage.ns + '|' + Object.values(query.namespaces).filter( ns => ns.content !== undefined ).map( ns => ns.id ).join('|') + '&srlimit=' + limit + '&srsearch=' + encodeURIComponent( ( searchterm || ' ' ) ) + '&format=json' ).then( response => {
  28. var body = response.body;
  29. if ( body && body.warnings ) log_warn(body.warnings);
  30. if ( response.statusCode !== 200 || !body || !body.query || !body.query.search || body.batchcomplete === undefined ) {
  31. return console.log( '- ' + response.statusCode + ': Error while getting the search results: ' + ( body && body.error && body.error.info ) );
  32. }
  33. if ( body.query.pages && body.query.pages['-1'] && body.query.pages['-1'].title ) {
  34. if ( searchterm.trim() ) {
  35. pagelink = wiki.toLink(body.query.pages['-1'].title, {search:searchterm,fulltext:1});
  36. embed.setURL( pagelink );
  37. }
  38. else {
  39. pagelink = wiki.toLink(body.query.pages['-1'].title);
  40. embed.setTitle( body.query.pages['-1'].title ).setURL( pagelink );
  41. }
  42. }
  43. if ( searchterm.trim() ) {
  44. var hasExactMatch = false;
  45. body.query.search.forEach( result => {
  46. let text = '• ';
  47. let bold = '';
  48. if ( result.title.replace( /[_-]/g, ' ' ).toLowerCase() === querypage.title.replace( /-/g, ' ' ).toLowerCase() ) {
  49. bold = '**';
  50. hasExactMatch = true;
  51. if ( query.redirects?.[0] ) {
  52. if ( query.redirects[0].tofragment && !result.sectiontitle ) {
  53. result.sectiontitle = query.redirects[0].tofragment;
  54. }
  55. if ( !result.redirecttitle ) result.redirecttitle = query.redirects[0].from;
  56. }
  57. }
  58. text += bold;
  59. text += '[' + result.title + '](' + wiki.toLink(result.title, '', '', true) + ')';
  60. if ( result.sectiontitle ) {
  61. text += ' § [' + result.sectiontitle + '](' + wiki.toLink(result.title, '', result.sectiontitle, true) + ')';
  62. }
  63. if ( result.redirecttitle ) {
  64. text += ' (⤷ [' + result.redirecttitle + '](' + wiki.toLink(result.redirecttitle, 'redirect=no', '', true) + '))';
  65. }
  66. text += bold;
  67. description.push( text );
  68. } );
  69. if ( !hasExactMatch ) {
  70. if ( query.interwiki?.[0] ) {
  71. let text = '• **⤷ ';
  72. text += '__[' + query.interwiki[0].title + '](' + query.interwiki[0].url.replace( /[()]/g, '\\$&' ) + ')__';
  73. if ( query.redirects?.[0] ) {
  74. text += ' (⤷ [' + query.redirects[0].from + '](' + wiki.toLink(query.redirects[0].from, 'redirect=no', '', true) + '))';
  75. }
  76. text += '**';
  77. description.unshift( text );
  78. }
  79. else if ( querypage.invalid === undefined && ( querypage.missing === undefined || querypage.known !== undefined ) ) {
  80. let text = '• **';
  81. text += '[' + querypage.title + '](' + wiki.toLink(querypage.title, '', '', true) + ')';
  82. if ( query.redirects?.[0] ) {
  83. if ( query.redirects[0].tofragment ) {
  84. text += ' § [' + query.redirects[0].tofragment + '](' + wiki.toLink(querypage.title, '', query.redirects[0].tofragment, true) + ')';
  85. }
  86. text += ' (⤷ [' + query.redirects[0].from + '](' + wiki.toLink(query.redirects[0].from, 'redirect=no', '', true) + '))';
  87. }
  88. text += '**';
  89. description.unshift( text );
  90. }
  91. }
  92. if ( body.query.searchinfo ) {
  93. embed.setFooter( lang.get('search.results', body.query.searchinfo.totalhits.toLocaleString(lang.get('dateformat')), body.query.searchinfo.totalhits) );
  94. }
  95. }
  96. }, error => {
  97. console.log( '- Error while getting the search results.' + error );
  98. } ).finally( () => {
  99. embed.setDescription( Util.splitMessage( description.join('\n') )[0] );
  100. msg.sendChannel( spoiler + '<' + pagelink + '>' + spoiler, {embed} );
  101. if ( reaction ) reaction.removeEmoji();
  102. } );
  103. }
  104. module.exports = {
  105. name: 'search',
  106. run: gamepedia_search
  107. };