syntax.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. */
  15. function minecraft_syntax(lang, msg, wiki, mccmd, args, title, cmd, reaction, spoiler) {
  16. mccmd = mccmd.toLowerCase();
  17. var aliasCmd = ( commands.aliases[mccmd] || mccmd );
  18. var cmdpage = commands.wikis[wiki.href];
  19. if ( commands.list.hasOwnProperty(aliasCmd) ) {
  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. 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 => {
  37. var body = response.body;
  38. if ( body && body.warnings ) log_warn(body.warnings);
  39. if ( response.statusCode !== 200 || !( body?.query?.pages || body?.parse?.sections?.length ) ) {
  40. console.log( '- ' + response.statusCode + ': Error while getting the command page: ' + ( body && body.error && body.error.info ) );
  41. }
  42. else if ( cmdpage.endsWith( '/' ) ) {
  43. if ( body.query.pages['-1'] ) {
  44. wiki = new Wiki('https://minecraft.fandom.com/');
  45. cmdpage = 'Commands/';
  46. }
  47. else {
  48. cmdpage = Object.values(body.query.pages)[0].title;
  49. aliasCmd = ( body.query.redirects?.[0]?.tofragment || '' );
  50. }
  51. }
  52. else {
  53. cmdpage = body.parse.title;
  54. if ( !body.parse.sections.some( section => section.anchor === aliasCmd ) ) {
  55. if ( body.parse.sections.some( section => section.anchor === mccmd ) ) {
  56. aliasCmd = mccmd;
  57. }
  58. else {
  59. wiki = new Wiki('https://minecraft.fandom.com/');
  60. cmdpage = 'Commands/';
  61. }
  62. }
  63. }
  64. }, error => {
  65. console.log( '- Error while getting the command page: ' + error );
  66. } ).finally( () => {
  67. 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}} );
  68. if ( reaction ) reaction.removeEmoji();
  69. } );
  70. }
  71. else {
  72. msg.notMinecraft = true;
  73. this.WIKI.general(lang, msg, title, wiki, cmd, reaction, spoiler);
  74. }
  75. }
  76. module.exports = {
  77. name: 'SYNTAX',
  78. run: minecraft_syntax
  79. };