Add command handler for bot

This commit is contained in:
Raven Scott 2024-06-04 01:19:56 -04:00
parent 6463f2e526
commit c293e3b5e7
4 changed files with 83 additions and 19 deletions

View File

@ -1,32 +1,72 @@
import fs from 'fs';
import path from 'path';
import chatBot from './includes/chatBot.js'; // Adjust the import path as necessary import chatBot from './includes/chatBot.js'; // Adjust the import path as necessary
// Generate a Buffer from the hexadecimal topic value // Generate a Buffer from the hexadecimal topic value
const topicHex = '86b91c2848f96ed9a982005709338a95a6bbee55057ab0f861f2c0ae979c3177'; const topicHex = '669c83fa3c559f2c330f71a34e351a9134cecc6c255e4ef7689177bf0d7fd52a';
const topicBuffer = Buffer.from(topicHex, 'hex'); const topicBuffer = Buffer.from(topicHex, 'hex');
// 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 = 'MyBot'; // Replace 'MyBot' with the desired bot name const botName = 'MyBot'; // Replace 'MyBot' with the desired bot name
const bot = new chatBot(topicBuffer, botName, onMessageReceived);
// Join the chat room // Load commands from the 'commands' directory
bot.joinChatRoom(); const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname);
// Wait for 2 seconds for the bot to connect async function loadCommands() {
setTimeout(() => { const commands = {};
// Send a message
bot.sendMessage('Hello, world!');
}, 2000); // Adjust the delay as necessary
// Define the function to handle received messages // Read files from the commands directory
function onMessageReceived(peer, message) { const files = await fs.promises.readdir(commandsDir);
console.log(`Message received from ${message.name} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
// Check if the message is a ping command // Iterate through each file
if (message.message.toLowerCase() === '!ping') { for (const file of files) {
bot.sendMessage('Pong!'); // Check if the file is a JavaScript file
} else if (message.message.toLowerCase().startsWith('!8ball')) { if (file.endsWith('.js')) {
const responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', 'Don\'t count on it.', 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.']; const commandName = path.basename(file, '.js');
const randomIndex = Math.floor(Math.random() * responses.length); const commandPath = path.join(commandsDir, file);
bot.sendMessage(responses[randomIndex]);
// 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('Hello, world!');
}, 2000); // Adjust the delay as necessary
}).catch(error => {
console.error('Error loading commands:', error);
});

View File

@ -0,0 +1,9 @@
// 8ball.js
export default {
handler: function(bot, args, message) {
const responses = ['It is certain.', 'It is decidedly so.', 'Without a doubt.', 'Yes - definitely.', 'You may rely on it.', 'As I see it, yes.', 'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.', 'Reply hazy, try again.', 'Ask again later.', 'Better not tell you now.', 'Cannot predict now.', 'Concentrate and ask again.', 'Don\'t count on it.', 'My reply is no.', 'My sources say no.', 'Outlook not so good.', 'Very doubtful.'];
const randomIndex = Math.floor(Math.random() * responses.length);
bot.sendMessage(responses[randomIndex]);
}
};

View File

@ -0,0 +1,8 @@
export default {
name: 'hello',
description: 'Greet the user',
handler: function(bot, args, message) {
const userName = message.name;
bot.sendMessage(`Hello, ${userName}! How can I assist you today?`);
}
};

7
chatBot/commands/ping.js Normal file
View File

@ -0,0 +1,7 @@
// ping.js
export default {
handler: function(bot, args, message) {
bot.sendMessage('Pong!');
}
};