global_block.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const cheerio = require('cheerio');
  2. const {timeoptions} = require('../util/default.json');
  3. /**
  4. * Add global blocks to user messages.
  5. * @param {import('../util/i18n.js')} lang - The user language.
  6. * @param {import('discord.js').Message} msg - The Discord message.
  7. * @param {String} username - The name of the user.
  8. * @param {String} text - The text of the response.
  9. * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
  10. * @param {String} wiki - The wiki for the page.
  11. * @param {String} spoiler - If the response is in a spoiler.
  12. */
  13. function global_block(lang, msg, username, text, embed, wiki, spoiler) {
  14. if ( !msg || msg.channel.type !== 'text' || !( msg.guild.id in patreons ) ) return;
  15. if ( msg.showEmbed() ) embed.fields.pop();
  16. else {
  17. let splittext = text.split('\n\n');
  18. splittext.pop();
  19. text = splittext.join('\n\n');
  20. }
  21. if ( wiki.isFandom() ) got.get( 'https://community.fandom.com/Special:Contributions/' + encodeURIComponent( username ) + '?limit=1' ).then( response => {
  22. var body = response.body;
  23. if ( response.statusCode !== 200 || !body ) {
  24. console.log( '- ' + response.statusCode + ': Error while getting the global block.' );
  25. }
  26. else {
  27. let $ = cheerio.load(body);
  28. if ( $('#mw-content-text .errorbox').length ) {
  29. if ( msg.showEmbed() ) embed.addField( lang.get('user.gblock.disabled'), '\u200b' );
  30. else text += '\n\n**' + lang.get('user.gblock.disabled') + '**';
  31. }
  32. else if ( $('.mw-warning-with-logexcerpt').length && !$(".mw-warning-with-logexcerpt .mw-logline-block").length ) {
  33. if ( msg.showEmbed() ) embed.addField( lang.get('user.gblock.header', username).escapeFormatting(), '\u200b' );
  34. else text += '\n\n**' + lang.get('user.gblock.header', username).escapeFormatting() + '**';
  35. }
  36. }
  37. }, error => {
  38. console.log( '- Error while getting the global block: ' + error );
  39. } ).finally( () => {
  40. if ( !/^(?:(?:\d{1,3}\.){3}\d{1,3}(?:\/\d{2})?|(?:[\dA-F]{1,4}:){7}[\dA-F]{1,4}(?:\/\d{2,3})?)$/.test(username) ) {
  41. got.get( 'https://community.fandom.com/wiki/Special:Editcount/' + encodeURIComponent( username ) ).then( gresponse => {
  42. var gbody = gresponse.body;
  43. if ( gresponse.statusCode !== 200 || !gbody ) {
  44. console.log( '- ' + gresponse.statusCode + ': Error while getting the global edit count.' );
  45. }
  46. else {
  47. let $ = cheerio.load(gbody);
  48. var globaledits = $('#editcount .TablePager th').eq(7).text().replace( /[,\.]/g, '' );
  49. if ( globaledits ) {
  50. if ( msg.showEmbed() ) embed.spliceFields(1, 0, {name:lang.get('user.info.globaleditcount'),value:'[' + globaledits + '](https://community.fandom.com/wiki/Special:Editcount/' + username.toTitle(true) + ')',inline:true});
  51. else {
  52. let splittext = text.split('\n');
  53. splittext.splice(5, 0, lang.get('user.info.globaleditcount') + ' ' + globaledits);
  54. text = splittext.join('\n');
  55. }
  56. }
  57. }
  58. }, error => {
  59. console.log( '- Error while getting the global edit count: ' + error );
  60. } ).finally( () => {
  61. msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  62. } );
  63. }
  64. else msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  65. } );
  66. else if ( wiki.endsWith( '.gamepedia.com/' ) ) got.get( 'https://help.gamepedia.com/Special:GlobalBlockList/' + encodeURIComponent( username ) + '?uselang=qqx' ).then( response => {
  67. var body = response.body;
  68. if ( response.statusCode !== 200 || !body ) {
  69. console.log( '- ' + response.statusCode + ': Error while getting the global block.' );
  70. }
  71. else {
  72. let $ = cheerio.load(body);
  73. var gblocklist = $('.mw-blocklist');
  74. let splittext = text.split('\n\n');
  75. if ( gblocklist.length ) gblocklist.find('tbody tr').each( (i, gblock) => {
  76. gblock = $(gblock);
  77. var reason = gblock.find('.TablePager_col_reason').text().replace( /\)$/, '' ).split(', ');
  78. var timestamp = new Date(gblock.find('.TablePager_col_timestamp').text().replace( /(\d{2}:\d{2}), (\d{1,2}) \((\w+)\) (\d{4})/, '$3 $2, $4 $1 UTC' )).toLocaleString(lang.get('dateformat'), timeoptions);
  79. var expiry = gblock.find('.TablePager_col_expiry').text();
  80. if ( expiry.startsWith( '(infiniteblock)' ) ) expiry = lang.get('user.block.until_infinity');
  81. else expiry = new Date(expiry.replace( /(\d{2}:\d{2}), (\d{1,2}) \((\w+)\) (\d{4})/, '$3 $2, $4 $1 UTC' )).toLocaleString(lang.get('dateformat'), timeoptions);
  82. if ( msg.showEmbed() ) {
  83. var gblocktitle = lang.get('user.gblock.header', username).escapeFormatting();
  84. var globalblock = embed.fields.find( field => field.inline === false && field.name === lang.get('user.block.header', username).escapeFormatting() && field.value.replace( /\[([^\]]*)\]\([^\)]*\)/g, '$1' ) === lang.get('user.block.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, reason[1].escapeFormatting(), reason.slice(4).join(', ').escapeFormatting()) );
  85. if ( globalblock ) globalblock.name = gblocktitle;
  86. else {
  87. var block_wiki = reason[3].replace( /Special:BlockList$/, '' );
  88. var gblocktext = lang.get('user.gblock.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, '[' + reason[1] + '](' + block_wiki + 'User:' + reason[1].toTitle(true) + ')', '[' + reason[2] + '](' + block_wiki + 'Special:Contribs/' + username.toTitle(true) + ')', reason.slice(4).join(', ').escapeFormatting());
  89. embed.addField( gblocktitle, gblocktext );
  90. }
  91. }
  92. else {
  93. var globalblock = splittext.indexOf('**' + lang.get('user.block.header', username).escapeFormatting() + '**\n' + lang.get('user.block.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, reason[1].escapeFormatting(), reason.slice(4).join(', ').escapeFormatting()));
  94. if ( globalblock !== -1 ) splittext[globalblock] = '**' + lang.get('user.gblock.header', username).escapeFormatting() + '**\n' + lang.get('user.block.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, reason[1].escapeFormatting(), reason.slice(4).join(', ').escapeFormatting());
  95. else splittext.push('**' + lang.get('user.gblock.header', username).escapeFormatting() + '**\n' + lang.get('user.gblock.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, reason[1].escapeFormatting(), reason[2], reason.slice(4).join(', ').escapeFormatting()));
  96. }
  97. } );
  98. text = splittext.join('\n\n');
  99. }
  100. }, error => {
  101. console.log( '- Error while getting the global block: ' + error );
  102. } ).finally( () => {
  103. if ( !/^(?:(?:\d{1,3}\.){3}\d{1,3}(?:\/\d{2})?|(?:[\dA-F]{1,4}:){7}[\dA-F]{1,4}(?:\/\d{2,3})?)$/.test(username) ) {
  104. got.get( 'https://help.gamepedia.com/UserProfile:' + encodeURIComponent( username ) ).then( gresponse => {
  105. var gbody = gresponse.body;
  106. if ( gresponse.statusCode !== 200 || !gbody ) {
  107. console.log( '- ' + gresponse.statusCode + ': Error while getting the global edit count.' );
  108. }
  109. else {
  110. let $ = cheerio.load(gbody);
  111. var wikisedited = $('.curseprofile .rightcolumn .section.stats dd').eq(0).text().replace( /[,\.]/g, '' );
  112. if ( wikisedited ) {
  113. if ( msg.showEmbed() ) embed.spliceFields(1, 0, {name:lang.get('user.info.wikisedited'),value:wikisedited,inline:true});
  114. else {
  115. let splittext = text.split('\n');
  116. splittext.splice(5, 0, lang.get('user.info.wikisedited') + ' ' + wikisedited);
  117. text = splittext.join('\n');
  118. }
  119. }
  120. var globaledits = $('.curseprofile .rightcolumn .section.stats dd').eq(2).text().replace( /[,\.]/g, '' );
  121. if ( globaledits ) {
  122. if ( msg.showEmbed() ) embed.spliceFields(1, 0, {name:lang.get('user.info.globaleditcount'),value:'[' + globaledits + '](https://help.gamepedia.com/Gamepedia_Help_Wiki:Global_user_tracker#' + wiki.replace( /^https:\/\/([a-z\d-]{1,50})\.gamepedia\.com\/$/, '$1/' ) + username.toTitle(true) + ')',inline:true});
  123. else {
  124. let splittext = text.split('\n');
  125. splittext.splice(5, 0, lang.get('user.info.globaleditcount') + ' ' + globaledits);
  126. text = splittext.join('\n');
  127. }
  128. }
  129. }
  130. }, error => {
  131. console.log( '- Error while getting the global edit count: ' + error );
  132. } ).finally( () => {
  133. msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  134. } );
  135. }
  136. else msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  137. } );
  138. }
  139. module.exports = global_block;