34 lines
961 B
JavaScript
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|