syntax.js 3.8 KB

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