您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Commands.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import UIUtil from '../../util/UIUtil';
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. /**
  4. * List with supported commands. The keys are the names of the commands and
  5. * the value is the function that processes the message.
  6. * @type {{String: function}}
  7. */
  8. const commands = {
  9. "topic" : processTopic
  10. };
  11. /**
  12. * Extracts the command from the message.
  13. * @param message the received message
  14. * @returns {string} the command
  15. */
  16. function getCommand(message) {
  17. if(message) {
  18. for(var command in commands) {
  19. if(message.indexOf("/" + command) === 0)
  20. return command;
  21. }
  22. }
  23. return "";
  24. }
  25. /**
  26. * Processes the data for topic command.
  27. * @param commandArguments the arguments of the topic command.
  28. */
  29. function processTopic(commandArguments, emitter) {
  30. var topic = UIUtil.escapeHtml(commandArguments);
  31. emitter.emit(UIEvents.SUBJECT_CHANGED, topic);
  32. }
  33. /**
  34. * Constructs a new CommandProccessor instance from a message that
  35. * handles commands received via chat messages.
  36. * @param message the message
  37. * @constructor
  38. */
  39. function CommandsProcessor(message, emitter) {
  40. var command = getCommand(message);
  41. this.emitter = emitter;
  42. /**
  43. * Returns the name of the command.
  44. * @returns {String} the command
  45. */
  46. this.getCommand = function() {
  47. return command;
  48. };
  49. var messageArgument = message.substr(command.length + 2);
  50. /**
  51. * Returns the arguments of the command.
  52. * @returns {string}
  53. */
  54. this.getArgument = function() {
  55. return messageArgument;
  56. };
  57. }
  58. /**
  59. * Checks whether this instance is valid command or not.
  60. * @returns {boolean}
  61. */
  62. CommandsProcessor.prototype.isCommand = function() {
  63. if (this.getCommand())
  64. return true;
  65. return false;
  66. };
  67. /**
  68. * Processes the command.
  69. */
  70. CommandsProcessor.prototype.processCommand = function() {
  71. if(!this.isCommand())
  72. return;
  73. commands[this.getCommand()](this.getArgument(), this.emitter);
  74. };
  75. export default CommandsProcessor;