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');
$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');
$div.appendChild($img);

View File

@ -5,7 +5,7 @@ import b4a from "b4a";
class Client extends EventEmitter {
constructor(botName) {
super();
if(!botName) return console.error("BotName is not defined!");
if (!botName) return console.error("BotName is not defined!");
this.botName = botName;
this.swarm = new Hyperswarm();
this.setupSwarm();
@ -14,9 +14,10 @@ class Client extends EventEmitter {
setupSwarm() {
this.swarm.on('connection', (peer) => {
peer.on('data', message => {
if(message.type === "message") this.emit('onMessage', peer, JSON.parse(message.toString()));
if(message.type === "icon") this.emit('onIcon', peer, JSON.parse(message.toString()));
if(message.type === "file") this.emit('onFile', peer, JSON.parse(message.toString()));
const messageObj = JSON.parse(message.toString());
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);
});
peer.on('error', e => {
@ -40,7 +41,7 @@ class Client extends EventEmitter {
}
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(() => {
console.log(`Bot ${this.botName} joined the chat room.`);
this.emit('onBotJoinRoom');
@ -50,16 +51,28 @@ class Client extends EventEmitter {
sendMessage(roomPeers, message) {
console.log('Bot name:', this.botName);
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 data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp
const messageObj = {
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);
}
sendMessageToAll(message) {
console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections]
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp
const messageObj = {
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);
}