1
0

global_block.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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', {
  22. responseType: 'text'
  23. } ).then( response => {
  24. var body = response.body;
  25. if ( response.statusCode !== 200 || !body ) {
  26. console.log( '- ' + response.statusCode + ': Error while getting the global block.' );
  27. }
  28. else {
  29. let $ = cheerio.load(body);
  30. if ( $('#mw-content-text .errorbox').length ) {
  31. if ( msg.showEmbed() ) embed.addField( lang.get('user.gblock.disabled'), '\u200b' );
  32. else text += '\n\n**' + lang.get('user.gblock.disabled') + '**';
  33. }
  34. else if ( $('.mw-warning-with-logexcerpt').length && !$(".mw-warning-with-logexcerpt .mw-logline-block").length ) {
  35. if ( msg.showEmbed() ) embed.addField( lang.get('user.gblock.header', username).escapeFormatting(), '\u200b' );
  36. else text += '\n\n**' + lang.get('user.gblock.header', username).escapeFormatting() + '**';
  37. }
  38. }
  39. }, error => {
  40. console.log( '- Error while getting the global block: ' + error );
  41. } ).finally( () => {
  42. if ( !/^(?:(?:\d{1,3}\.){3}\d{1,3}(?:\/\d{2})?|(?:[\dA-F]{1,4}:){7}[\dA-F]{1,4}(?:\/\d{2,3})?)$/.test(username) ) {
  43. got.get( 'https://community.fandom.com/wiki/Special:Editcount/' + encodeURIComponent( username ), {
  44. responseType: 'text'
  45. } ).then( gresponse => {
  46. var gbody = gresponse.body;
  47. if ( gresponse.statusCode !== 200 || !gbody ) {
  48. console.log( '- ' + gresponse.statusCode + ': Error while getting the global edit count.' );
  49. }
  50. else {
  51. let $ = cheerio.load(gbody);
  52. var globaledits = $('#editcount .TablePager th').eq(7).text().replace( /[,\.]/g, '' );
  53. if ( globaledits ) {
  54. 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});
  55. else {
  56. let splittext = text.split('\n');
  57. splittext.splice(5, 0, lang.get('user.info.globaleditcount') + ' ' + globaledits);
  58. text = splittext.join('\n');
  59. }
  60. }
  61. }
  62. }, error => {
  63. console.log( '- Error while getting the global edit count: ' + error );
  64. } ).finally( () => {
  65. msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  66. } );
  67. }
  68. else msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  69. } );
  70. else if ( wiki.endsWith( '.gamepedia.com/' ) ) got.get( 'https://help.gamepedia.com/Special:GlobalBlockList/' + encodeURIComponent( username ) + '?uselang=qqx', {
  71. responseType: 'text'
  72. } ).then( response => {
  73. var body = response.body;
  74. if ( response.statusCode !== 200 || !body ) {
  75. console.log( '- ' + response.statusCode + ': Error while getting the global block.' );
  76. }
  77. else {
  78. let $ = cheerio.load(body);
  79. var gblocklist = $('.mw-blocklist');
  80. let splittext = text.split('\n\n');
  81. if ( gblocklist.length ) gblocklist.find('tbody tr').each( (i, gblock) => {
  82. gblock = $(gblock);
  83. var reason = gblock.find('.TablePager_col_reason').text().replace( /\)$/, '' ).split(', ');
  84. 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);
  85. var expiry = gblock.find('.TablePager_col_expiry').text();
  86. if ( expiry.startsWith( '(infiniteblock)' ) ) expiry = lang.get('user.block.until_infinity');
  87. 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);
  88. if ( msg.showEmbed() ) {
  89. var gblocktitle = lang.get('user.gblock.header', username).escapeFormatting();
  90. 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()) );
  91. if ( globalblock ) globalblock.name = gblocktitle;
  92. else {
  93. var block_wiki = reason[3].replace( /Special:BlockList$/, '' );
  94. 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());
  95. embed.addField( gblocktitle, gblocktext );
  96. }
  97. }
  98. else {
  99. 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()));
  100. 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());
  101. 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()));
  102. }
  103. } );
  104. text = splittext.join('\n\n');
  105. }
  106. }, error => {
  107. console.log( '- Error while getting the global block: ' + error );
  108. } ).finally( () => {
  109. if ( !/^(?:(?:\d{1,3}\.){3}\d{1,3}(?:\/\d{2})?|(?:[\dA-F]{1,4}:){7}[\dA-F]{1,4}(?:\/\d{2,3})?)$/.test(username) ) {
  110. got.get( 'https://help.gamepedia.com/UserProfile:' + encodeURIComponent( username ), {
  111. responseType: 'text'
  112. } ).then( gresponse => {
  113. var gbody = gresponse.body;
  114. if ( gresponse.statusCode !== 200 || !gbody ) {
  115. console.log( '- ' + gresponse.statusCode + ': Error while getting the global edit count.' );
  116. }
  117. else {
  118. let $ = cheerio.load(gbody);
  119. var wikisedited = $('.curseprofile .rightcolumn .section.stats dd').eq(0).text().replace( /[,\.]/g, '' );
  120. if ( wikisedited ) {
  121. if ( msg.showEmbed() ) embed.spliceFields(1, 0, {name:lang.get('user.info.wikisedited'),value:wikisedited,inline:true});
  122. else {
  123. let splittext = text.split('\n');
  124. splittext.splice(5, 0, lang.get('user.info.wikisedited') + ' ' + wikisedited);
  125. text = splittext.join('\n');
  126. }
  127. }
  128. var globaledits = $('.curseprofile .rightcolumn .section.stats dd').eq(2).text().replace( /[,\.]/g, '' );
  129. if ( globaledits ) {
  130. 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});
  131. else {
  132. let splittext = text.split('\n');
  133. splittext.splice(5, 0, lang.get('user.info.globaleditcount') + ' ' + globaledits);
  134. text = splittext.join('\n');
  135. }
  136. }
  137. }
  138. }, error => {
  139. console.log( '- Error while getting the global edit count: ' + error );
  140. } ).finally( () => {
  141. msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  142. } );
  143. }
  144. else msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  145. } );
  146. }
  147. module.exports = global_block;