All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.5 KiB

1 year ago
  1. import ChatCommand from "./ChatCommand.mjs";
  2. export default class ChatCommands {
  3. constructor() {
  4. this.registeredCommands = [];
  5. }
  6. /**
  7. * Registers a Chat Command to be handled
  8. * @param command @typedef {Object} ChatCommand
  9. */
  10. registerCommand(command) {
  11. this.registeredCommands.push(command);
  12. }
  13. /**
  14. * Deregister a Chat Command
  15. * @param command @typedef {Object} ChatCommand
  16. */
  17. deregisterCommand(command) {
  18. ChatCommands._removeFromArray(this.registeredCommands, command);
  19. }
  20. static _removeFromArray(array, element) {
  21. const index = array.indexOf(element);
  22. if (index > -1) {
  23. array.splice(index, 1);
  24. }
  25. }
  26. /**
  27. * @deprecated in favor of createCommandFromData(data)
  28. */
  29. createCommand(commandKey, shouldDisplayToChat, invokeOnCommand, createdMessageType = 0) {
  30. return new ChatCommand(commandKey, shouldDisplayToChat, invokeOnCommand, createdMessageType, "fa-terminal", "No description provided");
  31. }
  32. createCommandFromData(data) {
  33. return new ChatCommand(
  34. data.commandKey,
  35. this._getOrDefault(data.shouldDisplayToChat, false),
  36. data.invokeOnCommand,
  37. this._getOrDefault(data.createdMessageType, 0),
  38. this._getOrDefault(data.iconClass, "fa-terminal"),
  39. this._getOrDefault(data.description, "No description provided"),
  40. this._getOrDefault(data.gmOnly, false)
  41. );
  42. }
  43. _getOrDefault(value, defaultValue) {
  44. if (value != undefined) {
  45. return value;
  46. }
  47. return defaultValue
  48. }
  49. handleChatMessage(chatlog, messageText, chatData) {
  50. var matchString = messageText.toLowerCase();
  51. let matchedCommands = [];
  52. for (var x = 0; x < this.registeredCommands.length; x++) {
  53. let registeredCommand = this.registeredCommands[x];
  54. var commandKey = registeredCommand.commandKey.toLowerCase();
  55. if (commandKey != "" && matchString.startsWith(commandKey)) {
  56. if (registeredCommand.gmOnly && !game.user.isGM) continue;
  57. matchedCommands.push(registeredCommand);
  58. }
  59. }
  60. let shouldCancel = false;
  61. let shouldShowToChat = false;
  62. for (let x=0; x < matchedCommands.length; x++) {
  63. let command = matchedCommands[x];
  64. messageText = ChatCommands._removeCommand(messageText, command.commandKey);
  65. chatData.type = command.createdMessageType;
  66. shouldCancel = true;
  67. if (command.shouldDisplayToChat) {
  68. shouldShowToChat = true;
  69. }
  70. if (command.invokeOnCommand != undefined) {
  71. let modifiedText = command.invokeOnCommand(chatlog, messageText, chatData);
  72. if (modifiedText != undefined) {
  73. messageText = modifiedText;
  74. }
  75. }
  76. }
  77. if (shouldShowToChat) {
  78. chatData.content = messageText;
  79. ChatMessage.create(chatData);
  80. }
  81. return !shouldCancel;
  82. }
  83. static _caseInsensitiveReplace(line, word, replaceWith) {
  84. var regex = new RegExp('(' + word + ')', 'gi');
  85. return line.replace(regex, replaceWith);
  86. }
  87. static _removeCommand(messageText, command) {
  88. messageText = ChatCommands._caseInsensitiveReplace(messageText, command, "");
  89. return messageText.trim();
  90. }
  91. }