newMessage.js 11 KB

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