bug.js 6.1 KB

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