separators/command.js

  1. /**
  2. * Seperates a message text into all parts of a Telegram command.
  3. *
  4. * @property {boolean} valid Is the message text a valid Command?
  5. * If false, all other params do not exist.
  6. * @property {string} text Whole message, equal to the constructor's param
  7. * @property {string} command Command, e.g. in '/help test' -> 'help'
  8. * @property {string} bot The mentioned bot, e.g. in '/echo@sk22testbot' ->
  9. * 'sk22testbot'
  10. * @property {string} args Command's args, e.g. in '/help test' -> 'test'
  11. */
  12. class CommandSeparator {
  13. constructor(text) {
  14. const regex = /^\/([^@\s]+)@?(?:(\S+)|)\s?([\s\S]*)$/i;
  15. const command = regex.exec(text);
  16. if (command) {
  17. this.valid = true;
  18. this.text = text;
  19. this.command = command[1];
  20. this.bot = command[2];
  21. this.args = command[3];
  22. } else {
  23. this.valid = false;
  24. }
  25. }
  26. }
  27. module.exports = CommandSeparator;