did lots of work on bot package

This commit is contained in:
MrMasrozYTLIVE 2024-06-14 16:43:17 +03:00
parent bd885bb591
commit b3ccda32ea
7 changed files with 78 additions and 30 deletions

7
app.js
View File

@ -213,6 +213,7 @@ 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);
@ -292,7 +293,8 @@ 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
@ -594,7 +596,8 @@ 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[commandName] = commandModule; commands[commandPath] = commandModule;
} }
} }

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 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
@ -41,13 +43,16 @@ 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.message, messageObj.timestamp)); this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.message));
if (messageObj.type === "file") 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") if (messageObj.type === "icon")
this.emit('onIcon', peer, messageObj); 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) { 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,26 +1,25 @@
class FileMessage { import Message from "./Message.js";
constructor(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) {
this.peerName = peerName; class FileMessage extends Message {
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({
type: 'file', ...this.toJson(),
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

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