Fixing bot
This commit is contained in:
parent
157c8af4f4
commit
e57db27a13
@ -8,7 +8,8 @@ class Client extends EventEmitter {
|
|||||||
if (!botName) return console.error("BotName is not defined!");
|
if (!botName) return console.error("BotName is not defined!");
|
||||||
this.botName = botName;
|
this.botName = botName;
|
||||||
this.swarm = new Hyperswarm();
|
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();
|
this.setupSwarm();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,12 +17,12 @@ class Client extends EventEmitter {
|
|||||||
this.swarm.on('connection', (peer) => {
|
this.swarm.on('connection', (peer) => {
|
||||||
peer.on('data', message => {
|
peer.on('data', message => {
|
||||||
const messageObj = JSON.parse(message.toString());
|
const messageObj = JSON.parse(message.toString());
|
||||||
if (messageObj.type === "message") {
|
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room
|
||||||
this.currentTopic = messageObj.topic; // Capture the topic from incoming messages
|
this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
|
||||||
this.emit('onMessage', peer, messageObj);
|
if (messageObj.type === "message") this.emit('onMessage', peer, messageObj);
|
||||||
}
|
|
||||||
if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
|
if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
|
||||||
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
|
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
peer.on('error', e => {
|
peer.on('error', e => {
|
||||||
@ -32,17 +33,11 @@ class Client extends EventEmitter {
|
|||||||
|
|
||||||
this.swarm.on('update', () => {
|
this.swarm.on('update', () => {
|
||||||
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
|
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) {
|
joinChatRoom(chatRoomID) {
|
||||||
|
this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms
|
||||||
this.currentTopic = chatRoomID; // Store the current topic
|
this.currentTopic = chatRoomID; // Store the current topic
|
||||||
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true });
|
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true });
|
||||||
this.discovery.flushed().then(() => {
|
this.discovery.flushed().then(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user