1
0

syntax.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const commands = require('./commands.json');
  2. /**
  3. * Sends a Minecraft command.
  4. * @param {import('../../util/i18n.js')} lang - The user language.
  5. * @param {import('discord.js').Message} msg - The Discord message.
  6. * @param {String} mccmd - The Minecraft command argument.
  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 {String} 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_syntax(lang, msg, mccmd, args, title, cmd, querystring, fragment, reaction, spoiler) {
  16. mccmd = mccmd.toLowerCase();
  17. var aliasCmd = ( commands.aliases[mccmd] || mccmd );
  18. if ( aliasCmd in commands.list ) {
  19. var cmdSyntaxMap = commands.list[aliasCmd].map( command => {
  20. var cmdargs = command.split(' ');
  21. if ( cmdargs[0].startsWith( '/' ) ) cmdargs = cmdargs.slice(1);
  22. var argmatches = cmdargs.map( (arg, i) => {
  23. if ( arg === args[i] ) return true;
  24. } );
  25. var matchCount = 0;
  26. argmatches.forEach( match => {
  27. if ( match ) matchCount++;
  28. } );
  29. return [argmatches.lastIndexOf(true),matchCount];
  30. } );
  31. var lastIndex = Math.max(...cmdSyntaxMap.map( command => command[0] ));
  32. var matchCount = Math.max(...cmdSyntaxMap.filter( command => command[0] === lastIndex ).map( command => command[1] ));
  33. var regex = new RegExp('/' + aliasCmd, 'g');
  34. var cmdSyntax = commands.list[aliasCmd].filter( (command, i) => ( lastIndex === -1 || cmdSyntaxMap[i][0] === lastIndex ) && cmdSyntaxMap[i][1] === matchCount ).join('\n').replaceSave( regex, '/' + mccmd );
  35. 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}} );
  36. if ( reaction ) reaction.removeEmoji();
  37. }
  38. else {
  39. msg.reactEmoji('❓');
  40. msg.notMinecraft = true;
  41. this.WIKI.gamepedia(lang, msg, title, lang.get('minecraft.link'), cmd, reaction, spoiler, querystring, fragment);
  42. }
  43. }
  44. module.exports = {
  45. name: 'SYNTAX',
  46. run: minecraft_syntax
  47. };