Fixing bot messages

This commit is contained in:
Raven Scott 2024-06-10 19:49:46 -04:00
parent e1153cb5df
commit 01fee56692
2 changed files with 24 additions and 12 deletions

1
app.js
View File

@ -433,7 +433,6 @@ function onMessageAdded(from, message, avatar) {
const $img = document.createElement('img'); const $img = document.createElement('img');
$img.src = updatePortInUrl(avatar) || 'https://via.placeholder.com/40'; // Default to a placeholder image if avatar URL is not provided $img.src = updatePortInUrl(avatar) || 'https://via.placeholder.com/40'; // Default to a placeholder image if avatar URL is not provided
console.log(updatePortInUrl(avatar))
$img.classList.add('avatar'); $img.classList.add('avatar');
$div.appendChild($img); $div.appendChild($img);

View File

@ -5,7 +5,7 @@ import b4a from "b4a";
class Client extends EventEmitter { class Client extends EventEmitter {
constructor(botName) { constructor(botName) {
super(); super();
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.setupSwarm(); this.setupSwarm();
@ -14,9 +14,10 @@ class Client extends EventEmitter {
setupSwarm() { setupSwarm() {
this.swarm.on('connection', (peer) => { this.swarm.on('connection', (peer) => {
peer.on('data', message => { peer.on('data', message => {
if(message.type === "message") this.emit('onMessage', peer, JSON.parse(message.toString())); const messageObj = JSON.parse(message.toString());
if(message.type === "icon") this.emit('onIcon', peer, JSON.parse(message.toString())); if (messageObj.type === "message") this.emit('onMessage', peer, messageObj);
if(message.type === "file") this.emit('onFile', peer, JSON.parse(message.toString())); if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
}); });
peer.on('error', e => { peer.on('error', e => {
@ -33,14 +34,14 @@ class Client extends EventEmitter {
const peer = [peerId]; const peer = [peerId];
const peerTopics = [peerInfo.topics] const peerTopics = [peerInfo.topics]
.filter(topics => topics) .filter(topics => topics)
.map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); .map(topics => topics.map(topic => b4a.toString(topic, 'hex')));
}); });
}); });
} }
joinChatRoom(chatRoomID) { joinChatRoom(chatRoomID) {
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(() => {
console.log(`Bot ${this.botName} joined the chat room.`); console.log(`Bot ${this.botName} joined the chat room.`);
this.emit('onBotJoinRoom'); this.emit('onBotJoinRoom');
@ -50,16 +51,28 @@ class Client extends EventEmitter {
sendMessage(roomPeers, message) { sendMessage(roomPeers, message) {
console.log('Bot name:', this.botName); console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); // We remove all the peers that arent included in a room const messageObj = {
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp type: 'message', // Add type field
name: this.botName,
message,
timestamp
};
const data = JSON.stringify(messageObj);
const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); // We remove all the peers that aren't included in a room
for (const peer of peers) peer.write(data); for (const peer of peers) peer.write(data);
} }
sendMessageToAll(message) { sendMessageToAll(message) {
console.log('Bot name:', this.botName); console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections] const messageObj = {
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp type: 'message', // Add type field
name: this.botName,
message,
timestamp
};
const data = JSON.stringify(messageObj);
const peers = [...this.swarm.connections];
for (const peer of peers) peer.write(data); for (const peer of peers) peer.write(data);
} }