bug.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 baseBrowseUrl = 'https://bugs.mojang.com/browse/';
  21. got.get( 'https://bugs.mojang.com/rest/api/2/issue/' + encodeURIComponent( invoke ) + '?fields=summary,description,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<' + baseBrowseUrl + 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 + '<' + baseBrowseUrl + invoke + '>' + spoiler );
  40. }
  41. }
  42. else {
  43. if ( !body.fields ) {
  44. msg.reactEmoji('error');
  45. }
  46. else {
  47. var statusList = lang.get('minecraft.status');
  48. var summary = body.fields.summary.escapeFormatting();
  49. if ( summary.length > 250 ) summary = summary.substring(0, 250) + '\u2026';
  50. var description = ( body.fields.description || '' ).replace( /\{code\}/g, '```' );
  51. if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
  52. var embed = new MessageEmbed().setAuthor( 'Mojira' ).setTitle( summary ).setURL( baseBrowseUrl + body.key ).setDescription( parse_links( description ) );
  53. var links = body.fields.issuelinks.filter( link => link.outwardIssue || ( link.inwardIssue && link.type.name !== 'Duplicate' ) );
  54. if ( links.length ) {
  55. var linkList = lang.get('minecraft.issue_link');
  56. var extralinks = [];
  57. links.forEach( link => {
  58. var ward = ( link.outwardIssue ? 'outward' : 'inward' );
  59. var issue = link[ward + 'Issue']; // looks for property (in|out)wardIssue
  60. var name = ( linkList?.[link.type.name]?.[ward]?.replaceSave( /\$1/g, issue.key ) || link.type[ward] + ' ' + issue.key );
  61. var status = issue.fields.status.name;
  62. var value = ( statusList?.[status] || status ) + ': [' + issue.fields.summary.escapeFormatting() + '](' + baseBrowseUrl + issue.key + ')';
  63. if ( embed.fields.length < 25 && ( embed.length + name.length + value.length ) < 6000 ) embed.addField( name, value );
  64. else extralinks.push({name,value,inline:false});
  65. } );
  66. if ( extralinks.length ) embed.setFooter( lang.get('minecraft.more', extralinks.length.toLocaleString(lang.get('dateformat')), extralinks.length) );
  67. }
  68. var status = ( body.fields.resolution ? body.fields.resolution.name : body.fields.status.name );
  69. var fixed = '';
  70. if ( body.fields.resolution && body.fields.fixVersions && body.fields.fixVersions.length ) {
  71. fixed = '\n' + lang.get('minecraft.fixed', body.fields.fixVersions.length) + ' ' + body.fields.fixVersions.map( v => v.name ).join(', ');
  72. }
  73. msg.sendChannel( spoiler + '**' + ( statusList?.[status] || status ) + '**: ' + body.fields.summary.escapeFormatting() + '\n<' + baseBrowseUrl + body.key + '>' + fixed + spoiler, {embed} );
  74. }
  75. }
  76. }, error => {
  77. console.log( '- Error while getting the issue: ' + error );
  78. msg.sendChannelError( spoiler + '<' + baseBrowseUrl + invoke + '>' + spoiler );
  79. } ).finally( () => {
  80. if ( reaction ) reaction.removeEmoji();
  81. } );
  82. }
  83. else if ( invoke && invoke.toLowerCase() === 'version' && args.length && args.join(' ').length < 100 ) {
  84. var jql = new URLSearchParams({
  85. jql: 'fixVersion="' + args.join(' ').replace( /["\\]/g, '\\$&' ) + '" order by key'
  86. });
  87. var uri = 'https://bugs.mojang.com/issues/?' + jql;
  88. got.get( 'https://bugs.mojang.com/rest/api/2/search?fields=summary,resolution,status&' + jql + '&maxResults=25' ).then( response => {
  89. var body = response.body;
  90. if ( response.statusCode !== 200 || !body || body['status-code'] === 404 || body.errorMessages || body.errors ) {
  91. if ( body && body.errorMessages ) {
  92. if ( body.errorMessages.includes( 'The value \'' + args.join(' ') + '\' does not exist for the field \'fixVersion\'.' ) ) {
  93. msg.reactEmoji('🤷');
  94. }
  95. else {
  96. console.log( '- ' + response.statusCode + ': Error while getting the issues: ' + body.errorMessages.join(' - ') );
  97. msg.reactEmoji('error');
  98. }
  99. }
  100. else {
  101. console.log( '- ' + response.statusCode + ': Error while getting the issues: ' + ( body && body.message ) );
  102. if ( body && body['status-code'] === 404 ) msg.reactEmoji('error');
  103. else msg.sendChannelError( spoiler + '<' + uri + '>' + spoiler );
  104. }
  105. }
  106. else {
  107. if ( !body.issues ) {
  108. msg.reactEmoji('error');
  109. }
  110. else {
  111. var embed = new MessageEmbed().setAuthor( 'Mojira' ).setTitle( args.join(' ') ).setURL( uri );
  112. if ( body.total > 0 ) {
  113. var statusList = lang.get('minecraft.status');
  114. body.issues.forEach( bug => {
  115. var status = ( bug.fields.resolution ? bug.fields.resolution.name : bug.fields.status.name );
  116. var value = ( statusList?.[status] || status ) + ': [' + bug.fields.summary.escapeFormatting() + '](https://bugs.mojang.com/browse/' + bug.key + ')';
  117. embed.addField( bug.key, value );
  118. } );
  119. if ( body.total > 25 ) {
  120. var extrabugs = body.total - 25;
  121. embed.setFooter( lang.get('minecraft.more', extrabugs.toLocaleString(lang.get('dateformat')), extrabugs) );
  122. }
  123. }
  124. var total = '**' + args.join(' ') + ':** ' + lang.get('minecraft.total', body.total.toLocaleString(lang.get('dateformat')), body.total);
  125. msg.sendChannel( spoiler + total + '\n<' + uri + '>' + spoiler, {embed} );
  126. }
  127. }
  128. }, error => {
  129. console.log( '- Error while getting the issues: ' + error );
  130. msg.sendChannelError( spoiler + '<' + uri + '>' + spoiler );
  131. } ).finally( () => {
  132. if ( reaction ) reaction.removeEmoji();
  133. } );
  134. }
  135. else {
  136. msg.notMinecraft = true;
  137. this.WIKI.general(lang, msg, title, new Wiki(lang.get('minecraft.link')), cmd, reaction, spoiler, querystring, fragment);
  138. }
  139. }
  140. /**
  141. * Parse Mojira links.
  142. * @param {String} text - The text to parse.
  143. * @returns {String}
  144. */
  145. function parse_links(text) {
  146. text = text.replace( /\[~([^\]]+)\]/g, '[$1](https://bugs.mojang.com/secure/ViewProfile.jspa?name=$1)' );
  147. text = text.replace( /\[([^\|]+)\|([^\]]+)\]/g, '[$1]($2)' );
  148. return text;
  149. }
  150. module.exports = {
  151. name: 'bug',
  152. run: minecraft_bug
  153. };