forked from snxraven/LinkUp-P2P-Chat
Up to date fork #1
92
app.js
92
app.js
@ -130,6 +130,7 @@ async function initialize() {
|
|||||||
|
|
||||||
if (messageObj.type === 'icon') {
|
if (messageObj.type === 'icon') {
|
||||||
const username = messageObj.username;
|
const username = messageObj.username;
|
||||||
|
const topic = messageObj.topic;
|
||||||
if (messageObj.avatar) {
|
if (messageObj.avatar) {
|
||||||
const avatarBuffer = b4a.from(messageObj.avatar, 'base64');
|
const avatarBuffer = b4a.from(messageObj.avatar, 'base64');
|
||||||
await drive.put(`/icons/${username}.png`, avatarBuffer);
|
await drive.put(`/icons/${username}.png`, avatarBuffer);
|
||||||
@ -137,6 +138,7 @@ async function initialize() {
|
|||||||
} else {
|
} else {
|
||||||
console.error('Received icon message with missing avatar data:', messageObj);
|
console.error('Received icon message with missing avatar data:', messageObj);
|
||||||
}
|
}
|
||||||
|
// updatePeerCount(topic);
|
||||||
} else if (messageObj.type === 'file') {
|
} else if (messageObj.type === 'file') {
|
||||||
if (messageObj.file && messageObj.fileName) {
|
if (messageObj.file && messageObj.fileName) {
|
||||||
const fileBuffer = b4a.from(messageObj.file, 'base64');
|
const fileBuffer = b4a.from(messageObj.file, 'base64');
|
||||||
@ -161,20 +163,38 @@ async function initialize() {
|
|||||||
|
|
||||||
swarm.on('connection', async (connection, info) => {
|
swarm.on('connection', async (connection, info) => {
|
||||||
try {
|
try {
|
||||||
const discovery = [...discoveryTopicsMap.entries()].find(([key, value]) => key.id === info.id);
|
connection.on('data', (data) => {
|
||||||
const topic = discovery ? discovery[1] : null;
|
const messageObj = JSON.parse(data.toString());
|
||||||
|
if (messageObj.type === 'icon' && messageObj.topic) {
|
||||||
if (!topic) {
|
connection.topic = messageObj.topic;
|
||||||
console.error('No topic found in connection info:', info);
|
if (!peerCounts[messageObj.topic]) {
|
||||||
return;
|
peerCounts[messageObj.topic] = 0;
|
||||||
}
|
}
|
||||||
|
peerCounts[messageObj.topic]++;
|
||||||
if (!peerCounts[topic]) {
|
updateHeaderPeerCount(messageObj.topic);
|
||||||
peerCounts[topic] = 0;
|
|
||||||
}
|
}
|
||||||
peerCounts[topic]++;
|
eventEmitter.emit('onMessage', messageObj);
|
||||||
updatePeerCount(topic);
|
});
|
||||||
console.log('Peer connected, current peer count:', peerCounts[topic]);
|
|
||||||
|
connection.on('close', () => {
|
||||||
|
const topic = connection.topic;
|
||||||
|
if (topic && peerCounts[topic]) {
|
||||||
|
peerCounts[topic]--;
|
||||||
|
updateHeaderPeerCount(topic);
|
||||||
|
console.log('Peer disconnected, current peer count:', peerCounts[topic]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sending current user's topic to the new peer
|
||||||
|
const currentTopic = document.querySelector('#chat-room-topic').innerText;
|
||||||
|
connection.topic = currentTopic;
|
||||||
|
|
||||||
|
if (!peerCounts[currentTopic]) {
|
||||||
|
peerCounts[currentTopic] = 0;
|
||||||
|
}
|
||||||
|
peerCounts[currentTopic]++;
|
||||||
|
updateHeaderPeerCount(currentTopic);
|
||||||
|
console.log('Peer connected, current peer count:', peerCounts[currentTopic]);
|
||||||
|
|
||||||
// Send the current user's icon to the new peer
|
// Send the current user's icon to the new peer
|
||||||
const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
|
const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
|
||||||
@ -183,21 +203,17 @@ async function initialize() {
|
|||||||
type: 'icon',
|
type: 'icon',
|
||||||
username: config.userName,
|
username: config.userName,
|
||||||
avatar: b4a.toString(iconBuffer, 'base64'),
|
avatar: b4a.toString(iconBuffer, 'base64'),
|
||||||
topic
|
topic: currentTopic
|
||||||
|
});
|
||||||
|
connection.write(iconMessage);
|
||||||
|
} else {
|
||||||
|
const iconMessage = JSON.stringify({
|
||||||
|
type: 'icon',
|
||||||
|
username: config.userName,
|
||||||
|
topic: currentTopic
|
||||||
});
|
});
|
||||||
connection.write(iconMessage);
|
connection.write(iconMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.on('data', (data) => {
|
|
||||||
const messageObj = JSON.parse(data.toString());
|
|
||||||
eventEmitter.emit('onMessage', messageObj);
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('close', () => {
|
|
||||||
peerCounts[topic]--;
|
|
||||||
updatePeerCount(topic);
|
|
||||||
console.log('Peer disconnected, current peer count:', peerCounts[topic]);
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error handling connection:', error);
|
console.error('Error handling connection:', error);
|
||||||
}
|
}
|
||||||
@ -601,27 +617,6 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar, topic) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePeerCount(topic) {
|
|
||||||
let peerCountElement = document.querySelector(`#peers-count-${topic}`);
|
|
||||||
if (!peerCountElement) {
|
|
||||||
const roomItem = document.querySelector(`#room-list li[data-topic="${topic}"]`);
|
|
||||||
if (roomItem) {
|
|
||||||
peerCountElement = document.createElement('span');
|
|
||||||
peerCountElement.id = `peers-count-${topic}`;
|
|
||||||
// roomItem.appendChild(peerCountElement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (peerCountElement) {
|
|
||||||
peerCountElement.textContent = ` (${peerCounts[topic]})`; // Display the peer count for the specific topic
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the header peer count if the current room matches the topic
|
|
||||||
const currentTopic = document.querySelector('#chat-room-topic').innerText;
|
|
||||||
if (currentTopic === topic) {
|
|
||||||
updateHeaderPeerCount(topic);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateHeaderPeerCount(topic) {
|
function updateHeaderPeerCount(topic) {
|
||||||
const peerCountElement = document.querySelector('#peers-count');
|
const peerCountElement = document.querySelector('#peers-count');
|
||||||
if (peerCountElement) {
|
if (peerCountElement) {
|
||||||
@ -637,6 +632,7 @@ function scrollToBottom() {
|
|||||||
|
|
||||||
function onMessageAdded(from, message, avatar, topic) {
|
function onMessageAdded(from, message, avatar, topic) {
|
||||||
console.log('Adding message:', { from, message, avatar, topic }); // Debugging log
|
console.log('Adding message:', { from, message, avatar, topic }); // Debugging log
|
||||||
|
|
||||||
const messageObj = {
|
const messageObj = {
|
||||||
from,
|
from,
|
||||||
message,
|
message,
|
||||||
@ -667,6 +663,7 @@ function onMessageAdded(from, message, avatar, topic) {
|
|||||||
const $text = document.createElement('div');
|
const $text = document.createElement('div');
|
||||||
$text.classList.add('message-text');
|
$text.classList.add('message-text');
|
||||||
|
|
||||||
|
if (typeof message === 'string') {
|
||||||
const md = window.markdownit({
|
const md = window.markdownit({
|
||||||
highlight: function (str, lang) {
|
highlight: function (str, lang) {
|
||||||
if (lang && hljs.getLanguage(lang)) {
|
if (lang && hljs.getLanguage(lang)) {
|
||||||
@ -680,6 +677,10 @@ function onMessageAdded(from, message, avatar, topic) {
|
|||||||
|
|
||||||
const markdownContent = md.render(message);
|
const markdownContent = md.render(message);
|
||||||
$text.innerHTML = markdownContent;
|
$text.innerHTML = markdownContent;
|
||||||
|
} else {
|
||||||
|
console.error('Message content is not a string:', message);
|
||||||
|
$text.textContent = 'Invalid message content.';
|
||||||
|
}
|
||||||
|
|
||||||
$content.appendChild($header);
|
$content.appendChild($header);
|
||||||
$content.appendChild($text);
|
$content.appendChild($text);
|
||||||
@ -799,7 +800,6 @@ function renderMessagesForRoom(topic) {
|
|||||||
onMessageAdded(message.from, message.message, message.avatar, topic);
|
onMessageAdded(message.from, message.message, message.avatar, topic);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMessagesForRoom(topic) {
|
function getMessagesForRoom(topic) {
|
||||||
return messagesStore[topic] || [];
|
return messagesStore[topic] || [];
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import 'dotenv/config';
|
|||||||
|
|
||||||
// Create a new instance of the chatBot class with a valid botName
|
// Create a new instance of the chatBot class with a valid botName
|
||||||
const botName = process.env.BOT_NAME; // Replace 'MyBot' with the desired bot name
|
const botName = process.env.BOT_NAME; // Replace 'MyBot' with the desired bot name
|
||||||
|
const linkupRoomId = process.env.LINKUP_ROOM_ID; // Use the environment variable for room ID
|
||||||
|
|
||||||
// Load commands from the 'commands' directory
|
// Load commands from the 'commands' directory
|
||||||
const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname);
|
const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname);
|
||||||
@ -41,29 +42,31 @@ loadCommands().then(commands => {
|
|||||||
bot.on('onMessage', (peer, message) => {
|
bot.on('onMessage', (peer, message) => {
|
||||||
console.log(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
|
// Handle all messages as potential AI commands
|
||||||
if (message.message.startsWith('!')) {
|
const userMessage = message.message;
|
||||||
// Extract the command and arguments
|
|
||||||
const [command, ...args] = message.message.slice(1).split(' ');
|
|
||||||
|
|
||||||
// Find the corresponding command handler
|
// Find the corresponding command handler (assuming the AI command handler is in 'commands/ai.js')
|
||||||
const commandHandler = commands[command.toLowerCase()];
|
const commandHandler = commands['ai'];
|
||||||
|
|
||||||
// If the command exists, execute its handler
|
// If the command exists, execute its handler
|
||||||
if (commandHandler && typeof commandHandler.handler === 'function') {
|
if (commandHandler && typeof commandHandler.handler === 'function') {
|
||||||
commandHandler.handler(bot, args, message);
|
commandHandler.handler(bot, [userMessage], message);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('onBotJoinRoom', () => {
|
bot.on('onBotJoinRoom', () => {
|
||||||
console.log("Bot is ready!");
|
console.log("Bot is ready!");
|
||||||
bot.sendTextMessage(process.env.ON_READY_MESSAGE);
|
// Include topic in the message sent when the bot joins the room
|
||||||
|
bot.sendTextMessage({
|
||||||
|
text: process.env.ON_READY_MESSAGE,
|
||||||
|
topic: linkupRoomId
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.joinChatRoom(process.env.LINKUP_ROOM_ID);
|
// Ensure the bot joins the chat room with the topic information
|
||||||
|
bot.joinChatRoom(linkupRoomId);
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error('Error loading commands:', error);
|
console.error('Error loading commands:', error);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user