forked from snxraven/LinkUp-P2P-Chat
Up to date fork #1
@ -5,7 +5,6 @@ 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);
|
||||||
@ -42,31 +41,29 @@ 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.peerName}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
|
console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
|
||||||
|
|
||||||
// Handle all messages as potential AI commands
|
// Check if the message starts with a command prefix
|
||||||
const userMessage = message.message;
|
if (message.message.startsWith('!')) {
|
||||||
|
// Extract the command and arguments
|
||||||
|
const [command, ...args] = message.message.slice(1).split(' ');
|
||||||
|
|
||||||
// Find the corresponding command handler (assuming the AI command handler is in 'commands/ai.js')
|
// Find the corresponding command handler
|
||||||
const commandHandler = commands['ai'];
|
const commandHandler = commands[command.toLowerCase()];
|
||||||
|
|
||||||
// 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, [userMessage], message);
|
commandHandler.handler(bot, args, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('onBotJoinRoom', () => {
|
bot.on('onBotJoinRoom', () => {
|
||||||
console.log("Bot is ready!");
|
console.log("Bot is ready!");
|
||||||
// Include topic in the message sent when the bot joins the room
|
bot.sendTextMessage(process.env.ON_READY_MESSAGE);
|
||||||
bot.sendTextMessage({
|
|
||||||
text: process.env.ON_READY_MESSAGE,
|
|
||||||
topic: linkupRoomId
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Ensure the bot joins the chat room with the topic information
|
bot.joinChatRoom(process.env.LINKUP_ROOM_ID);
|
||||||
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