From b244aa9bdfb02ed1e838cd14d723dd8f95b3ea84 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:29:51 +0300 Subject: [PATCH 1/4] Added Event enum and 2 new events --- chatBot/bot.js | 9 +++++++-- chatBot/includes/client.js | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index 9f9e0a9..2122221 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -38,7 +38,7 @@ loadCommands().then(commands => { const bot = new Client(process.env.LINKUP_ROOM_ID, botName); // We use Event Emitter here to handle new messages - bot.on('onMessage', (peer, message) => { + bot.on(Event.onMessage, (peer, message) => { if (message.type == "icon") return; console.log(message); @@ -57,7 +57,12 @@ loadCommands().then(commands => { commandHandler.handler(bot, args, message); } } - }) + }); + + bot.on(Event.onBotJoinRoom, () => { + console.log("Bot is ready!"); + }); + bot.joinChatRoom(); // Wait for 2 seconds for the bot to connect diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 1d4369a..52770ed 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -12,8 +12,11 @@ class Client extends EventEmitter { setupSwarm() { this.swarm.on('connection', (peer) => { - peer.on('data', message => this.emit("onMessage", peer, JSON.parse(message.toString()))); - peer.on('error', e => console.log(`Connection error: ${e}`)); + peer.on('data', message => this.emit(Event.onMessage, peer, JSON.parse(message.toString()))); + peer.on('error', e => { + this.emit(Event.onError, e); + console.error(`Connection error: ${e}`) + }); }); this.swarm.on('update', () => { @@ -24,6 +27,7 @@ class Client extends EventEmitter { joinChatRoom() { this.discovery = this.swarm.join(this.topicBuffer, { client: true, server: true }); this.discovery.flushed().then(() => { + this.emit(Event.onBotJoinRoom) console.log(`Bot ${this.botName} joined the chat room.`); }); } @@ -42,4 +46,10 @@ class Client extends EventEmitter { } } -export default Client; +enum Event { + onMessage, + onError, + onBotJoinRoom +} + +export default { Client, Event }; From 164efc5b7294ab2f7a1bd8fb088ef6e84faba41f Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:31:24 +0300 Subject: [PATCH 2/4] Removed Enum class because it does not work :sob: --- chatBot/bot.js | 4 ++-- chatBot/includes/client.js | 18 ++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index 2122221..e23e5f1 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -38,7 +38,7 @@ loadCommands().then(commands => { const bot = new Client(process.env.LINKUP_ROOM_ID, botName); // We use Event Emitter here to handle new messages - bot.on(Event.onMessage, (peer, message) => { + bot.on('onMessage', (peer, message) => { if (message.type == "icon") return; console.log(message); @@ -59,7 +59,7 @@ loadCommands().then(commands => { } }); - bot.on(Event.onBotJoinRoom, () => { + bot.on('onBotJoinRoom', () => { console.log("Bot is ready!"); }); diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 52770ed..e20c13a 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -12,9 +12,9 @@ class Client extends EventEmitter { setupSwarm() { this.swarm.on('connection', (peer) => { - peer.on('data', message => this.emit(Event.onMessage, peer, JSON.parse(message.toString()))); + peer.on('data', message => this.emit('onMessage', peer, JSON.parse(message.toString()))); peer.on('error', e => { - this.emit(Event.onError, e); + this.emit('onError', e); console.error(`Connection error: ${e}`) }); }); @@ -25,9 +25,9 @@ class Client extends EventEmitter { } joinChatRoom() { - this.discovery = this.swarm.join(this.topicBuffer, { client: true, server: true }); + this.discovery = this.swarm.join(this.topicBuffer, {client: true, server: true}); this.discovery.flushed().then(() => { - this.emit(Event.onBotJoinRoom) + this.emit('onBotJoinRoom') console.log(`Bot ${this.botName} joined the chat room.`); }); } @@ -36,7 +36,7 @@ class Client extends EventEmitter { console.log('Bot name:', this.botName); const timestamp = Date.now(); // Generate timestamp const peers = [...this.swarm.connections]; - const data = JSON.stringify({ name: this.botName, message, timestamp }); // Include timestamp + const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp for (const peer of peers) peer.write(data); } @@ -46,10 +46,4 @@ class Client extends EventEmitter { } } -enum Event { - onMessage, - onError, - onBotJoinRoom -} - -export default { Client, Event }; +export default Client; From cd5cfb20a429bafc0521758e6a0d50de675f3b54 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:36:00 +0300 Subject: [PATCH 3/4] Replaced 'setTimeOut' with bot.on('onBotJoinRoom', () => {}); for sending ON_READY_MESSAGE to the chat --- chatBot/bot.js | 7 +------ chatBot/includes/client.js | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index e23e5f1..4a3e775 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -61,15 +61,10 @@ loadCommands().then(commands => { bot.on('onBotJoinRoom', () => { console.log("Bot is ready!"); + bot.sendMessage(process.env.ON_READY_MESSAGE); }); bot.joinChatRoom(); - - // Wait for 2 seconds for the bot to connect - setTimeout(() => { - // Send a message - bot.sendMessage(process.env.ON_READY_MESSAGE); - }, 2000); // Adjust the delay as necessary }).catch(error => { console.error('Error loading commands:', error); }); diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index e20c13a..7d61fbd 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -27,8 +27,8 @@ class Client extends EventEmitter { joinChatRoom() { this.discovery = this.swarm.join(this.topicBuffer, {client: true, server: true}); this.discovery.flushed().then(() => { - this.emit('onBotJoinRoom') console.log(`Bot ${this.botName} joined the chat room.`); + this.emit('onBotJoinRoom'); }); } From 9f2353c9025d04095f9e642ad3a28a174f0e8356 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:38:44 +0300 Subject: [PATCH 4/4] Added check if botName or chatRoomID is not defined --- chatBot/includes/client.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 7d61fbd..5033659 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -4,6 +4,9 @@ import EventEmitter from 'node:events' class Client extends EventEmitter { constructor(chatRoomID, botName) { super(); + + if(!chatRoomID || !botName) return console.error("ChatRoomID or BotName is not defined!"); + this.topicBuffer = Buffer.from(chatRoomID, 'hex'); this.botName = botName; this.swarm = new Hyperswarm();