This repository has been archived on 2025-03-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
wetsock/lib/WetSock/Message.js
2024-04-07 20:07:50 +01:00

34 lines
961 B
JavaScript

/**
* Represents a chat message.
*/
export default class ChatMessage {
#origin;
#data;
#message;
/**
* Creates an instance of ChatMessage.
* @param {string} rawText - The raw text of the chat message.
*/
constructor(rawText) {
this.#origin = rawText;
try {
this.#data = JSON.parse(rawText.trim());
this.#message = this.#data.message;
} catch(err) {}
}
/**
* Emits the properties of the chat message through the provided WebSocket.
* @param {WebSocket} wetsock - The WebSocket instance to emit the properties through.
*/
emitProps(wetsock) {
if (!this.#data || typeof this.#data !== 'object') return;
for (const key in this.#data) {
if (typeof this.#data[key] === 'string' && this.#data[key].indexOf(' ') === -1) {
wetsock.emit(`message:${key}:${this.#data[key]}`, this);
}
}
}
}