Compare commits

..

No commits in common. "b3ccda32ea91d5b3f4dca6c384aa228e19421472" and "624222f7269f05193557a64725caca68adf28bc3" have entirely different histories.

7 changed files with 31 additions and 79 deletions

7
app.js
View File

@ -213,7 +213,6 @@ async function handleConnection(connection, info) {
type: 'icon', type: 'icon',
username: config.userName, username: config.userName,
avatar: b4a.toString(iconBuffer, 'base64'), avatar: b4a.toString(iconBuffer, 'base64'),
timestamp: Date.now()
}); });
console.log('Sending icon to new peer:', iconMessage); console.log('Sending icon to new peer:', iconMessage);
connection.write(iconMessage); connection.write(iconMessage);
@ -293,8 +292,7 @@ function setupTalkButton() {
audio: b4a.toString(buffer, 'base64'), audio: b4a.toString(buffer, 'base64'),
audioType: audioBlob.type, audioType: audioBlob.type,
avatar: updatePortInUrl(config.userAvatar), avatar: updatePortInUrl(config.userAvatar),
topic: topic, topic: topic
timestamp: Date.now()
}; };
console.log('Sending audio message:', audioMessage); // Debugging log console.log('Sending audio message:', audioMessage); // Debugging log
@ -596,8 +594,7 @@ async function handleFileInput(event) {
file: b4a.toString(buffer, 'base64'), file: b4a.toString(buffer, 'base64'),
fileType: file.type, fileType: file.type,
avatar: updatePortInUrl(config.userAvatar), avatar: updatePortInUrl(config.userAvatar),
topic: topic, topic: topic
timestamp: Date.now()
}; };
console.log('Sending file message:', fileMessage); // Debugging log console.log('Sending file message:', fileMessage); // Debugging log

View File

@ -19,14 +19,14 @@ async function loadCommands() {
for (const file of files) { for (const file of files) {
// Check if the file is a JavaScript file // Check if the file is a JavaScript file
if (file.endsWith('.js')) { if (file.endsWith('.js')) {
// const commandName = path.basename(file, '.js'); const commandName = path.basename(file, '.js');
const commandPath = path.join(commandsDir, file); const commandPath = path.join(commandsDir, file);
// Dynamically import the command module // Dynamically import the command module
const { default: commandModule } = await import(commandPath); const { default: commandModule } = await import(commandPath);
// Add the command module to the commands object // Add the command module to the commands object
commands[commandPath] = commandModule; commands[commandName] = commandModule;
} }
} }
@ -41,7 +41,7 @@ loadCommands().then(commands => {
bot.on('onMessage', (peer, message) => { bot.on('onMessage', (peer, message) => {
console.log(message); console.log(message);
console.log(`Message received from ${message.peerName}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
// Check if the message starts with a command prefix // Check if the message starts with a command prefix
if (message.message.startsWith('!')) { if (message.message.startsWith('!')) {

View File

@ -1,23 +0,0 @@
import Message from "./Message.js";
class AudioMessage extends Message {
constructor(peerName, peerAvatar, topic, timestamp, audio, audioType) {
super("audio", peerName, peerAvatar, topic, timestamp);
this.audio = audio;
this.audioType = audioType;
}
toJsonString() {
return JSON.stringify({
...this.toJson(),
audio: this.audio,
audioType: this.audioType
});
}
static new(bot, audio, audioType) {
return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audio, audioType);
}
}
export default AudioMessage;

View File

@ -3,14 +3,12 @@ import EventEmitter from 'node:events';
import b4a from "b4a"; import b4a from "b4a";
import TextMessage from "./TextMessage.js"; import TextMessage from "./TextMessage.js";
import FileMessage from "./FileMessage.js"; import FileMessage from "./FileMessage.js";
import AudioMessage from "./AudioMessage.js";
class Client extends EventEmitter { class Client extends EventEmitter {
constructor(botName) { constructor(botName) {
super(); super();
if (!botName) return console.error("Bot Name is not defined!"); if (!botName) return console.error("Bot Name is not defined!");
this.botName = botName; this.botName = botName;
this.botAvatar = "";
this.swarm = new Hyperswarm(); this.swarm = new Hyperswarm();
this.joinedRooms = new Set(); // Track the rooms the bot has joined this.joinedRooms = new Set(); // Track the rooms the bot has joined
this.currentTopic = null; // Track the current topic this.currentTopic = null; // Track the current topic
@ -43,16 +41,13 @@ class Client extends EventEmitter {
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room 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 this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
if (messageObj.type === "message") if (messageObj.type === "message")
this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.message)); this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.message, messageObj.timestamp));
if (messageObj.type === "file") if (messageObj.type === "file")
this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.avatar, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp)); this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp));
if (messageObj.type === "icon") if (messageObj.type === "icon")
this.emit('onIcon', peer, messageObj); this.emit('onIcon', peer, messageObj);
if (messageObj.type === "audio")
this.emit('onAudio', peer, new AudioMessage());
} }
}); });
@ -86,8 +81,6 @@ class Client extends EventEmitter {
} }
sendMessage(message) { sendMessage(message) {
console.log(typeof message);
console.log('Bot name:', this.botName); console.log('Bot name:', this.botName);
const data = message.toJsonString(); const data = message.toJsonString();
const peers = [...this.swarm.connections]; const peers = [...this.swarm.connections];

View File

@ -1,25 +1,26 @@
import Message from "./Message.js"; class FileMessage {
constructor(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) {
class FileMessage extends Message { this.peerName = peerName;
constructor(peerName, peerAvatar, topic, timestamp, fileName, fileUrl, fileType) {
super("file", peerName, peerAvatar, topic, timestamp);
this.fileName = fileName; this.fileName = fileName;
this.fileUrl = fileUrl; this.fileUrl = fileUrl;
this.fileType = fileType; this.fileType = fileType;
this.peerAvatar = peerAvatar;
this.topic = topic;
this.timestamp = timestamp;
} }
toJsonString() { toJsonString() {
return JSON.stringify({ return JSON.stringify({
...this.toJson(), type: 'file',
name: this.peerName,
fileName: this.fileName, fileName: this.fileName,
fileUrl: this.fileUrl, fileUrl: this.fileUrl,
fileType: this.fileType fileType: this.fileType,
avatar: this.peerAvatar,
topic: this.topic,
timestamp: this.timestamp,
}); });
} }
static new(bot, fileName, fileUrl, fileType) {
return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType);
}
} }
export default FileMessage; export default FileMessage;

View File

@ -1,21 +0,0 @@
class Message {
constructor(messageType, peerName, peerAvatar, topic, timestamp) {
this.messageType = messageType;
this.peerName = peerName;
this.peerAvatar = peerAvatar;
this.topic = topic;
this.timestamp = timestamp;
}
toJson() {
return {
type: this.messageType,
name: this.peerName,
avatar: this.peerAvatar,
topic: this.topic,
timestamp: this.timestamp
};
}
}
export default Message;

View File

@ -1,20 +1,25 @@
import Message from "./Message.js"; class TextMessage {
constructor(peerName, peerAvatar, topic, message, timestamp) {
class TextMessage extends Message { this.peerName = peerName;
constructor(peerName, peerAvatar, topic, timestamp, message) { this.peerAvatar = peerAvatar;
super("message", peerName, peerAvatar, topic, timestamp); this.topic = topic;
this.message = message; this.message = message;
this.timestamp = timestamp;
} }
toJsonString() { toJsonString() {
return JSON.stringify({ return JSON.stringify({
...this.toJson(), type: 'message',
name: this.peerName,
message: this.message, message: this.message,
avatar: this.peerAvatar,
topic: this.topic,
timestamp: this.timestamp
}); });
} }
static new(bot, message) { static new(bot, message) {
return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message); return new TextMessage(bot.botName, "", bot.currentTopic, message, Date.now());
} }
} }