bug.js 6.0 KB

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