Fix chatBot

This commit is contained in:
Raven Scott 2024-06-11 17:40:02 -04:00
parent 9a14c10bac
commit b9db313eff
3 changed files with 12 additions and 8 deletions

View File

@ -7,7 +7,7 @@ import FileMessage from "./FileMessage.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.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
@ -48,6 +48,10 @@ class Client extends EventEmitter {
} }
joinChatRoom(chatRoomID) { joinChatRoom(chatRoomID) {
if (!chatRoomID || typeof chatRoomID !== 'string') {
return console.error("Invalid chat room ID!");
}
this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms
this.currentTopic = chatRoomID; // Store the current topic this.currentTopic = chatRoomID; // Store the current topic
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 });

View File

@ -1,5 +1,5 @@
class FileMessage { class FileMessage {
public FileMessage(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) { constructor(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) {
this.peerName = peerName; this.peerName = peerName;
this.fileName = fileName; this.fileName = fileName;
this.fileUrl = fileUrl; this.fileUrl = fileUrl;
@ -9,7 +9,7 @@ class FileMessage {
this.timestamp = timestamp; this.timestamp = timestamp;
} }
public toJsonString() { toJsonString() {
return JSON.stringify({ return JSON.stringify({
type: 'file', type: 'file',
name: this.peerName, name: this.peerName,
@ -23,4 +23,4 @@ class FileMessage {
} }
} }
export default FileMessage; export default FileMessage;

View File

@ -1,5 +1,5 @@
class TextMessage { class TextMessage {
public TextMessage(peerName, peerAvatar, topic, message, timestamp) { constructor(peerName, peerAvatar, topic, message, timestamp) {
this.peerName = peerName; this.peerName = peerName;
this.peerAvatar = peerAvatar; this.peerAvatar = peerAvatar;
this.topic = topic; this.topic = topic;
@ -7,7 +7,7 @@ class TextMessage {
this.timestamp = timestamp; this.timestamp = timestamp;
} }
public toJsonString() { toJsonString() {
return JSON.stringify({ return JSON.stringify({
type: 'message', type: 'message',
name: this.peerName, name: this.peerName,
@ -18,9 +18,9 @@ class TextMessage {
}); });
} }
public static new(bot, message) { static new(bot, message) {
return new TextMessage(bot.botName, "", bot.currentTopic, message, Date.now()); return new TextMessage(bot.botName, "", bot.currentTopic, message, Date.now());
} }
} }
export default TextMessage; export default TextMessage;