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] 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); } }