global_block.js 8.5 KB

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