bug.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {MessageEmbed} from 'discord.js';
  2. import {got, escapeFormatting, limitLength} from '../../util/functions.js';
  3. /**
  4. * Sends a Minecraft issue.
  5. * @param {import('../../util/i18n.js').default} lang - The user language.
  6. * @param {import('discord.js').Message} msg - The Discord message.
  7. * @param {import('../../util/wiki.js').default} wiki - The wiki.
  8. * @param {String[]} args - The command arguments.
  9. * @param {String} title - The page title.
  10. * @param {String} cmd - The command at this point.
  11. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  12. * @param {String} spoiler - If the response is in a spoiler.
  13. * @param {Boolean} noEmbed - If the response should be without an embed.
  14. */
  15. function minecraft_bug(lang, msg, wiki, args, title, cmd, reaction, spoiler, noEmbed) {
  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 = escapeFormatting(body.fields.summary);
  49. if ( summary.length > 250 ) summary = summary.substring(0, 250) + '\u2026';
  50. var description = parse_links( ( body.fields.description || '' ).replace( /\{code\}/g, '```' ) );
  51. var embed = null;
  52. if ( msg.showEmbed() && !noEmbed ) {
  53. embed = new MessageEmbed().setAuthor( 'Mojira' ).setTitle( summary ).setURL( baseBrowseUrl + body.key ).setDescription( limitLength(description, 2000, 20) );
  54. var links = body.fields.issuelinks.filter( link => link.outwardIssue || ( link.inwardIssue && link.type.name !== 'Duplicate' ) );
  55. if ( links.length ) {
  56. var linkList = lang.get('minecraft.issue_link');
  57. var extralinks = [];
  58. links.forEach( link => {
  59. var ward = ( link.outwardIssue ? 'outward' : 'inward' );
  60. var issue = link[ward + 'Issue']; // looks for property (in|out)wardIssue
  61. var name = ( linkList?.[link.type.name]?.[ward]?.replaceSave( /\$1/g, issue.key ) || link.type[ward] + ' ' + issue.key );
  62. var status = issue.fields.status.name;
  63. var value = ( statusList?.[status] || status ) + ': [' + escapeFormatting(issue.fields.summary) + '](' + baseBrowseUrl + issue.key + ')';
  64. if ( embed.fields.length < 25 && ( embed.length + name.length + value.length ) < 6000 ) embed.addField( name, value );
  65. else extralinks.push({name,value,inline:false});
  66. } );
  67. if ( extralinks.length ) embed.setFooter( lang.get('minecraft.more', extralinks.length.toLocaleString(lang.get('dateformat')), extralinks.length) );
  68. }
  69. }
  70. var status = ( body.fields.resolution ? body.fields.resolution.name : body.fields.status.name );
  71. var fixed = '';
  72. if ( body.fields.resolution && body.fields.fixVersions && body.fields.fixVersions.length ) {
  73. fixed = '\n' + lang.get('minecraft.fixed', body.fields.fixVersions.length) + ' ' + body.fields.fixVersions.map( v => v.name ).join(', ');
  74. }
  75. msg.sendChannel( {content: spoiler + '**' + ( statusList?.[status] || status ) + '**: ' + escapeFormatting(body.fields.summary) + '\n<' + baseBrowseUrl + body.key + '>' + fixed + spoiler, embeds: [embed]} );
  76. }
  77. }
  78. }, error => {
  79. console.log( '- Error while getting the issue: ' + error );
  80. msg.sendChannelError( spoiler + '<' + baseBrowseUrl + invoke + '>' + spoiler );
  81. } ).finally( () => {
  82. if ( reaction ) reaction.removeEmoji();
  83. } );
  84. }
  85. else if ( invoke && invoke.toLowerCase() === 'version' && args.length && args.join(' ').length < 100 ) {
  86. var jql = new URLSearchParams({
  87. jql: 'fixVersion="' + args.join(' ').replace( /["\\]/g, '\\$&' ) + '" order by key'
  88. });
  89. var uri = 'https://bugs.mojang.com/issues/?' + jql;
  90. got.get( 'https://bugs.mojang.com/rest/api/2/search?fields=summary,resolution,status&' + jql + '&maxResults=25' ).then( response => {
  91. var body = response.body;
  92. if ( response.statusCode !== 200 || !body || body['status-code'] === 404 || body.errorMessages || body.errors ) {
  93. if ( body && body.errorMessages ) {
  94. if ( body.errorMessages.includes( 'The value \'' + args.join(' ') + '\' does not exist for the field \'fixVersion\'.' ) ) {
  95. msg.reactEmoji('🤷');
  96. }
  97. else {
  98. console.log( '- ' + response.statusCode + ': Error while getting the issues: ' + body.errorMessages.join(' - ') );
  99. msg.reactEmoji('error');
  100. }
  101. }
  102. else {
  103. console.log( '- ' + response.statusCode + ': Error while getting the issues: ' + ( body && body.message ) );
  104. if ( body && body['status-code'] === 404 ) msg.reactEmoji('error');
  105. else msg.sendChannelError( spoiler + '<' + uri + '>' + spoiler );
  106. }
  107. }
  108. else {
  109. if ( !body.issues ) {
  110. msg.reactEmoji('error');
  111. }
  112. else {
  113. var embed = null;
  114. if ( msg.showEmbed() && !noEmbed ) {
  115. embed = new MessageEmbed().setAuthor( 'Mojira' ).setTitle( args.join(' ') ).setURL( uri );
  116. if ( body.total > 0 ) {
  117. var statusList = lang.get('minecraft.status');
  118. body.issues.forEach( bug => {
  119. var status = ( bug.fields.resolution ? bug.fields.resolution.name : bug.fields.status.name );
  120. var value = ( statusList?.[status] || status ) + ': [' + escapeFormatting(bug.fields.summary) + '](https://bugs.mojang.com/browse/' + bug.key + ')';
  121. embed.addField( bug.key, value );
  122. } );
  123. if ( body.total > 25 ) {
  124. var extrabugs = body.total - 25;
  125. embed.setFooter( lang.get('minecraft.more', extrabugs.toLocaleString(lang.get('dateformat')), extrabugs) );
  126. }
  127. }
  128. }
  129. var total = '**' + args.join(' ') + ':** ' + lang.get('minecraft.total', body.total.toLocaleString(lang.get('dateformat')), body.total);
  130. msg.sendChannel( {content: spoiler + total + '\n<' + uri + '>' + spoiler, embeds: [embed]} );
  131. }
  132. }
  133. }, error => {
  134. console.log( '- Error while getting the issues: ' + error );
  135. msg.sendChannelError( spoiler + '<' + uri + '>' + spoiler );
  136. } ).finally( () => {
  137. if ( reaction ) reaction.removeEmoji();
  138. } );
  139. }
  140. else {
  141. msg.notMinecraft = true;
  142. this.WIKI.general(lang, msg, title, wiki, cmd, reaction, spoiler, noEmbed);
  143. }
  144. }
  145. /**
  146. * Parse Mojira links.
  147. * @param {String} text - The text to parse.
  148. * @returns {String}
  149. */
  150. function parse_links(text) {
  151. text = text.replace( /\[~([^\]]+)\]/g, '[$1](https://bugs.mojang.com/secure/ViewProfile.jspa?name=$1)' );
  152. text = text.replace( /\[([^\|]+)\|([^\]]+)\]/g, '[$1]($2)' );
  153. text = text.replace( /{panel(?::title=([^|}]+))?[^}]*}/g, (panel, title) => {
  154. return ( title ? '**' + title + '**' : '' );
  155. } );
  156. return text;
  157. }
  158. export default {
  159. name: 'bug',
  160. run: minecraft_bug
  161. };