1
0

syntax.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 {String} mccmd - The Minecraft command argument.
  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 {URLSearchParams} querystring - The querystring for the link.
  12. * @param {String} fragment - The section for the link.
  13. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  14. * @param {String} spoiler - If the response is in a spoiler.
  15. */
  16. function minecraft_syntax(lang, msg, mccmd, args, title, cmd, querystring, fragment, reaction, spoiler) {
  17. mccmd = mccmd.toLowerCase();
  18. var aliasCmd = ( commands.aliases[mccmd] || mccmd );
  19. if ( aliasCmd in commands.list ) {
  20. var cmdSyntaxMap = commands.list[aliasCmd].map( command => {
  21. var cmdargs = command.split(' ');
  22. if ( cmdargs[0].startsWith( '/' ) ) cmdargs = cmdargs.slice(1);
  23. var argmatches = cmdargs.map( (arg, i) => {
  24. if ( arg === args[i] ) return true;
  25. } );
  26. var matchCount = 0;
  27. argmatches.forEach( match => {
  28. if ( match ) matchCount++;
  29. } );
  30. return [argmatches.lastIndexOf(true),matchCount];
  31. } );
  32. var lastIndex = Math.max(...cmdSyntaxMap.map( command => command[0] ));
  33. var matchCount = Math.max(...cmdSyntaxMap.filter( command => command[0] === lastIndex ).map( command => command[1] ));
  34. var regex = new RegExp('/' + aliasCmd, 'g');
  35. var cmdSyntax = commands.list[aliasCmd].filter( (command, i) => ( lastIndex === -1 || cmdSyntaxMap[i][0] === lastIndex ) && cmdSyntaxMap[i][1] === matchCount ).join('\n').replaceSave( regex, '/' + mccmd );
  36. msg.sendChannel( spoiler + '```md\n' + cmdSyntax + '```<' + lang.get('minecraft.link') + lang.get('minecraft.cmdpage') + aliasCmd + '>' + spoiler, {split:{maxLength:2000,prepend:spoiler + '```md\n',append:'```' + spoiler}} );
  37. if ( reaction ) reaction.removeEmoji();
  38. }
  39. else {
  40. msg.reactEmoji('❓');
  41. msg.notMinecraft = true;
  42. this.WIKI.general(lang, msg, title, new Wiki(lang.get('minecraft.link')), cmd, reaction, spoiler, querystring, fragment);
  43. }
  44. }
  45. module.exports = {
  46. name: 'SYNTAX',
  47. run: minecraft_syntax
  48. };