first commit

This commit is contained in:
Raven Scott
2024-06-03 19:23:14 -04:00
commit c58671dc7d
8 changed files with 595 additions and 0 deletions

32
chatBot/bot.js Normal file
View File

@ -0,0 +1,32 @@
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]);
}
}

View File

@ -0,0 +1,44 @@
import Hyperswarm from 'hyperswarm';
class chatBot {
constructor(topicBuffer, botName, onMessageReceived) {
this.topicBuffer = topicBuffer;
this.botName = botName;
this.swarm = new Hyperswarm();
this.onMessageReceived = onMessageReceived;
this.setupSwarm();
}
setupSwarm() {
this.swarm.on('connection', (peer) => {
peer.on('data', message => this.onMessageReceived(peer, JSON.parse(message.toString())));
peer.on('error', e => console.log(`Connection error: ${e}`));
});
this.swarm.on('update', () => {
console.log(`Peers count: ${this.swarm.connections.size}`);
});
}
joinChatRoom() {
this.discovery = this.swarm.join(this.topicBuffer, { client: true, server: true });
this.discovery.flushed().then(() => {
console.log(`Bot ${this.botName} joined the chat room.`);
});
}
sendMessage(message) {
console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections];
const data = JSON.stringify({ name: this.botName, message, timestamp }); // Include timestamp
for (const peer of peers) peer.write(data);
}
destroy() {
this.swarm.destroy();
console.log(`Bot ${this.botName} disconnected.`);
}
}
export default chatBot;