1
0

bug.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const {MessageEmbed} = require('discord.js');
  2. /**
  3. * Sends a Minecraft issue.
  4. * @param {import('../../util/i18n.js')} lang - The user language.
  5. * @param {import('discord.js').Message} msg - The Discord message.
  6. * @param {String[]} args - The command arguments.
  7. * @param {String} title - The page title.
  8. * @param {String} cmd - The command at this point.
  9. * @param {String} querystring - The querystring for the link.
  10. * @param {String} fragment - The section for the link.
  11. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  12. * @param {String} spoiler - If the response is in a spoiler.
  13. */
  14. function minecraft_bug(lang, msg, args, title, cmd, querystring, fragment, reaction, spoiler) {
  15. var invoke = args[0];
  16. args = args.slice(1);
  17. if ( invoke && /\d+$/.test(invoke) && !args.length ) {
  18. if ( /^\d+$/.test(invoke) ) invoke = 'MC-' + invoke;
  19. var link = 'https://bugs.mojang.com/browse/';
  20. got.get( 'https://bugs.mojang.com/rest/api/2/issue/' + encodeURIComponent( invoke ) + '?fields=summary,issuelinks,fixVersions,resolution,status', {
  21. responseType: 'json'
  22. } ).then( response => {
  23. var body = response.body;
  24. if ( response.statusCode !== 200 || !body || body['status-code'] === 404 || body.errorMessages || body.errors ) {
  25. if ( body && body.errorMessages ) {
  26. if ( body.errorMessages.includes( 'Issue Does Not Exist' ) ) {
  27. msg.reactEmoji('🤷');
  28. }
  29. else if ( body.errorMessages.includes( 'You do not have the permission to see the specified issue.' ) ) {
  30. msg.sendChannel( spoiler + lang.get('minecraft.private') + '\n<' + link + invoke + '>' + spoiler );
  31. }
  32. else {
  33. console.log( '- ' + ( response && response.statusCode ) + ': Error while getting the issue: ' + body.errorMessages.join(' - ') );
  34. msg.reactEmoji('error');
  35. }
  36. }
  37. else {
  38. console.log( '- ' + response.statusCode + ': Error while getting the issue: ' + ( body && body.message ) );
  39. if ( body && body['status-code'] === 404 ) msg.reactEmoji('error');
  40. else msg.sendChannelError( spoiler + '<' + link + invoke + '>' + spoiler );
  41. }
  42. }
  43. else {
  44. if ( !body.fields ) {
  45. msg.reactEmoji('error');
  46. }
  47. else {
  48. var bugs = body.fields.issuelinks.filter( bug => bug.outwardIssue || ( bug.inwardIssue && bug.type.name != 'Duplicate' ) );
  49. if ( bugs.length ) {
  50. var embed = new MessageEmbed();
  51. var extrabugs = [];
  52. bugs.forEach( bug => {
  53. var ward = ( bug.outwardIssue ? 'outward' : 'inward' );
  54. var issue = bug[ward + 'Issue'];
  55. var name = bug.type[ward] + ' ' + issue.key;
  56. var value = issue.fields.status.name + ': [' + issue.fields.summary.escapeFormatting() + '](' + link + issue.key + ')';
  57. if ( embed.fields.length < 25 ) embed.addField( name, value );
  58. else extrabugs.push({name,value,inline:false});
  59. } );
  60. if ( extrabugs.length ) embed.setFooter( lang.get('minecraft.more').replaceSave( '%s', extrabugs.length ) );
  61. }
  62. var status = '**' + ( body.fields.resolution ? body.fields.resolution.name : body.fields.status.name ) + ':** ';
  63. var fixed = '';
  64. if ( body.fields.resolution && body.fields.fixVersions && body.fields.fixVersions.length ) {
  65. fixed = '\n' + lang.get('minecraft.fixed') + ' ' + body.fields.fixVersions.map( v => v.name ).join(', ');
  66. }
  67. msg.sendChannel( spoiler + status + body.fields.summary.escapeFormatting() + '\n<' + link + body.key + '>' + fixed + spoiler, {embed} );
  68. }
  69. }
  70. }, error => {
  71. console.log( '- Error while getting the issue: ' + error );
  72. msg.sendChannelError( spoiler + '<' + link + invoke + '>' + spoiler );
  73. } ).finally( () => {
  74. if ( reaction ) reaction.removeEmoji();
  75. } );
  76. }
  77. else if ( invoke && invoke.toLowerCase() === 'version' && args.length && args.join(' ').length < 100 ) {
  78. var jql = 'fixVersion="' + args.join(' ').replace( /(["\\])/g, '\\$1' ).toSearch() + '"+order+by+key';
  79. var link = 'https://bugs.mojang.com/issues/?jql=' + jql;
  80. got.get( 'https://bugs.mojang.com/rest/api/2/search?fields=summary,resolution,status&jql=' + jql + '&maxResults=25', {
  81. responseType: 'json'
  82. } ).then( response => {
  83. var body = response.body;
  84. if ( response.statusCode !== 200 || !body || body['status-code'] === 404 || body.errorMessages || body.errors ) {
  85. if ( body && body.errorMessages ) {
  86. if ( body.errorMessages.includes( 'The value \'' + args.join(' ') + '\' does not exist for the field \'fixVersion\'.' ) ) {
  87. msg.reactEmoji('🤷');
  88. }
  89. else {
  90. console.log( '- ' + response.statusCode + ': Error while getting the issues: ' + body.errorMessages.join(' - ') );
  91. msg.reactEmoji('error');
  92. }
  93. }
  94. else {
  95. console.log( '- ' + response.statusCode + ': Error while getting the issues: ' + ( body && body.message ) );
  96. if ( body && body['status-code'] === 404 ) msg.reactEmoji('error');
  97. else msg.sendChannelError( spoiler + '<' + link + '>' + spoiler );
  98. }
  99. }
  100. else {
  101. if ( !body.issues ) {
  102. msg.reactEmoji('error');
  103. }
  104. else {
  105. if ( body.total > 0 ) {
  106. var embed = new MessageEmbed();
  107. body.issues.forEach( bug => {
  108. var status = ( bug.fields.resolution ? bug.fields.resolution.name : bug.fields.status.name );
  109. var value = status + ': [' + bug.fields.summary.escapeFormatting() + '](https://bugs.mojang.com/browse/' + bug.key + ')';
  110. embed.addField( bug.key, value );
  111. } );
  112. if ( body.total > 25 ) embed.setFooter( lang.get('minecraft.more').replaceSave( '%s', body.total - 25 ) );
  113. }
  114. var total = '**' + args.join(' ') + ':** ' + lang.get('minecraft.total').replaceSave( '%s', body.total );
  115. msg.sendChannel( spoiler + total + '\n<' + link + '>' + spoiler, {embed} );
  116. }
  117. }
  118. }, error => {
  119. console.log( '- Error while getting the issues: ' + error );
  120. msg.sendChannelError( spoiler + '<' + link + '>' + spoiler );
  121. } ).finally( () => {
  122. if ( reaction ) reaction.removeEmoji();
  123. } );
  124. }
  125. else {
  126. msg.notMinecraft = true;
  127. this.WIKI.gamepedia(lang, msg, title, lang.get('minecraft.link'), cmd, reaction, spoiler, querystring, fragment);
  128. }
  129. }
  130. module.exports = {
  131. name: 'bug',
  132. run: minecraft_bug
  133. };