From b3ccda32ea91d5b3f4dca6c384aa228e19421472 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:43:17 +0300 Subject: [PATCH 01/20] did lots of work on bot package --- app.js | 7 +++++-- chatBot/bot.js | 4 ++-- chatBot/includes/AudioMessage.js | 23 +++++++++++++++++++++++ chatBot/includes/Client.js | 11 +++++++++-- chatBot/includes/FileMessage.js | 23 +++++++++++------------ chatBot/includes/Message.js | 21 +++++++++++++++++++++ chatBot/includes/TextMessage.js | 19 +++++++------------ 7 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 chatBot/includes/AudioMessage.js create mode 100644 chatBot/includes/Message.js diff --git a/app.js b/app.js index b3a2a12..3608b34 100644 --- a/app.js +++ b/app.js @@ -213,6 +213,7 @@ async function handleConnection(connection, info) { type: 'icon', username: config.userName, avatar: b4a.toString(iconBuffer, 'base64'), + timestamp: Date.now() }); console.log('Sending icon to new peer:', iconMessage); connection.write(iconMessage); @@ -292,7 +293,8 @@ function setupTalkButton() { audio: b4a.toString(buffer, 'base64'), audioType: audioBlob.type, avatar: updatePortInUrl(config.userAvatar), - topic: topic + topic: topic, + timestamp: Date.now() }; console.log('Sending audio message:', audioMessage); // Debugging log @@ -594,7 +596,8 @@ async function handleFileInput(event) { file: b4a.toString(buffer, 'base64'), fileType: file.type, avatar: updatePortInUrl(config.userAvatar), - topic: topic + topic: topic, + timestamp: Date.now() }; console.log('Sending file message:', fileMessage); // Debugging log diff --git a/chatBot/bot.js b/chatBot/bot.js index d13f001..e89d03d 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -19,14 +19,14 @@ async function loadCommands() { for (const file of files) { // Check if the file is a JavaScript file if (file.endsWith('.js')) { - const commandName = path.basename(file, '.js'); + // const commandName = path.basename(file, '.js'); const commandPath = path.join(commandsDir, file); // Dynamically import the command module const { default: commandModule } = await import(commandPath); // Add the command module to the commands object - commands[commandName] = commandModule; + commands[commandPath] = commandModule; } } diff --git a/chatBot/includes/AudioMessage.js b/chatBot/includes/AudioMessage.js new file mode 100644 index 0000000..2fcb8a0 --- /dev/null +++ b/chatBot/includes/AudioMessage.js @@ -0,0 +1,23 @@ +import Message from "./Message.js"; + +class AudioMessage extends Message { + constructor(peerName, peerAvatar, topic, timestamp, audio, audioType) { + super("audio", peerName, peerAvatar, topic, timestamp); + this.audio = audio; + this.audioType = audioType; + } + + toJsonString() { + return JSON.stringify({ + ...this.toJson(), + audio: this.audio, + audioType: this.audioType + }); + } + + static new(bot, audio, audioType) { + return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audio, audioType); + } +} + +export default AudioMessage; diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index dbc5c08..3e222c0 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -3,12 +3,14 @@ import EventEmitter from 'node:events'; import b4a from "b4a"; import TextMessage from "./TextMessage.js"; import FileMessage from "./FileMessage.js"; +import AudioMessage from "./AudioMessage.js"; class Client extends EventEmitter { constructor(botName) { super(); if (!botName) return console.error("Bot Name is not defined!"); this.botName = botName; + this.botAvatar = ""; this.swarm = new Hyperswarm(); this.joinedRooms = new Set(); // Track the rooms the bot has joined this.currentTopic = null; // Track the current topic @@ -41,13 +43,16 @@ class Client extends EventEmitter { if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room this.currentTopic = messageObj.topic; // Set the current topic from the incoming message if (messageObj.type === "message") - this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.message, messageObj.timestamp)); + this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.message)); if (messageObj.type === "file") - this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp)); + this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.avatar, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp)); if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj); + + if (messageObj.type === "audio") + this.emit('onAudio', peer, new AudioMessage()); } }); @@ -81,6 +86,8 @@ class Client extends EventEmitter { } sendMessage(message) { + console.log(typeof message); + console.log('Bot name:', this.botName); const data = message.toJsonString(); const peers = [...this.swarm.connections]; diff --git a/chatBot/includes/FileMessage.js b/chatBot/includes/FileMessage.js index aa055fe..d62ff7b 100644 --- a/chatBot/includes/FileMessage.js +++ b/chatBot/includes/FileMessage.js @@ -1,26 +1,25 @@ -class FileMessage { - constructor(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) { - this.peerName = peerName; +import Message from "./Message.js"; + +class FileMessage extends Message { + constructor(peerName, peerAvatar, topic, timestamp, fileName, fileUrl, fileType) { + super("file", peerName, peerAvatar, topic, timestamp); this.fileName = fileName; this.fileUrl = fileUrl; this.fileType = fileType; - this.peerAvatar = peerAvatar; - this.topic = topic; - this.timestamp = timestamp; } toJsonString() { return JSON.stringify({ - type: 'file', - name: this.peerName, + ...this.toJson(), fileName: this.fileName, fileUrl: this.fileUrl, - fileType: this.fileType, - avatar: this.peerAvatar, - topic: this.topic, - timestamp: this.timestamp, + fileType: this.fileType }); } + + static new(bot, fileName, fileUrl, fileType) { + return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType); + } } export default FileMessage; diff --git a/chatBot/includes/Message.js b/chatBot/includes/Message.js new file mode 100644 index 0000000..9f6f4e4 --- /dev/null +++ b/chatBot/includes/Message.js @@ -0,0 +1,21 @@ +class Message { + constructor(messageType, peerName, peerAvatar, topic, timestamp) { + this.messageType = messageType; + this.peerName = peerName; + this.peerAvatar = peerAvatar; + this.topic = topic; + this.timestamp = timestamp; + } + + toJson() { + return { + type: this.messageType, + name: this.peerName, + avatar: this.peerAvatar, + topic: this.topic, + timestamp: this.timestamp + }; + } +} + +export default Message; diff --git a/chatBot/includes/TextMessage.js b/chatBot/includes/TextMessage.js index fa07783..716cde1 100644 --- a/chatBot/includes/TextMessage.js +++ b/chatBot/includes/TextMessage.js @@ -1,25 +1,20 @@ -class TextMessage { - constructor(peerName, peerAvatar, topic, message, timestamp) { - this.peerName = peerName; - this.peerAvatar = peerAvatar; - this.topic = topic; +import Message from "./Message.js"; + +class TextMessage extends Message { + constructor(peerName, peerAvatar, topic, timestamp, message) { + super("message", peerName, peerAvatar, topic, timestamp); this.message = message; - this.timestamp = timestamp; } toJsonString() { return JSON.stringify({ - type: 'message', - name: this.peerName, + ...this.toJson(), message: this.message, - avatar: this.peerAvatar, - topic: this.topic, - timestamp: this.timestamp }); } static new(bot, message) { - return new TextMessage(bot.botName, "", bot.currentTopic, message, Date.now()); + return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message); } } From feff30fecc2886b3f48803b5f229b26788d15d7d Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:00:35 +0300 Subject: [PATCH 02/20] Testing if sendMessage argument extends Message class --- chatBot/includes/Client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 3e222c0..12b7436 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -4,6 +4,7 @@ import b4a from "b4a"; import TextMessage from "./TextMessage.js"; import FileMessage from "./FileMessage.js"; import AudioMessage from "./AudioMessage.js"; +import Message from "./Message.js"; class Client extends EventEmitter { constructor(botName) { @@ -86,7 +87,7 @@ class Client extends EventEmitter { } sendMessage(message) { - console.log(typeof message); + console.log(message.prototype instanceof Message); console.log('Bot name:', this.botName); const data = message.toJsonString(); From 457bf01bc3ebe0a6c6d5ab946b0d7aa2ec7c7715 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:02:26 +0300 Subject: [PATCH 03/20] Trying out isPrototypeOf() function --- chatBot/includes/Client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 12b7436..e1133e4 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -87,7 +87,8 @@ class Client extends EventEmitter { } sendMessage(message) { - console.log(message.prototype instanceof Message); + console.log(message); + console.log(Message.isPrototypeOf(message)); console.log('Bot name:', this.botName); const data = message.toJsonString(); From 8262bd4eb7954ccd0211e3ecc908c2c84b334164 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:04:08 +0300 Subject: [PATCH 04/20] Hm. Its pretty strange. Thats why i dont really like JavaScript --- chatBot/includes/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index e1133e4..f223271 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -88,7 +88,7 @@ class Client extends EventEmitter { sendMessage(message) { console.log(message); - console.log(Message.isPrototypeOf(message)); + console.log(Object.prototype.isPrototypeOf.call(Message.prototype, message.prototype)); console.log('Bot name:', this.botName); const data = message.toJsonString(); From d50e0df6a115dbba9ee55abf33df79b749ae5a9d Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:05:00 +0300 Subject: [PATCH 05/20] Time to do lots of debugging :sob: --- chatBot/includes/Client.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index f223271..248d0d5 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -88,6 +88,8 @@ class Client extends EventEmitter { sendMessage(message) { console.log(message); + console.log(message.prototype); + console.log(Message.prototype); console.log(Object.prototype.isPrototypeOf.call(Message.prototype, message.prototype)); console.log('Bot name:', this.botName); From 2ff9c73585eea09ab77e4cd38b5b8c05f7b338c3 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:07:21 +0300 Subject: [PATCH 06/20] It doesnt want to work :sob: --- chatBot/includes/Client.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 248d0d5..0110f22 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -87,10 +87,8 @@ class Client extends EventEmitter { } sendMessage(message) { - console.log(message); - console.log(message.prototype); - console.log(Message.prototype); - console.log(Object.prototype.isPrototypeOf.call(Message.prototype, message.prototype)); + console.log(message instanceof Message); + console.log(message instanceof TextMessage); console.log('Bot name:', this.botName); const data = message.toJsonString(); From d5650a9d7bb6b1f46bd60ec8ef2dbfaf6150ea21 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:09:30 +0300 Subject: [PATCH 07/20] I think i did it yay --- chatBot/includes/Client.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 0110f22..eec549e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -87,8 +87,7 @@ class Client extends EventEmitter { } sendMessage(message) { - console.log(message instanceof Message); - console.log(message instanceof TextMessage); + if(!(message instanceof Message)) return console.log(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message); console.log('Bot name:', this.botName); const data = message.toJsonString(); From 75be3a5b33912e0ba1c96564ce4db2430af581cc Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:20:37 +0300 Subject: [PATCH 08/20] Reworked commands, added some funny stuff --- chatBot/includes/Client.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index eec549e..d5f6866 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -43,17 +43,18 @@ class Client extends EventEmitter { const messageObj = JSON.parse(message.toString()); if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room this.currentTopic = messageObj.topic; // Set the current topic from the incoming message + if (messageObj.type === "message") this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.message)); if (messageObj.type === "file") - this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.avatar, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp)); + this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj); if (messageObj.type === "audio") - this.emit('onAudio', peer, new AudioMessage()); + this.emit('onAudio', peer, new AudioMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.audio, messageObj.audioType)); } }); From 70655ce90172b07c26f24bcd82a6d92252fc76b2 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:34:06 +0300 Subject: [PATCH 09/20] So messages actually broke somehow lol --- chatBot/bot.js | 2 +- chatBot/includes/Client.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index e89d03d..cb9162d 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -39,7 +39,7 @@ loadCommands().then(commands => { // We use Event Emitter here to handle new messages bot.on('onMessage', (peer, message) => { - console.log(message); + // console.log(message); console.log(`Message received from ${message.peerName}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index d5f6866..1ec2200 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -90,7 +90,8 @@ class Client extends EventEmitter { sendMessage(message) { if(!(message instanceof Message)) return console.log(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message); - console.log('Bot name:', this.botName); + // console.log('Bot name:', this.botName); + console.log("Sending message:", message); const data = message.toJsonString(); const peers = [...this.swarm.connections]; for (const peer of peers) peer.write(data); From 52a319a1c06964ac892482c653cbcd30dc3b4c75 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:36:16 +0300 Subject: [PATCH 10/20] Fixed my bots messages --- chatBot/bot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index cb9162d..663c46c 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -19,14 +19,14 @@ async function loadCommands() { for (const file of files) { // Check if the file is a JavaScript file if (file.endsWith('.js')) { - // const commandName = path.basename(file, '.js'); + const commandName = path.basename(file, '.js'); const commandPath = path.join(commandsDir, file); // Dynamically import the command module const { default: commandModule } = await import(commandPath); // Add the command module to the commands object - commands[commandPath] = commandModule; + commands[commandName] = commandModule; } } From bb26b5c2a82e7c55ef1658c24da0a67a1fa56b1b Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:37:58 +0300 Subject: [PATCH 11/20] Revert all changes i did to app.js --- app.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 3608b34..af1e06c 100644 --- a/app.js +++ b/app.js @@ -212,8 +212,7 @@ async function handleConnection(connection, info) { const iconMessage = JSON.stringify({ type: 'icon', username: config.userName, - avatar: b4a.toString(iconBuffer, 'base64'), - timestamp: Date.now() + avatar: b4a.toString(iconBuffer, 'base64') }); console.log('Sending icon to new peer:', iconMessage); connection.write(iconMessage); @@ -293,8 +292,7 @@ function setupTalkButton() { audio: b4a.toString(buffer, 'base64'), audioType: audioBlob.type, avatar: updatePortInUrl(config.userAvatar), - topic: topic, - timestamp: Date.now() + topic: topic }; console.log('Sending audio message:', audioMessage); // Debugging log @@ -596,8 +594,7 @@ async function handleFileInput(event) { file: b4a.toString(buffer, 'base64'), fileType: file.type, avatar: updatePortInUrl(config.userAvatar), - topic: topic, - timestamp: Date.now() + topic: topic }; console.log('Sending file message:', fileMessage); // Debugging log From d449249f4115251b44bb22a9f4de328fa63c7a86 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:39:16 +0300 Subject: [PATCH 12/20] Revert reverting --- app.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index af1e06c..3608b34 100644 --- a/app.js +++ b/app.js @@ -212,7 +212,8 @@ async function handleConnection(connection, info) { const iconMessage = JSON.stringify({ type: 'icon', username: config.userName, - avatar: b4a.toString(iconBuffer, 'base64') + avatar: b4a.toString(iconBuffer, 'base64'), + timestamp: Date.now() }); console.log('Sending icon to new peer:', iconMessage); connection.write(iconMessage); @@ -292,7 +293,8 @@ function setupTalkButton() { audio: b4a.toString(buffer, 'base64'), audioType: audioBlob.type, avatar: updatePortInUrl(config.userAvatar), - topic: topic + topic: topic, + timestamp: Date.now() }; console.log('Sending audio message:', audioMessage); // Debugging log @@ -594,7 +596,8 @@ async function handleFileInput(event) { file: b4a.toString(buffer, 'base64'), fileType: file.type, avatar: updatePortInUrl(config.userAvatar), - topic: topic + topic: topic, + timestamp: Date.now() }; console.log('Sending file message:', fileMessage); // Debugging log From 14aa129f6ddbc2382babf43c90cc0e2fe88fc3d7 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:44:05 +0300 Subject: [PATCH 13/20] Trying some changes to make bot messages and client mmessages have same format --- app.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 3608b34..b5800d4 100644 --- a/app.js +++ b/app.js @@ -290,11 +290,11 @@ function setupTalkButton() { const audioMessage = { type: 'audio', name: config.userName, - audio: b4a.toString(buffer, 'base64'), - audioType: audioBlob.type, avatar: updatePortInUrl(config.userAvatar), topic: topic, - timestamp: Date.now() + timestamp: Date.now(), + audio: b4a.toString(buffer, 'base64'), + audioType: audioBlob.type }; console.log('Sending audio message:', audioMessage); // Debugging log @@ -565,10 +565,10 @@ async function sendMessage(e) { const messageObj = JSON.stringify({ type: 'message', name: config.userName, - message, avatar: config.userAvatar, topic: topic, - timestamp: timestamp + timestamp: timestamp, + message }); const peers = [...activeRooms.find(room => room.topic === topic).swarm.connections]; @@ -592,12 +592,12 @@ async function handleFileInput(event) { const fileMessage = { type: 'file', name: config.userName, - fileName: file.name, - file: b4a.toString(buffer, 'base64'), - fileType: file.type, avatar: updatePortInUrl(config.userAvatar), topic: topic, - timestamp: Date.now() + timestamp: Date.now(), + fileName: file.name, + file: b4a.toString(buffer, 'base64'), + fileType: file.type }; console.log('Sending file message:', fileMessage); // Debugging log From 9b4357dd00164f0a671d1270deb793f7c2ac838b Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:25:17 +0300 Subject: [PATCH 14/20] i hate js. it ignores all of my code and just sends 'messagetype' instead of 'type' --- chatBot/bot.js | 4 ++-- chatBot/includes/Message.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index 663c46c..9757135 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -39,9 +39,9 @@ loadCommands().then(commands => { // We use Event Emitter here to handle new messages bot.on('onMessage', (peer, message) => { - // console.log(message); - console.log(`Message received from ${message.peerName}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); + console.log(`Message received from ${message.peerName} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); + console.log(message); // Check if the message starts with a command prefix if (message.message.startsWith('!')) { diff --git a/chatBot/includes/Message.js b/chatBot/includes/Message.js index 9f6f4e4..fbb2fff 100644 --- a/chatBot/includes/Message.js +++ b/chatBot/includes/Message.js @@ -1,6 +1,6 @@ class Message { constructor(messageType, peerName, peerAvatar, topic, timestamp) { - this.messageType = messageType; + this.type = messageType; this.peerName = peerName; this.peerAvatar = peerAvatar; this.topic = topic; @@ -9,7 +9,7 @@ class Message { toJson() { return { - type: this.messageType, + type: this.type, name: this.peerName, avatar: this.peerAvatar, topic: this.topic, From 6cbfe34966c54a88192dc4b839c5631e49ca4fae Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:49:26 +0300 Subject: [PATCH 15/20] Testing bot avatars thingy --- chatBot/bot.js | 3 ++- chatBot/includes/Client.js | 7 +++++++ chatBot/includes/IconMessage.js | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 chatBot/includes/IconMessage.js diff --git a/chatBot/bot.js b/chatBot/bot.js index 9757135..88cc5e9 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -39,7 +39,6 @@ loadCommands().then(commands => { // We use Event Emitter here to handle new messages bot.on('onMessage', (peer, message) => { - console.log(`Message received from ${message.peerName} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); console.log(message); @@ -64,6 +63,8 @@ loadCommands().then(commands => { }); bot.joinChatRoom(process.env.LINKUP_ROOM_ID); + + bot.fetchAvatar(`https://avatar.iran.liara.run/username?username=${this.botName}&background=f4d9b2&color=FF9800&size=40`); // Debugging avatar }).catch(error => { console.error('Error loading commands:', error); }); \ No newline at end of file diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 1ec2200..b40677e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -5,6 +5,7 @@ import TextMessage from "./TextMessage.js"; import FileMessage from "./FileMessage.js"; import AudioMessage from "./AudioMessage.js"; import Message from "./Message.js"; +import IconMessage from "./IconMessage.js"; class Client extends EventEmitter { constructor(botName) { @@ -37,6 +38,12 @@ class Client extends EventEmitter { }); } + async fetchAvatar(url) { + this.botAvatar = url; + const img = await fetch(url); + this.sendMessage(IconMessage.new(this, img)); + } + setupSwarm() { this.swarm.on('connection', (peer) => { peer.on('data', message => { diff --git a/chatBot/includes/IconMessage.js b/chatBot/includes/IconMessage.js new file mode 100644 index 0000000..6ae10b4 --- /dev/null +++ b/chatBot/includes/IconMessage.js @@ -0,0 +1,19 @@ +import Message from "./Message.js"; + +class IconMessage extends Message { + async constructor(peerName, avatarBuffer, topic, timestamp) { + super("icon", peerName, avatarBuffer, topic, timestamp); + } + + toJsonString() { + return JSON.stringify({ + ...this.toJson() + }); + } + + static new(bot, avatarBuffer) { + return new IconMessage(bot.botName, avatarBuffer, bot.currentTopic, Date.now()); + } +} + +export default IconMessage; From 2d0f97bc8f93eda28a1684998363379f79e3f8e6 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:50:10 +0300 Subject: [PATCH 16/20] Oops i made async constructor --- chatBot/includes/IconMessage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatBot/includes/IconMessage.js b/chatBot/includes/IconMessage.js index 6ae10b4..a0c5cc3 100644 --- a/chatBot/includes/IconMessage.js +++ b/chatBot/includes/IconMessage.js @@ -1,7 +1,7 @@ import Message from "./Message.js"; class IconMessage extends Message { - async constructor(peerName, avatarBuffer, topic, timestamp) { + constructor(peerName, avatarBuffer, topic, timestamp) { super("icon", peerName, avatarBuffer, topic, timestamp); } From 3e9a2101d503ecee43b8d820c7eae7fa5943799a Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:51:34 +0300 Subject: [PATCH 17/20] I did this.botName and not bot.botName lol --- chatBot/bot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index 88cc5e9..d5eabd7 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -64,7 +64,7 @@ loadCommands().then(commands => { bot.joinChatRoom(process.env.LINKUP_ROOM_ID); - bot.fetchAvatar(`https://avatar.iran.liara.run/username?username=${this.botName}&background=f4d9b2&color=FF9800&size=40`); // Debugging avatar + bot.fetchAvatar(`https://avatar.iran.liara.run/username?username=${bot.botName}&background=f4d9b2&color=FF9800&size=40`); // Debugging avatar }).catch(error => { console.error('Error loading commands:', error); }); \ No newline at end of file From d4dc1b299d05e7cf9442bf70386d6054d0d1fa80 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:53:51 +0300 Subject: [PATCH 18/20] Ignoring 'updatePortInUrl' if port is 443 (SSL connection) --- app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app.js b/app.js index b5800d4..208c3a0 100644 --- a/app.js +++ b/app.js @@ -850,6 +850,7 @@ function writeConfigToFile(filePath) { function updatePortInUrl(url) { if (!url) return url; const urlObject = new URL(url); + if(urlObject.port == 443) return urlObject.toString(); urlObject.port = servePort; return urlObject.toString(); } From 42dccf95477c00e92eb50e67a4a81e7764b8abe8 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:58:40 +0300 Subject: [PATCH 19/20] Actually just returning the url if its host is not 'localhost' --- app.js | 2 +- chatBot/includes/Client.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 208c3a0..74a6207 100644 --- a/app.js +++ b/app.js @@ -850,7 +850,7 @@ function writeConfigToFile(filePath) { function updatePortInUrl(url) { if (!url) return url; const urlObject = new URL(url); - if(urlObject.port == 443) return urlObject.toString(); + if(!urlObject.host.startsWith("localhost")) return urlObject.toString(); urlObject.port = servePort; return urlObject.toString(); } diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index b40677e..d44982e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -40,8 +40,9 @@ class Client extends EventEmitter { async fetchAvatar(url) { this.botAvatar = url; - const img = await fetch(url); - this.sendMessage(IconMessage.new(this, img)); + const web = await fetch(url); + const img = await web.body.getReader().read(); + this.sendMessage(IconMessage.new(this, img.value)); } setupSwarm() { From f706e7d621534ad12a81644fdba7754ba0d5e729 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 20:06:05 +0300 Subject: [PATCH 20/20] Made Client.js->setupSwarm() parsing messages more readable --- chatBot/includes/Client.js | 31 +++++++++++-------- .../includes/{ => message}/AudioMessage.js | 0 chatBot/includes/{ => message}/FileMessage.js | 0 chatBot/includes/{ => message}/IconMessage.js | 7 +++-- chatBot/includes/{ => message}/Message.js | 0 chatBot/includes/{ => message}/TextMessage.js | 0 6 files changed, 22 insertions(+), 16 deletions(-) rename chatBot/includes/{ => message}/AudioMessage.js (100%) rename chatBot/includes/{ => message}/FileMessage.js (100%) rename chatBot/includes/{ => message}/IconMessage.js (51%) rename chatBot/includes/{ => message}/Message.js (100%) rename chatBot/includes/{ => message}/TextMessage.js (100%) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index d44982e..c1da18e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -1,11 +1,11 @@ import Hyperswarm from 'hyperswarm'; import EventEmitter from 'node:events'; import b4a from "b4a"; -import TextMessage from "./TextMessage.js"; -import FileMessage from "./FileMessage.js"; -import AudioMessage from "./AudioMessage.js"; -import Message from "./Message.js"; -import IconMessage from "./IconMessage.js"; +import TextMessage from "./message/TextMessage.js"; +import FileMessage from "./message/FileMessage.js"; +import AudioMessage from "./message/AudioMessage.js"; +import Message from "./message/Message.js"; +import IconMessage from "./message/IconMessage.js"; class Client extends EventEmitter { constructor(botName) { @@ -52,17 +52,22 @@ class Client extends EventEmitter { if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room this.currentTopic = messageObj.topic; // Set the current topic from the incoming message - if (messageObj.type === "message") - this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.message)); + const msgType = messageObj.type; + const peerName = messageObj.name; + const peerAvatar = messageObj.avatar; + const timestamp = messageObj.timestamp; - if (messageObj.type === "file") - this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); + if (msgType === "message") + this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message)); - if (messageObj.type === "icon") - this.emit('onIcon', peer, messageObj); + if (msgType === "file") + this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); - if (messageObj.type === "audio") - this.emit('onAudio', peer, new AudioMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.audio, messageObj.audioType)); + if (msgType === "icon") + this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp)); + + if (msgType === "audio") + this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.audio, messageObj.audioType)); } }); diff --git a/chatBot/includes/AudioMessage.js b/chatBot/includes/message/AudioMessage.js similarity index 100% rename from chatBot/includes/AudioMessage.js rename to chatBot/includes/message/AudioMessage.js diff --git a/chatBot/includes/FileMessage.js b/chatBot/includes/message/FileMessage.js similarity index 100% rename from chatBot/includes/FileMessage.js rename to chatBot/includes/message/FileMessage.js diff --git a/chatBot/includes/IconMessage.js b/chatBot/includes/message/IconMessage.js similarity index 51% rename from chatBot/includes/IconMessage.js rename to chatBot/includes/message/IconMessage.js index a0c5cc3..bb4afc4 100644 --- a/chatBot/includes/IconMessage.js +++ b/chatBot/includes/message/IconMessage.js @@ -1,8 +1,9 @@ import Message from "./Message.js"; +import b4a from "b4a"; class IconMessage extends Message { - constructor(peerName, avatarBuffer, topic, timestamp) { - super("icon", peerName, avatarBuffer, topic, timestamp); + constructor(peerName, peerAvatar, topic, timestamp) { + super("icon", peerName, peerAvatar, topic, timestamp); } toJsonString() { @@ -12,7 +13,7 @@ class IconMessage extends Message { } static new(bot, avatarBuffer) { - return new IconMessage(bot.botName, avatarBuffer, bot.currentTopic, Date.now()); + return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now()); } } diff --git a/chatBot/includes/Message.js b/chatBot/includes/message/Message.js similarity index 100% rename from chatBot/includes/Message.js rename to chatBot/includes/message/Message.js diff --git a/chatBot/includes/TextMessage.js b/chatBot/includes/message/TextMessage.js similarity index 100% rename from chatBot/includes/TextMessage.js rename to chatBot/includes/message/TextMessage.js