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