Compare commits

...

6 Commits

4 changed files with 23 additions and 12 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ storage_*
storage
chatBot/.env
chatBot/commands/ai.js
config.json

16
app.js
View File

@ -7,7 +7,7 @@ import Corestore from 'corestore';
import { EventEmitter } from 'events';
import fs from "fs";
const storagePath = `./storage/storage_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
const storagePath = `./storage/`;
const store = new Corestore(storagePath);
const drive = new Hyperdrive(store);
@ -99,9 +99,14 @@ async function initialize() {
if (configExists) {
config = JSON.parse(fs.readFileSync("./config.json", 'utf8'));
console.log("Read config from file:", config)
// Update port in URLs
config.userAvatar = updatePortInUrl(config.userAvatar);
config.rooms.forEach(room => {
addRoomToListWithoutWritingToConfig(room);
});
for (let user in registeredUsers) {
registeredUsers[user] = updatePortInUrl(registeredUsers[user]);
}
}
const registerDiv = document.querySelector('#register');
@ -175,7 +180,7 @@ function registerUser(e) {
const buffer = new Uint8Array(event.target.result);
await drive.put(`/icons/${regUsername}.png`, buffer);
config.userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL
registeredUsers[regUsername] = config.userAvatar;
registeredUsers[regUsername] = `http://localhost:${servePort}/icons/${regUsername}.png`; // Use placeholder URL
localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers));
continueRegistration(regUsername);
};
@ -438,4 +443,11 @@ function writeConfigToFile(filePath) {
});
}
function updatePortInUrl(url) {
if (!url) return url;
const urlObject = new URL(url);
urlObject.port = servePort;
return urlObject.toString();
}
initialize();

View File

@ -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);
});

View File

@ -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');