Compare commits

..

No commits in common. "824e2dab4280fa5fa655be5866343107ee5cf41c" and "0077989253a5d956372f174904b104718db50b98" have entirely different histories.

4 changed files with 12 additions and 23 deletions

1
.gitignore vendored
View File

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

16
app.js
View File

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

View File

@ -35,7 +35,7 @@ async function loadCommands() {
loadCommands().then(commands => { loadCommands().then(commands => {
// Create the chatBot instance with the defined onMessageReceived function // Create the chatBot instance with the defined onMessageReceived function
const bot = new Client(botName); const bot = new Client(process.env.LINKUP_ROOM_ID, botName);
// We use Event Emitter here to handle new messages // We use Event Emitter here to handle new messages
bot.on('onMessage', (peer, message) => { bot.on('onMessage', (peer, message) => {
@ -64,8 +64,7 @@ loadCommands().then(commands => {
bot.sendMessage(process.env.ON_READY_MESSAGE); bot.sendMessage(process.env.ON_READY_MESSAGE);
}); });
bot.joinChatRoom(1234); bot.joinChatRoom();
bot.joinChatRoom(5678);
}).catch(error => { }).catch(error => {
console.error('Error loading commands:', error); console.error('Error loading commands:', error);
}); });

View File

@ -2,9 +2,12 @@ import Hyperswarm from 'hyperswarm';
import EventEmitter from 'node:events' import EventEmitter from 'node:events'
class Client extends EventEmitter { class Client extends EventEmitter {
constructor(botName) { constructor(chatRoomID, botName) {
super(); super();
if(!botName) return console.error("BotName is not defined!");
if(!chatRoomID || !botName) return console.error("ChatRoomID or BotName is not defined!");
this.topicBuffer = Buffer.from(chatRoomID, 'hex');
this.botName = botName; this.botName = botName;
this.swarm = new Hyperswarm(); this.swarm = new Hyperswarm();
this.setupSwarm(); this.setupSwarm();
@ -24,8 +27,8 @@ class Client extends EventEmitter {
}); });
} }
joinChatRoom(chatRoomID) { joinChatRoom() {
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), {client: true, server: true}); this.discovery = this.swarm.join(this.topicBuffer, {client: true, server: true});
this.discovery.flushed().then(() => { this.discovery.flushed().then(() => {
console.log(`Bot ${this.botName} joined the chat room.`); console.log(`Bot ${this.botName} joined the chat room.`);
this.emit('onBotJoinRoom'); this.emit('onBotJoinRoom');