From 824e2dab4280fa5fa655be5866343107ee5cf41c Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 10:51:15 +0300 Subject: [PATCH] Started working on making the bot be in multiple chatrooms --- chatBot/bot.js | 5 +++-- chatBot/includes/client.js | 11 ++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index 4a3e775..0fef442 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -35,7 +35,7 @@ async function loadCommands() { loadCommands().then(commands => { // Create the chatBot instance with the defined onMessageReceived function - const bot = new Client(process.env.LINKUP_ROOM_ID, botName); + const bot = new Client(botName); // We use Event Emitter here to handle new messages bot.on('onMessage', (peer, message) => { @@ -64,7 +64,8 @@ loadCommands().then(commands => { bot.sendMessage(process.env.ON_READY_MESSAGE); }); - bot.joinChatRoom(); + bot.joinChatRoom(1234); + bot.joinChatRoom(5678); }).catch(error => { console.error('Error loading commands:', error); }); diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 5033659..6df8ff4 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -2,12 +2,9 @@ import Hyperswarm from 'hyperswarm'; import EventEmitter from 'node:events' class Client extends EventEmitter { - constructor(chatRoomID, botName) { + constructor(botName) { super(); - - if(!chatRoomID || !botName) return console.error("ChatRoomID or BotName is not defined!"); - - this.topicBuffer = Buffer.from(chatRoomID, 'hex'); + if(!botName) return console.error("BotName is not defined!"); this.botName = botName; this.swarm = new Hyperswarm(); this.setupSwarm(); @@ -27,8 +24,8 @@ class Client extends EventEmitter { }); } - joinChatRoom() { - this.discovery = this.swarm.join(this.topicBuffer, {client: true, server: true}); + joinChatRoom(chatRoomID) { + this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), {client: true, server: true}); this.discovery.flushed().then(() => { console.log(`Bot ${this.botName} joined the chat room.`); this.emit('onBotJoinRoom');