Compare commits

..

3 Commits

Author SHA1 Message Date
MrMasrozYTLIVE
b3ccda32ea did lots of work on bot package 2024-06-14 16:43:17 +03:00
Raven Scott
bd885bb591 Fix peerName in Message received 2024-06-14 04:46:19 -04:00
66dc7cdac2 Merge pull request 'Fix shutdown' (#9) from MiTask/LinkUp-P2P-Chat:main into main
Reviewed-on: snxraven/LinkUp-P2P-Chat#9
2024-06-14 08:44:09 +00:00
7 changed files with 79 additions and 31 deletions

7
app.js
View File

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

View File

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

View File

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

View File

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

View File

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