Compare commits

..

No commits in common. "1c07819d91160634e4978bf57ec57b20c56f7659" and "babde932e782633ea36e8730524f9f0de28bb385" have entirely different histories.

3 changed files with 13 additions and 12 deletions

7
app.js
View File

@ -278,15 +278,14 @@ function leaveRoom() {
if (roomItem) { if (roomItem) {
roomItem.remove(); roomItem.remove();
} }
config.rooms = config.rooms.filter(e => e !== topic);
writeConfigToFile("./config.json");
currentRoom.destroy(); currentRoom.destroy();
currentRoom = null; currentRoom = null;
} }
document.querySelector('#chat').classList.add('hidden'); document.querySelector('#chat').classList.add('hidden');
document.querySelector('#setup').classList.remove('hidden'); document.querySelector('#setup').classList.remove('hidden');
config.rooms = config.rooms.filter(e => e !== currentRoom.topic);
writeConfigToFile("./config.json");
} }
function sendMessage(e) { function sendMessage(e) {

View File

@ -35,14 +35,14 @@ async function loadCommands() {
loadCommands().then(commands => { loadCommands().then(commands => {
// Create the chatBot instance with the defined onMessageReceived function // Create the chatBot instance with the defined onMessageReceived function
const bot = new Client(botName); const bot = new Client(process.env.LINKUP_ROOM_ID, botName);
// We use Event Emitter here to handle new messages // We use Event Emitter here to handle new messages
bot.on('onMessage', (peer, message) => { bot.on('onMessage', (peer, message) => {
if (message.type == "icon") return; if (message.type == "icon") return;
console.log(message); console.log(message);
console.log(`Message received from ${message.name}@${peer} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); console.log(`Message received from ${message.name} 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('!')) {
@ -64,8 +64,7 @@ loadCommands().then(commands => {
bot.sendMessage(process.env.ON_READY_MESSAGE); bot.sendMessage(process.env.ON_READY_MESSAGE);
}); });
bot.joinChatRoom(process.env.LINKUP_ROOM_ID); bot.joinChatRoom();
bot.joinChatRoom(process.env.LINKUP_ROOM_ID2);
}).catch(error => { }).catch(error => {
console.error('Error loading commands:', error); console.error('Error loading commands:', error);
}); });

View File

@ -2,9 +2,12 @@ import Hyperswarm from 'hyperswarm';
import EventEmitter from 'node:events' import EventEmitter from 'node:events'
class Client extends EventEmitter { class Client extends EventEmitter {
constructor(botName) { constructor(chatRoomID, botName) {
super(); super();
if(!botName) return console.error("BotName is not defined!");
if(!chatRoomID || !botName) return console.error("ChatRoomID or BotName is not defined!");
this.topicBuffer = Buffer.from(chatRoomID, 'hex');
this.botName = botName; this.botName = botName;
this.swarm = new Hyperswarm(); this.swarm = new Hyperswarm();
this.setupSwarm(); this.setupSwarm();
@ -24,8 +27,8 @@ class Client extends EventEmitter {
}); });
} }
joinChatRoom(chatRoomID) { joinChatRoom() {
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), {client: true, server: true}); this.discovery = this.swarm.join(this.topicBuffer, {client: true, server: true});
this.discovery.flushed().then(() => { this.discovery.flushed().then(() => {
console.log(`Bot ${this.botName} joined the chat room.`); console.log(`Bot ${this.botName} joined the chat room.`);
this.emit('onBotJoinRoom'); this.emit('onBotJoinRoom');