Fixing bot

This commit is contained in:
Raven Scott 2024-06-10 22:20:36 -04:00
parent 157c8af4f4
commit e57db27a13

View File

@ -8,7 +8,8 @@ class Client extends EventEmitter {
if (!botName) return console.error("BotName is not defined!");
this.botName = botName;
this.swarm = new Hyperswarm();
this.currentTopic = null; // Store the current topic
this.joinedRooms = new Set(); // Track the rooms the bot has joined
this.currentTopic = null; // Track the current topic
this.setupSwarm();
}
@ -16,12 +17,12 @@ class Client extends EventEmitter {
this.swarm.on('connection', (peer) => {
peer.on('data', message => {
const messageObj = JSON.parse(message.toString());
if (messageObj.type === "message") {
this.currentTopic = messageObj.topic; // Capture the topic from incoming messages
this.emit('onMessage', peer, messageObj);
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room
this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
if (messageObj.type === "message") this.emit('onMessage', peer, messageObj);
if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
}
if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
});
peer.on('error', e => {
@ -32,17 +33,11 @@ class Client extends EventEmitter {
this.swarm.on('update', () => {
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
this.swarm.peers.forEach((peerInfo, peerId) => {
const peer = [peerId];
const peerTopics = [peerInfo.topics]
.filter(topics => topics)
.map(topics => topics.map(topic => b4a.toString(topic, 'hex')));
});
});
}
joinChatRoom(chatRoomID) {
this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms
this.currentTopic = chatRoomID; // Store the current topic
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true });
this.discovery.flushed().then(() => {