newMessage.js 12 KB

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