LinkUp-P2P-Chat/chatBot/bot.js
2024-06-03 19:23:14 -04:00

33 lines
1.6 KiB
JavaScript

import chatBot from './includes/chatBot.js'; // Adjust the import path as necessary
// Generate a Buffer from the hexadecimal topic value
const topicHex = '86b91c2848f96ed9a982005709338a95a6bbee55057ab0f861f2c0ae979c3177';
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();
// Wait for 2 seconds for the bot to connect
setTimeout(() => {
// Send a message
bot.sendMessage('Hello, world!');
}, 2000); // Adjust the delay as necessary
// 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}`);
// 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]);
}
}