From 4415d196de183ed0f9865deacabedd2c90fd39aa Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Tue, 11 Jun 2024 20:53:30 +0300 Subject: [PATCH 1/4] small change in text --- 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 426b094..78d364f 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -5,7 +5,7 @@ import b4a from "b4a"; class Client extends EventEmitter { constructor(botName) { super(); - if(!botName) return console.error("BotName is not defined!"); + if(!botName) return console.error("Bot Name is not defined!"); this.botName = botName; this.swarm = new Hyperswarm(); this.setupSwarm(); -- 2.39.3 From 4aa205cd731b9dddfa21cb98cfb1d765097930c5 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Tue, 11 Jun 2024 20:56:02 +0300 Subject: [PATCH 2/4] Got rid of 2 useless classes --- chatBot/includes/ChatRoom.js | 8 -------- chatBot/includes/Client.js | 18 ++---------------- chatBot/includes/UserPeer.js | 10 ---------- 3 files changed, 2 insertions(+), 34 deletions(-) delete mode 100644 chatBot/includes/ChatRoom.js delete mode 100644 chatBot/includes/UserPeer.js diff --git a/chatBot/includes/ChatRoom.js b/chatBot/includes/ChatRoom.js deleted file mode 100644 index 0686013..0000000 --- a/chatBot/includes/ChatRoom.js +++ /dev/null @@ -1,8 +0,0 @@ -class ChatRoom { - public ChatRoom(topic, peers) { - this.topic = topic; - this.peers = peers; - } -} - -export default ChatRoom; \ No newline at end of file diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index f249b23..5c82352 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -46,22 +46,8 @@ class Client extends EventEmitter { }); } - sendMessage(roomPeers, message) { - console.log('Bot name:', this.botName); - const timestamp = Date.now(); - const messageObj = { - type: 'message', - name: this.botName, - message, - timestamp, - topic: this.currentTopic // Include the current topic - }; - const data = JSON.stringify(messageObj); - const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); - for (const peer of peers) peer.write(data); - } - - sendMessageToAll(message) { + // TODO: Make topic here actually work. + sendMessage(topic, message) { console.log('Bot name:', this.botName); const timestamp = Date.now(); const messageObj = { diff --git a/chatBot/includes/UserPeer.js b/chatBot/includes/UserPeer.js deleted file mode 100644 index 9546499..0000000 --- a/chatBot/includes/UserPeer.js +++ /dev/null @@ -1,10 +0,0 @@ -class UserPeer { - public UserPeer(peer, topics, username, avatar) { - this.peer = peer; - this.topics = topics; - this.username = username; - this.avatar = avatar; - } -} - -export default UserPeer; \ No newline at end of file -- 2.39.3 From 00b26d6afe37c6353375856fab879beb2dba04d5 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:47:42 +0300 Subject: [PATCH 3/4] Changed lots of stuff inside of bot code. Got rid of peers in message object. --- app.js | 9 +-------- chatBot/bot.js | 2 +- chatBot/commands/8ball.js | 2 +- chatBot/commands/hello.js | 2 +- chatBot/commands/ping.js | 4 ++-- chatBot/includes/Client.js | 34 +++++++++++++++++++-------------- chatBot/includes/FileMessage.js | 20 ++++++++++++++++--- chatBot/includes/TextMessage.js | 22 ++++++++++++++++++--- 8 files changed, 62 insertions(+), 33 deletions(-) diff --git a/app.js b/app.js index fa93d67..30b0d47 100644 --- a/app.js +++ b/app.js @@ -390,20 +390,13 @@ function sendMessage(e) { onMessageAdded(config.userName, message, config.userAvatar, topic); - let peersPublicKeys = []; - peersPublicKeys.push([...swarm.connections].map(peer => peer.remotePublicKey.toString('hex'))); - peersPublicKeys = peersPublicKeys.flat(1); - peersPublicKeys.push(swarm.keyPair.publicKey.toString('hex')); - const messageObj = JSON.stringify({ type: 'message', name: config.userName, message, avatar: config.userAvatar, topic: topic, - peers: peersPublicKeys, // Deprecated. To be deleted in future updates - timestamp: Date.now(), - readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp + timestamp: Date.now() }); const peers = [...swarm.connections]; diff --git a/chatBot/bot.js b/chatBot/bot.js index 2dcb9ba..f6ed08e 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -60,7 +60,7 @@ loadCommands().then(commands => { bot.on('onBotJoinRoom', () => { console.log("Bot is ready!"); - bot.sendMessageToAll(process.env.ON_READY_MESSAGE); + bot.sendTextMessage(process.env.ON_READY_MESSAGE); }); bot.joinChatRoom(process.env.LINKUP_ROOM_ID); diff --git a/chatBot/commands/8ball.js b/chatBot/commands/8ball.js index 0fc287f..7982065 100644 --- a/chatBot/commands/8ball.js +++ b/chatBot/commands/8ball.js @@ -4,6 +4,6 @@ export default { handler: function(bot, args, message) { const responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', 'Don\'t count on it.', 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.']; const randomIndex = Math.floor(Math.random() * responses.length); - bot.sendMessage(message.peers, responses[randomIndex]); + bot.sendTextMessage(responses[randomIndex]); } }; \ No newline at end of file diff --git a/chatBot/commands/hello.js b/chatBot/commands/hello.js index 1c0ee81..cee8139 100644 --- a/chatBot/commands/hello.js +++ b/chatBot/commands/hello.js @@ -3,6 +3,6 @@ export default { description: 'Greet the user', handler: function(bot, args, message) { const userName = message.name; - bot.sendMessage(message.peers, `Hello, ${userName}! How can I assist you today?`); + bot.sendTextMessage(`Hello, ${userName}! How can I assist you today?`); } }; \ No newline at end of file diff --git a/chatBot/commands/ping.js b/chatBot/commands/ping.js index 16f26a4..d30a588 100644 --- a/chatBot/commands/ping.js +++ b/chatBot/commands/ping.js @@ -2,6 +2,6 @@ export default { handler: function(bot, args, message) { - bot.sendMessage(message.peers, 'Pong!'); + bot.sendTextMessage('Pong!'); } - }; \ No newline at end of file +}; \ No newline at end of file diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 5c82352..6c76306 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -1,6 +1,8 @@ import Hyperswarm from 'hyperswarm'; import EventEmitter from 'node:events'; import b4a from "b4a"; +import TextMessage from "./TextMessage.js"; +import FileMessage from "./FileMessage.js"; class Client extends EventEmitter { constructor(botName) { @@ -11,6 +13,10 @@ class Client extends EventEmitter { this.joinedRooms = new Set(); // Track the rooms the bot has joined this.currentTopic = null; // Track the current topic this.setupSwarm(); + + process.on('exit', () => { + this.destroy(); + }); } setupSwarm() { @@ -19,9 +25,14 @@ 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, messageObj); - if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj); - if (messageObj.type === "file") this.emit('onFile', peer, messageObj); + if (messageObj.type === "message") + this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.message, messageObj.timestamp)); + + if (messageObj.type === "file") + this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp)); + + if (messageObj.type === "icon") + this.emit('onIcon', peer, messageObj); } }); @@ -46,18 +57,13 @@ class Client extends EventEmitter { }); } - // TODO: Make topic here actually work. - sendMessage(topic, message) { + sendTextMessage(message) { + this.sendMessage(TextMessage.new(this, message)); + } + + sendMessage(message) { console.log('Bot name:', this.botName); - const timestamp = Date.now(); - const messageObj = { - type: 'message', - name: this.botName, - message, - timestamp, - topic: this.currentTopic // Include the current topic - }; - const data = JSON.stringify(messageObj); + const data = message.toJsonString(); const peers = [...this.swarm.connections]; for (const peer of peers) peer.write(data); } diff --git a/chatBot/includes/FileMessage.js b/chatBot/includes/FileMessage.js index 55d58b4..b4a78f4 100644 --- a/chatBot/includes/FileMessage.js +++ b/chatBot/includes/FileMessage.js @@ -1,12 +1,26 @@ class FileMessage { - public FileMessage(chatRoom, userPeer, fileName, fileUrl, fileType, timestamp) { - this.chatRoom = chatRoom; - this.userPeer = userPeer; + public FileMessage(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) { + this.peerName = peerName; this.fileName = fileName; this.fileUrl = fileUrl; this.fileType = fileType; + this.peerAvatar = peerAvatar; + this.topic = topic; this.timestamp = timestamp; } + + public toJsonString() { + return JSON.stringify({ + type: 'file', + name: this.peerName, + fileName: this.fileName, + fileUrl: this.fileUrl, + fileType: this.fileType, + avatar: this.peerAvatar, + topic: this.topic, + timestamp: this.timestamp, + }); + } } export default FileMessage; \ No newline at end of file diff --git a/chatBot/includes/TextMessage.js b/chatBot/includes/TextMessage.js index f3a8021..b691113 100644 --- a/chatBot/includes/TextMessage.js +++ b/chatBot/includes/TextMessage.js @@ -1,10 +1,26 @@ class TextMessage { - public TextMessage(chatRoom, userPeer, message, timestamp) { - this.chatRoom = chatRoom; - this.userPeer = userPeer; + public TextMessage(peerName, peerAvatar, topic, message, timestamp) { + this.peerName = peerName; + this.peerAvatar = peerAvatar; + this.topic = topic; this.message = message; this.timestamp = timestamp; } + + public toJsonString() { + return JSON.stringify({ + type: 'message', + name: this.peerName, + message: this.message, + avatar: this.peerAvatar, + topic: this.topic, + timestamp: this.timestamp + }); + } + + public static new(bot, message) { + return new TextMessage(bot.botName, "", bot.currentTopic, message, Date.now()); + } } export default TextMessage; \ No newline at end of file -- 2.39.3 From f69b22c810633161451c8e348e4249f2f53f75af Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:49:34 +0300 Subject: [PATCH 4/4] Removed .idea folder --- .gitignore | 1 + .idea/.gitignore | 3 --- .idea/LinkUp-P2P-Chat.iml | 12 ------------ .idea/misc.xml | 6 ------ .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 6 files changed, 1 insertion(+), 35 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/LinkUp-P2P-Chat.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 9179efb..e87fbcc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ storage chatBot/.env chatBot/commands/ai.js config.json +.idea diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/LinkUp-P2P-Chat.iml b/.idea/LinkUp-P2P-Chat.iml deleted file mode 100644 index 24643cc..0000000 --- a/.idea/LinkUp-P2P-Chat.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 3668dc8..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 8ab2f2f..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file -- 2.39.3