syntax.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const Wiki = require('../../util/wiki.js');
  2. const commands = require('./commands.json');
  3. /**
  4. * Sends a Minecraft command.
  5. * @param {import('../../util/i18n.js')} lang - The user language.
  6. * @param {import('discord.js').Message} msg - The Discord message.
  7. * @param {import('../../util/wiki.js')} wiki - The wiki.
  8. * @param {String} mccmd - The Minecraft command argument.
  9. * @param {String[]} args - The command arguments.
  10. * @param {String} title - The page title.
  11. * @param {String} cmd - The command at this point.
  12. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  13. * @param {String} spoiler - If the response is in a spoiler.
  14. * @param {Boolean} noEmbed - If the response should be without an embed.
  15. */
  16. function minecraft_syntax(lang, msg, wiki, mccmd, args, title, cmd, reaction, spoiler, noEmbed) {
  17. mccmd = mccmd.toLowerCase();
  18. var aliasCmd = ( commands.aliases[mccmd] || mccmd );
  19. var cmdpage = commands.wikis[wiki.href];
  20. if ( commands.list.hasOwnProperty(aliasCmd) ) {
  21. var cmdSyntaxMap = commands.list[aliasCmd].map( command => {
  22. var cmdargs = command.split(' ');
  23. if ( cmdargs[0].startsWith( '/' ) ) cmdargs = cmdargs.slice(1);
  24. var argmatches = cmdargs.map( (arg, i) => {
  25. if ( arg === args[i] ) return true;
  26. } );
  27. var matchCount = 0;
  28. argmatches.forEach( match => {
  29. if ( match ) matchCount++;
  30. } );
  31. return [argmatches.lastIndexOf(true),matchCount];
  32. } );
  33. var lastIndex = Math.max(...cmdSyntaxMap.map( command => command[0] ));
  34. var matchCount = Math.max(...cmdSyntaxMap.filter( command => command[0] === lastIndex ).map( command => command[1] ));
  35. var regex = new RegExp('/' + aliasCmd, 'g');
  36. var cmdSyntax = commands.list[aliasCmd].filter( (command, i) => ( lastIndex === -1 || cmdSyntaxMap[i][0] === lastIndex ) && cmdSyntaxMap[i][1] === matchCount ).join('\n').replaceSave( regex, '/' + mccmd );
  37. got.get( wiki + ( cmdpage.endsWith( '/' ) ? 'api.php?action=query&redirects=true&converttitles=true&titles=%1F' + encodeURIComponent( cmdpage + aliasCmd ) : 'api.php?action=parse&redirects=true&prop=sections&page=' + encodeURIComponent( cmdpage ) ) + '&format=json' ).then( response => {
  38. var body = response.body;
  39. if ( body && body.warnings ) log_warn(body.warnings);
  40. if ( response.statusCode !== 200 || !( body?.query?.pages || body?.parse?.sections?.length ) ) {
  41. console.log( '- ' + response.statusCode + ': Error while getting the command page: ' + ( body && body.error && body.error.info ) );
  42. }
  43. else if ( cmdpage.endsWith( '/' ) ) {
  44. if ( body.query.pages['-1'] ) {
  45. wiki = new Wiki('https://minecraft.fandom.com/');
  46. cmdpage = 'Commands/';
  47. }
  48. else {
  49. cmdpage = Object.values(body.query.pages)[0].title;
  50. aliasCmd = ( body.query.redirects?.[0]?.tofragment || '' );
  51. }
  52. }
  53. else {
  54. cmdpage = body.parse.title;
  55. if ( !body.parse.sections.some( section => section.anchor === aliasCmd ) ) {
  56. if ( body.parse.sections.some( section => section.anchor === mccmd ) ) {
  57. aliasCmd = mccmd;
  58. }
  59. else {
  60. wiki = new Wiki('https://minecraft.fandom.com/');
  61. cmdpage = 'Commands/';
  62. }
  63. }
  64. }
  65. }, error => {
  66. console.log( '- Error while getting the command page: ' + error );
  67. } ).finally( () => {
  68. msg.sendChannel( spoiler + '```md\n' + cmdSyntax + '```<' + wiki.toLink(( cmdpage.endsWith( '/' ) ? cmdpage + aliasCmd : cmdpage ), '', ( cmdpage.endsWith( '/' ) ? '' : aliasCmd )) + '>' + spoiler, {split:{maxLength:2000,prepend:spoiler + '```md\n',append:'```' + spoiler}} );
  69. if ( reaction ) reaction.removeEmoji();
  70. } );
  71. }
  72. else {
  73. msg.notMinecraft = true;
  74. this.WIKI.general(lang, msg, title, wiki, cmd, reaction, spoiler, noEmbed);
  75. }
  76. }
  77. module.exports = {
  78. name: 'SYNTAX',
  79. run: minecraft_syntax
  80. };