From c293e3b5e711468adc69f560afead52617652ef2 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Tue, 4 Jun 2024 01:19:56 -0400 Subject: [PATCH] Add command handler for bot --- chatBot/bot.js | 78 +++++++++++++++++++++++++++++---------- chatBot/commands/8ball.js | 9 +++++ chatBot/commands/hello.js | 8 ++++ chatBot/commands/ping.js | 7 ++++ 4 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 chatBot/commands/8ball.js create mode 100644 chatBot/commands/hello.js create mode 100644 chatBot/commands/ping.js diff --git a/chatBot/bot.js b/chatBot/bot.js index 2465ad8..a3a4b96 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -1,32 +1,72 @@ +import fs from 'fs'; +import path from 'path'; import chatBot from './includes/chatBot.js'; // Adjust the import path as necessary // Generate a Buffer from the hexadecimal topic value -const topicHex = '86b91c2848f96ed9a982005709338a95a6bbee55057ab0f861f2c0ae979c3177'; +const topicHex = '669c83fa3c559f2c330f71a34e351a9134cecc6c255e4ef7689177bf0d7fd52a'; const topicBuffer = Buffer.from(topicHex, 'hex'); // Create a new instance of the chatBot class with a valid botName const botName = 'MyBot'; // Replace 'MyBot' with the desired bot name -const bot = new chatBot(topicBuffer, botName, onMessageReceived); -// Join the chat room -bot.joinChatRoom(); +// Load commands from the 'commands' directory +const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname); -// Wait for 2 seconds for the bot to connect -setTimeout(() => { - // Send a message - bot.sendMessage('Hello, world!'); -}, 2000); // Adjust the delay as necessary +async function loadCommands() { + const commands = {}; -// Define the function to handle received messages -function onMessageReceived(peer, message) { - console.log(`Message received from ${message.name} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); + // Read files from the commands directory + const files = await fs.promises.readdir(commandsDir); - // Check if the message is a ping command - if (message.message.toLowerCase() === '!ping') { - bot.sendMessage('Pong!'); - } else if (message.message.toLowerCase().startsWith('!8ball')) { - 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]); + // 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('Hello, world!'); + }, 2000); // Adjust the delay as necessary +}).catch(error => { + console.error('Error loading commands:', error); +}); diff --git a/chatBot/commands/8ball.js b/chatBot/commands/8ball.js new file mode 100644 index 0000000..d9cb238 --- /dev/null +++ b/chatBot/commands/8ball.js @@ -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]); + } + }; \ No newline at end of file diff --git a/chatBot/commands/hello.js b/chatBot/commands/hello.js new file mode 100644 index 0000000..8bf16c2 --- /dev/null +++ b/chatBot/commands/hello.js @@ -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?`); + } + }; \ No newline at end of file diff --git a/chatBot/commands/ping.js b/chatBot/commands/ping.js new file mode 100644 index 0000000..df71415 --- /dev/null +++ b/chatBot/commands/ping.js @@ -0,0 +1,7 @@ +// ping.js + +export default { + handler: function(bot, args, message) { + bot.sendMessage('Pong!'); + } + }; \ No newline at end of file