newMessage.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. const {Util} = require('discord.js');
  2. const {defaultSettings} = require('./default.json');
  3. const check_wiki = {
  4. fandom: require('../cmds/wiki/fandom.js'),
  5. gamepedia: require('../cmds/wiki/gamepedia.js')
  6. };
  7. const fs = require('fs');
  8. var cmdmap = {};
  9. var pausecmdmap = {};
  10. var ownercmdmap = {};
  11. fs.readdir( './cmds', (error, files) => {
  12. if ( error ) return error;
  13. files.filter( file => file.endsWith('.js') ).forEach( file => {
  14. var command = require('../cmds/' + file);
  15. if ( command.everyone ) cmdmap[command.name] = command.run;
  16. if ( command.pause ) pausecmdmap[command.name] = command.run;
  17. if ( command.owner ) ownercmdmap[command.name] = command.run;
  18. } );
  19. } );
  20. function newMessage(msg, lang, wiki = defaultSettings.wiki, prefix = process.env.prefix, noInline = null, content = '') {
  21. msg.noInline = noInline;
  22. var cont = ( content || msg.content );
  23. var cleanCont = ( content && Util.cleanContent(content, msg) || msg.cleanContent );
  24. var author = msg.author;
  25. var channel = msg.channel;
  26. var invoke = ( cont.split(' ')[1] ? cont.split(' ')[1].split('\n')[0].toLowerCase() : '' );
  27. var aliasInvoke = ( lang.aliases[invoke] || invoke );
  28. var ownercmd = ( msg.isOwner() && aliasInvoke in ownercmdmap );
  29. if ( cont.hasPrefix(prefix) && ownercmd ) {
  30. var args = cont.split(' ').slice(2);
  31. if ( cont.split(' ')[1].split('\n')[1] ) args.unshift( '', cont.split(' ')[1].split('\n')[1] );
  32. else console.log( ( channel.type === 'text' ? msg.guild.id : '@' + author.id ) + ': ' + cont );
  33. ownercmdmap[aliasInvoke](lang, msg, args, cont, wiki);
  34. } else {
  35. var count = 0;
  36. var maxcount = ( channel.type === 'text' && msg.guild.id in patreons ? 15 : 10 );
  37. cleanCont.replace( /\u200b/g, '' ).split('\n').forEach( line => {
  38. if ( line.hasPrefix(prefix) && count < maxcount ) {
  39. count++;
  40. invoke = ( line.split(' ')[1] ? line.split(' ')[1].toLowerCase() : '' );
  41. var args = line.split(' ').slice(2);
  42. aliasInvoke = ( lang.aliases[invoke] || invoke );
  43. ownercmd = ( msg.isOwner() && aliasInvoke in ownercmdmap );
  44. if ( channel.type === 'text' && pause[msg.guild.id] && !( ( msg.isAdmin() && aliasInvoke in pausecmdmap ) || ownercmd ) ) console.log( msg.guild.id + ': Paused' );
  45. else console.log( ( channel.type === 'text' ? msg.guild.id : '@' + author.id ) + ': ' + line );
  46. if ( ownercmd ) ownercmdmap[aliasInvoke](lang, msg, args, line, wiki);
  47. else if ( channel.type !== 'text' || !pause[msg.guild.id] || ( msg.isAdmin() && aliasInvoke in pausecmdmap ) ) {
  48. if ( aliasInvoke in cmdmap ) cmdmap[aliasInvoke](lang, msg, args, line, wiki);
  49. else if ( /^![a-z\d-]{1,50}$/.test(invoke) ) {
  50. cmdmap.LINK(lang, msg, args.join(' '), 'https://' + invoke.substring(1) + '.gamepedia.com/', ' ' + invoke + ' ');
  51. }
  52. else if ( /^\?(?:[a-z-]{1,8}\.)?[a-z\d-]{1,50}$/.test(invoke) ) {
  53. var invokeWiki = wiki;
  54. if ( invoke.includes( '.' ) ) invokeWiki = 'https://' + invoke.split('.')[1] + '.fandom.com/' + invoke.substring(1).split('.')[0] + '/';
  55. else invokeWiki = 'https://' + invoke.substring(1) + '.fandom.com/';
  56. cmdmap.LINK(lang, msg, args.join(' '), invokeWiki, ' ' + invoke + ' ');
  57. }
  58. else if ( /^\?\?(?:[a-z-]{1,8}\.)?[a-z\d-]{1,50}$/.test(invoke) ) {
  59. var invokeWiki = wiki;
  60. if ( invoke.includes( '.' ) ) invokeWiki = 'https://' + invoke.split('.')[1] + '.wikia.org/' + invoke.substring(2).split('.')[0] + '/';
  61. else invokeWiki = 'https://' + invoke.substring(2) + '.wikia.org/';
  62. cmdmap.LINK(lang, msg, args.join(' '), invokeWiki, ' ' + invoke + ' ');
  63. }
  64. else cmdmap.LINK(lang, msg, line.split(' ').slice(1).join(' '), wiki);
  65. }
  66. } else if ( line.hasPrefix(prefix) && count === maxcount ) {
  67. count++;
  68. console.log( '- Message contains too many commands!' );
  69. msg.reactEmoji('⚠️');
  70. msg.sendChannelError( lang.limit.replaceSave( '%s', '<@' + author.id + '>' ), {allowedMentions:{users:[author.id]}} );
  71. }
  72. } );
  73. if ( ( channel.type !== 'text' || !pause[msg.guild.id] ) && !noInline && ( cont.includes( '[[' ) || cont.includes( '{{' ) ) ) {
  74. var links = [];
  75. var embeds = [];
  76. var linkcount = 0;
  77. var linkmaxcount = maxcount + 5;
  78. msg.cleanContent.replace( /\u200b/g, '' ).replace( /(?<!\\)```.+?```/gs, '<codeblock>' ).replace( /(?<!\\)`.+?`/gs, '<code>' ).split('\n').forEach( line => {
  79. if ( line.hasPrefix(prefix) || !( line.includes( '[[' ) || line.includes( '{{' ) ) ) return;
  80. if ( line.includes( '[[' ) && line.includes( ']]' ) && linkcount <= linkmaxcount ) {
  81. let regex = new RegExp( '(?<!\\\\)(|\\|\\|)\\[\\[([^' + "<>\\[\\]\\|{}\\x01-\\x1F\\x7F" + ']+)(?<!\\\\)\\]\\]\\1', 'g' );
  82. let entry = null;
  83. while ( ( entry = regex.exec(line) ) !== null ) {
  84. if ( linkcount < linkmaxcount ) {
  85. linkcount++;
  86. console.log( ( channel.type === 'text' ? msg.guild.id : '@' + author.id ) + ': ' + entry[0] );
  87. let title = entry[2].split('#')[0];
  88. let section = ( entry[2].includes( '#' ) ? entry[2].split('#').slice(1).join('#') : '' )
  89. links.push({title,section,spoiler:entry[1]});
  90. }
  91. else if ( linkcount === linkmaxcount ) {
  92. linkcount++;
  93. console.log( '- Message contains too many links!' );
  94. msg.reactEmoji('⚠️');
  95. break;
  96. }
  97. }
  98. }
  99. if ( line.includes( '{{' ) && line.includes( '}}' ) && count <= maxcount ) {
  100. let regex = new RegExp( '(?<!\\\\)(|\\|\\|)(?<!\\{)\\{\\{([^' + "<>\\[\\]\\|{}\\x01-\\x1F\\x7F" + ']+)(?<!\\\\)\\}\\}\\1', 'g' );
  101. let entry = null;
  102. while ( ( entry = regex.exec(line) ) !== null ) {
  103. if ( count < maxcount ) {
  104. count++;
  105. console.log( ( channel.type === 'text' ? msg.guild.id : '@' + author.id ) + ': ' + entry[0] );
  106. let title = entry[2].split('#')[0];
  107. let section = ( entry[2].includes( '#' ) ? entry[2].split('#').slice(1).join('#') : '' )
  108. embeds.push({title,section,spoiler:entry[1]});
  109. }
  110. else if ( count === maxcount ) {
  111. count++;
  112. console.log( '- Message contains too many links!' );
  113. msg.reactEmoji('⚠️');
  114. break;
  115. }
  116. }
  117. }
  118. } );
  119. if ( links.length ) got.get( wiki + 'api.php?action=query&meta=siteinfo&siprop=general&iwurl=true&titles=' + encodeURIComponent( links.map( link => link.title ).join('|') ) + '&format=json', {
  120. responseType: 'json'
  121. } ).then( response => {
  122. var body = response.body;
  123. if ( response.statusCode !== 200 || !body || !body.query ) {
  124. if ( wiki.noWiki(response.url) || response.statusCode === 410 ) {
  125. console.log( '- This wiki doesn\'t exist!' );
  126. msg.reactEmoji('nowiki');
  127. return;
  128. }
  129. console.log( '- ' + response.statusCode + ': Error while following the links: ' + ( body && body.error && body.error.info ) );
  130. return;
  131. }
  132. if ( body.query.normalized ) {
  133. body.query.normalized.forEach( title => links.filter( link => link.title === title.from ).forEach( link => link.title = title.to ) );
  134. }
  135. if ( body.query.interwiki ) {
  136. body.query.interwiki.forEach( interwiki => links.filter( link => link.title === interwiki.title ).forEach( link => {
  137. link.url = interwiki.url + ( link.section ? '#' + link.section.toSection() : '' );
  138. } ) );
  139. }
  140. if ( body.query.pages ) {
  141. var querypages = Object.values(body.query.pages);
  142. querypages.filter( page => page.invalid !== undefined ).forEach( page => links.filter( link => link.title === page.title ).forEach( link => {
  143. links.splice(links.indexOf(link), 1);
  144. } ) );
  145. querypages.filter( page => page.missing !== undefined && page.known === undefined ).forEach( page => links.filter( link => link.title === page.title ).forEach( link => {
  146. if ( ( page.ns === 2 || page.ns === 202 ) && !page.title.includes( '/' ) ) return;
  147. link.url = wiki.toLink(link.title, 'action=edit&redlink=1', '', body.query.general);
  148. } ) );
  149. }
  150. if ( links.length ) msg.sendChannel( links.map( link => link.spoiler + '<' + ( link.url || wiki.toLink(link.title, '', link.section, body.query.general) ) + '>' + link.spoiler ).join('\n'), {split:true} );
  151. }, error => {
  152. if ( wiki.noWiki(error.message) ) {
  153. console.log( '- This wiki doesn\'t exist!' );
  154. msg.reactEmoji('nowiki');
  155. }
  156. else {
  157. console.log( '- Error while following the links: ' + error );
  158. }
  159. } );
  160. if ( embeds.length ) got.get( wiki + 'api.php?action=query&meta=siteinfo&siprop=general' + ( wiki.isFandom() ? '' : '|variables' ) + '&titles=' + encodeURIComponent( embeds.map( embed => embed.title + '|Template:' + embed.title ).join('|') ) + '&format=json', {
  161. responseType: 'json'
  162. } ).then( response => {
  163. var body = response.body;
  164. if ( response.statusCode !== 200 || !body || !body.query ) {
  165. if ( wiki.noWiki(response.url) || response.statusCode === 410 ) {
  166. console.log( '- This wiki doesn\'t exist!' );
  167. msg.reactEmoji('nowiki');
  168. return;
  169. }
  170. console.log( '- ' + response.statusCode + ': Error while following the links: ' + ( body && body.error && body.error.info ) );
  171. return;
  172. }
  173. if ( body.query.normalized ) {
  174. body.query.normalized.forEach( title => embeds.filter( embed => embed.title === title.from ).forEach( embed => embed.title = title.to ) );
  175. }
  176. if ( body.query.pages ) {
  177. var querypages = Object.values(body.query.pages);
  178. querypages.filter( page => page.invalid !== undefined ).forEach( page => embeds.filter( embed => embed.title === page.title ).forEach( embed => {
  179. embeds.splice(embeds.indexOf(embed), 1);
  180. } ) );
  181. var missing = [];
  182. querypages.filter( page => page.missing !== undefined && page.known === undefined ).forEach( page => embeds.filter( embed => embed.title === page.title ).forEach( embed => {
  183. if ( ( page.ns === 2 || page.ns === 202 ) && !page.title.includes( '/' ) ) return;
  184. embeds.splice(embeds.indexOf(embed), 1);
  185. if ( page.ns === 0 && !embed.section ) {
  186. var template = querypages.find( template => template.ns === 10 && template.title.split(':').slice(1).join(':') === embed.title );
  187. if ( template && template.missing === undefined ) embed.template = wiki.toLink(template.title, '', '', body.query.general);
  188. }
  189. if ( embed.template || !body.query.variables || !body.query.variables.some( variable => variable.toUpperCase() === embed.title ) ) missing.push(embed);
  190. } ) );
  191. if ( missing.length ) {
  192. msg.sendChannel( missing.map( embed => embed.spoiler + '<' + ( embed.template || wiki.toLink(embed.title, 'action=edit&redlink=1', '', body.query.general) ) + '>' + embed.spoiler ).join('\n'), {split:true} );
  193. }
  194. }
  195. if ( embeds.length ) {
  196. if ( wiki.isFandom() ) embeds.forEach( embed => msg.reactEmoji('⏳').then( reaction => {
  197. check_wiki.fandom(lang, msg, embed.title, wiki, ' ', reaction, embed.spoiler, '', embed.section);
  198. } ) );
  199. else embeds.forEach( embed => msg.reactEmoji('⏳').then( reaction => {
  200. check_wiki.gamepedia(lang, msg, embed.title, wiki, ' ', reaction, embed.spoiler, '', embed.section);
  201. } ) );
  202. }
  203. }, error => {
  204. if ( wiki.noWiki(error.message) ) {
  205. console.log( '- This wiki doesn\'t exist!' );
  206. msg.reactEmoji('nowiki');
  207. }
  208. else {
  209. console.log( '- Error while following the links: ' + error );
  210. }
  211. } );
  212. }
  213. }
  214. }
  215. module.exports = newMessage;