74 lines
2.4 KiB
JavaScript
74 lines
2.4 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import chatBot from './includes/chatBot.js'; // Adjust the import path as necessary
|
|
import 'dotenv/config'
|
|
|
|
// Generate a Buffer from the hexadecimal topic value
|
|
const topicHex = process.env.LINKUP_ROOM_ID;
|
|
const topicBuffer = Buffer.from(topicHex, 'hex');
|
|
|
|
// 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
|
|
|
|
// Load commands from the 'commands' directory
|
|
const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname);
|
|
|
|
async function loadCommands() {
|
|
const commands = {};
|
|
|
|
// Read files from the commands directory
|
|
const files = await fs.promises.readdir(commandsDir);
|
|
|
|
// Iterate through each file
|
|
for (const file of files) {
|
|
// Check if the file is a JavaScript file
|
|
if (file.endsWith('.js')) {
|
|
const commandName = path.basename(file, '.js');
|
|
const commandPath = path.join(commandsDir, file);
|
|
|
|
// Dynamically import the command module
|
|
const { default: commandModule } = await import(commandPath);
|
|
|
|
// Add the command module to the commands object
|
|
commands[commandName] = commandModule;
|
|
}
|
|
}
|
|
|
|
return commands;
|
|
}
|
|
|
|
loadCommands().then(commands => {
|
|
const onMessageReceived = (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}`);
|
|
|
|
// Check if the message starts with a command prefix
|
|
if (message.message.startsWith('!')) {
|
|
// Extract the command and arguments
|
|
const [command, ...args] = message.message.slice(1).split(' ');
|
|
|
|
// Find the corresponding command handler
|
|
const commandHandler = commands[command.toLowerCase()];
|
|
|
|
// If the command exists, execute its handler
|
|
if (commandHandler && typeof commandHandler.handler === 'function') {
|
|
commandHandler.handler(bot, args, message);
|
|
}
|
|
}
|
|
};
|
|
|
|
// Create the chatBot instance with the defined onMessageReceived function
|
|
const bot = new chatBot(topicBuffer, botName, onMessageReceived);
|
|
bot.joinChatRoom();
|
|
|
|
// Wait for 2 seconds for the bot to connect
|
|
setTimeout(() => {
|
|
// Send a message
|
|
bot.sendMessage(process.env.ON_READY_MESSAGE);
|
|
}, 2000); // Adjust the delay as necessary
|
|
}).catch(error => {
|
|
console.error('Error loading commands:', error);
|
|
});
|