global_block.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * @param {String} [gender] - The gender of the user.
  13. */
  14. function global_block(lang, msg, username, text, embed, wiki, spoiler, gender) {
  15. if ( !msg || msg.channel.type !== 'text' || !( msg.guild.id in patreons ) ) return;
  16. var isUser = true;
  17. if ( !gender ) {
  18. isUser = false;
  19. gender = 'unknown';
  20. }
  21. if ( msg.showEmbed() ) embed.fields.pop();
  22. else {
  23. let splittext = text.split('\n\n');
  24. splittext.pop();
  25. text = splittext.join('\n\n');
  26. }
  27. if ( wiki.isFandom() ) Promise.all([
  28. got.get( 'https://community.fandom.com/Special:Contributions/' + encodeURIComponent( username ) + '?limit=1', {
  29. responseType: 'text'
  30. } ).then( response => {
  31. var body = response.body;
  32. if ( response.statusCode !== 200 || !body ) {
  33. console.log( '- ' + response.statusCode + ': Error while getting the global block.' );
  34. }
  35. else {
  36. let $ = cheerio.load(body);
  37. if ( $('#mw-content-text .errorbox').length ) {
  38. if ( msg.showEmbed() ) embed.addField( lang.get('user.gblock.disabled'), '\u200b' );
  39. else text += '\n\n**' + lang.get('user.gblock.disabled') + '**';
  40. }
  41. else if ( $('.mw-warning-with-logexcerpt').length && !$(".mw-warning-with-logexcerpt .mw-logline-block").length ) {
  42. if ( msg.showEmbed() ) embed.addField( lang.get('user.gblock.header', username, gender).escapeFormatting(), '\u200b' );
  43. else text += '\n\n**' + lang.get('user.gblock.header', username, gender).escapeFormatting() + '**';
  44. }
  45. }
  46. }, error => {
  47. console.log( '- Error while getting the global block: ' + error );
  48. } ),
  49. ( isUser ? got.get( 'https://community.fandom.com/wiki/Special:Editcount/' + encodeURIComponent( username ), {
  50. responseType: 'text'
  51. } ).then( gresponse => {
  52. var gbody = gresponse.body;
  53. if ( gresponse.statusCode !== 200 || !gbody ) {
  54. console.log( '- ' + gresponse.statusCode + ': Error while getting the global edit count.' );
  55. }
  56. else {
  57. let $ = cheerio.load(gbody);
  58. var globaledits = $('#editcount .TablePager th').eq(7).text().replace( /[,\.]/g, '' );
  59. if ( globaledits ) {
  60. if ( msg.showEmbed() ) embed.spliceFields(1, 0, {
  61. name: lang.get('user.info.globaleditcount'),
  62. value: '[' + globaledits + '](https://community.fandom.com/wiki/Special:Editcount/' + username.toTitle(true) + ')',
  63. inline: true
  64. });
  65. else {
  66. let splittext = text.split('\n');
  67. splittext.splice(5, 0, lang.get('user.info.globaleditcount') + ' ' + globaledits);
  68. text = splittext.join('\n');
  69. }
  70. }
  71. }
  72. }, error => {
  73. console.log( '- Error while getting the global edit count: ' + error );
  74. } ) : undefined )
  75. ]).finally( () => {
  76. msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  77. } );
  78. else if ( wiki.endsWith( '.gamepedia.com/' ) ) Promise.all([
  79. got.get( 'https://help.gamepedia.com/Special:GlobalBlockList/' + encodeURIComponent( username ) + '?uselang=qqx', {
  80. responseType: 'text'
  81. } ).then( response => {
  82. var body = response.body;
  83. if ( response.statusCode !== 200 || !body ) {
  84. console.log( '- ' + response.statusCode + ': Error while getting the global block.' );
  85. }
  86. else {
  87. let $ = cheerio.load(body);
  88. var gblocklist = $('.mw-blocklist');
  89. let splittext = text.split('\n\n');
  90. if ( gblocklist.length ) gblocklist.find('tbody tr').each( (i, gblock) => {
  91. gblock = $(gblock);
  92. var reason = gblock.find('.TablePager_col_reason').text().replace( /\)$/, '' ).split(', ');
  93. 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);
  94. var expiry = gblock.find('.TablePager_col_expiry').text();
  95. if ( expiry.startsWith( '(infiniteblock)' ) ) expiry = lang.get('user.block.until_infinity');
  96. 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);
  97. if ( msg.showEmbed() ) {
  98. var gblocktitle = lang.get('user.gblock.header', username, gender).escapeFormatting();
  99. var globalblock = embed.fields.find( field => field.inline === false && field.name === lang.get('user.block.header', username, gender).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()) );
  100. if ( globalblock ) globalblock.name = gblocktitle;
  101. else {
  102. var block_wiki = reason[3].replace( /Special:BlockList$/, '' );
  103. 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());
  104. embed.addField( gblocktitle, gblocktext );
  105. }
  106. }
  107. else {
  108. var globalblock = splittext.indexOf('**' + lang.get('user.block.header', username, gender).escapeFormatting() + '**\n' + lang.get('user.block.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, reason[1].escapeFormatting(), reason.slice(4).join(', ').escapeFormatting()));
  109. if ( globalblock !== -1 ) splittext[globalblock] = '**' + lang.get('user.gblock.header', username, gender).escapeFormatting() + '**\n' + lang.get('user.block.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, reason[1].escapeFormatting(), reason.slice(4).join(', ').escapeFormatting());
  110. else splittext.push('**' + lang.get('user.gblock.header', username, gender).escapeFormatting() + '**\n' + lang.get('user.gblock.' + ( reason.length > 4 ? 'text' : 'noreason' ), timestamp, expiry, reason[1].escapeFormatting(), reason[2], reason.slice(4).join(', ').escapeFormatting()));
  111. }
  112. } );
  113. text = splittext.join('\n\n');
  114. }
  115. }, error => {
  116. console.log( '- Error while getting the global block: ' + error );
  117. } ),
  118. ( isUser ? got.get( 'https://help.gamepedia.com/UserProfile:' + encodeURIComponent( username ), {
  119. responseType: 'text'
  120. } ).then( gresponse => {
  121. var gbody = gresponse.body;
  122. if ( gresponse.statusCode !== 200 || !gbody ) {
  123. console.log( '- ' + gresponse.statusCode + ': Error while getting the global edit count.' );
  124. }
  125. else {
  126. let $ = cheerio.load(gbody);
  127. var wikisedited = $('.curseprofile .rightcolumn .section.stats dd').eq(0).text().replace( /[,\.]/g, '' );
  128. if ( wikisedited ) {
  129. if ( msg.showEmbed() ) embed.spliceFields(1, 0, {
  130. name: lang.get('user.info.wikisedited'),
  131. value: wikisedited,
  132. inline: true
  133. });
  134. else {
  135. let splittext = text.split('\n');
  136. splittext.splice(5, 0, lang.get('user.info.wikisedited') + ' ' + wikisedited);
  137. text = splittext.join('\n');
  138. }
  139. }
  140. var globaledits = $('.curseprofile .rightcolumn .section.stats dd').eq(2).text().replace( /[,\.]/g, '' );
  141. if ( globaledits ) {
  142. if ( msg.showEmbed() ) embed.spliceFields(1, 0, {
  143. name: lang.get('user.info.globaleditcount'),
  144. 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) + ')',
  145. inline: true
  146. });
  147. else {
  148. let splittext = text.split('\n');
  149. splittext.splice(5, 0, lang.get('user.info.globaleditcount') + ' ' + globaledits);
  150. text = splittext.join('\n');
  151. }
  152. }
  153. if ( msg.showEmbed() ) {
  154. var avatar = $('.curseprofile .mainavatar img').prop('src');
  155. if ( avatar ) embed.setThumbnail( avatar.replace( /^(?:https?:)?\/\//, 'https://' ).replace( '?d=mm&s=96', '?d=404' ) );
  156. }
  157. }
  158. }, error => {
  159. console.log( '- Error while getting the global edit count: ' + error );
  160. } ) : undefined )
  161. ]).finally( () => {
  162. msg.edit( spoiler + text + spoiler, {embed,allowedMentions:{parse:[]}} ).catch(log_error);
  163. } );
  164. }
  165. module.exports = global_block;