Compare commits

...

11 Commits

Author SHA1 Message Date
1c07819d91 Merge pull request 'Fixed rooms not being deleted from the json file' (#4) from MiTask/LinkUp-P2P-Chat:main into main
Reviewed-on: #4

#killinit. - Coding and vibin like it should be!
2024-06-09 08:19:59 +00:00
MrMasrozYTLIVE
8bac7def09 I fr fixed it this time 2024-06-09 11:17:20 +03:00
MrMasrozYTLIVE
d44ef48d89 I guess i know why it doesnt delete the room now 2024-06-09 11:16:22 +03:00
MrMasrozYTLIVE
2a2fbed732 Well i didnt... 2024-06-09 11:15:05 +03:00
MrMasrozYTLIVE
c59795a407 Well i hope i fixed it now 2024-06-09 11:12:40 +03:00
MrMasrozYTLIVE
33e0a13f93 Merge branch 'main' of https://git.ssh.surf/snxraven/LinkUp-P2P-Chat 2024-06-09 11:10:02 +03:00
MrMasrozYTLIVE
840b6e2069 Fixed removing rooms 2024-06-09 11:09:58 +03:00
MrMasrozYTLIVE
72bf632cd4 I dont know what to say here because i just need up-to-date repo on my laptop 2024-06-09 11:07:52 +03:00
MrMasrozYTLIVE
966873774c Merge branch 'main' of https://git.ssh.surf/snxraven/LinkUp-P2P-Chat 2024-06-09 11:05:42 +03:00
MrMasrozYTLIVE
3df73b97c5 Guess gonna try this and then really take a break 2024-06-09 10:55:54 +03:00
MrMasrozYTLIVE
824e2dab42 Started working on making the bot be in multiple chatrooms 2024-06-09 10:51:15 +03:00
3 changed files with 12 additions and 13 deletions

7
app.js
View File

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

View File

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

View File

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