From de005143971cf8833cbac64bfcf3d2e4e994bb59 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sat, 8 Jun 2024 22:34:25 +0300 Subject: [PATCH] I actually dont know if it works or not. Changes: Replaced TopicBuffer with chatRoomID and now topicBuffer is being created directly in Client class; Replaced chatBot with Client; Client now extends EventEmitter; bot.on('onMessage', (pear, message) => {}) is now a thing instead of directly putting method in chatBot constructor --- chatBot/bot.js | 21 +++++++++------------ chatBot/includes/{chatBot.js => client.js} | 13 +++++++------ package.json | 1 + 3 files changed, 17 insertions(+), 18 deletions(-) rename chatBot/includes/{chatBot.js => client.js} (79%) diff --git a/chatBot/bot.js b/chatBot/bot.js index 1c356f2..9f9e0a9 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -1,14 +1,10 @@ import fs from 'fs'; import path from 'path'; -import chatBot from './includes/chatBot.js'; // Adjust the import path as necessary -import 'dotenv/config' - -// Generate a Buffer from the hexadecimal topic value -const topicHex = process.env.LINKUP_ROOM_ID; -const topicBuffer = Buffer.from(topicHex, 'hex'); +import Client from './includes/client.js'; // Adjust the import path as necessary +import 'dotenv/config'; // Create a new instance of the chatBot class with a valid botName -const botName = process.env.BOT_NAME;; // Replace 'MyBot' with the desired bot name +const botName = process.env.BOT_NAME; // Replace 'MyBot' with the desired bot name // Load commands from the 'commands' directory const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname); @@ -38,7 +34,11 @@ async function loadCommands() { } loadCommands().then(commands => { - const onMessageReceived = (peer, message) => { + // Create the chatBot instance with the defined onMessageReceived function + const bot = new Client(process.env.LINKUP_ROOM_ID, botName); + + // We use Event Emitter here to handle new messages + bot.on('onMessage', (peer, message) => { if (message.type == "icon") return; console.log(message); @@ -57,10 +57,7 @@ loadCommands().then(commands => { commandHandler.handler(bot, args, message); } } - }; - - // Create the chatBot instance with the defined onMessageReceived function - const bot = new chatBot(topicBuffer, botName, onMessageReceived); + }) bot.joinChatRoom(); // Wait for 2 seconds for the bot to connect diff --git a/chatBot/includes/chatBot.js b/chatBot/includes/client.js similarity index 79% rename from chatBot/includes/chatBot.js rename to chatBot/includes/client.js index 82ab798..1d4369a 100644 --- a/chatBot/includes/chatBot.js +++ b/chatBot/includes/client.js @@ -1,17 +1,18 @@ import Hyperswarm from 'hyperswarm'; +import EventEmitter from 'node:events' -class chatBot { - constructor(topicBuffer, botName, onMessageReceived) { - this.topicBuffer = topicBuffer; +class Client extends EventEmitter { + constructor(chatRoomID, botName) { + super(); + this.topicBuffer = Buffer.from(chatRoomID, 'hex'); this.botName = botName; this.swarm = new Hyperswarm(); - this.onMessageReceived = onMessageReceived; this.setupSwarm(); } setupSwarm() { this.swarm.on('connection', (peer) => { - peer.on('data', message => this.onMessageReceived(peer, JSON.parse(message.toString()))); + peer.on('data', message => this.emit("onMessage", peer, JSON.parse(message.toString()))); peer.on('error', e => console.log(`Connection error: ${e}`)); }); @@ -41,4 +42,4 @@ class chatBot { } } -export default chatBot; +export default Client; diff --git a/package.json b/package.json index 446d262..1edae13 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "dependencies": { "b4a": "^1.6.6", "corestore": "^6.18.2", + "dotenv": "^16.4.5", "electron": "^30.0.8", "hypercore-crypto": "^3.4.1", "hyperdrive": "^11.8.1", -- 2.43.5