newMessage.js 11 KB

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