Compare commits
6 Commits
9461368c02
...
cc654112b2
Author | SHA1 | Date | |
---|---|---|---|
cc654112b2 | |||
d65b76cbc5 | |||
|
9f2353c902 | ||
|
cd5cfb20a4 | ||
|
164efc5b72 | ||
|
b244aa9bdf |
@ -57,14 +57,14 @@ loadCommands().then(commands => {
|
||||
commandHandler.handler(bot, args, message);
|
||||
}
|
||||
}
|
||||
})
|
||||
bot.joinChatRoom();
|
||||
});
|
||||
|
||||
// Wait for 2 seconds for the bot to connect
|
||||
setTimeout(() => {
|
||||
// Send a message
|
||||
bot.on('onBotJoinRoom', () => {
|
||||
console.log("Bot is ready!");
|
||||
bot.sendMessage(process.env.ON_READY_MESSAGE);
|
||||
}, 2000); // Adjust the delay as necessary
|
||||
});
|
||||
|
||||
bot.joinChatRoom();
|
||||
}).catch(error => {
|
||||
console.error('Error loading commands:', error);
|
||||
});
|
||||
|
@ -4,6 +4,9 @@ import EventEmitter from 'node:events'
|
||||
class Client extends EventEmitter {
|
||||
constructor(chatRoomID, botName) {
|
||||
super();
|
||||
|
||||
if(!chatRoomID || !botName) return console.error("ChatRoomID or BotName is not defined!");
|
||||
|
||||
this.topicBuffer = Buffer.from(chatRoomID, 'hex');
|
||||
this.botName = botName;
|
||||
this.swarm = new Hyperswarm();
|
||||
@ -12,8 +15,11 @@ class Client extends EventEmitter {
|
||||
|
||||
setupSwarm() {
|
||||
this.swarm.on('connection', (peer) => {
|
||||
peer.on('data', message => this.emit("onMessage", peer, JSON.parse(message.toString())));
|
||||
peer.on('error', e => console.log(`Connection error: ${e}`));
|
||||
peer.on('data', message => this.emit('onMessage', peer, JSON.parse(message.toString())));
|
||||
peer.on('error', e => {
|
||||
this.emit('onError', e);
|
||||
console.error(`Connection error: ${e}`)
|
||||
});
|
||||
});
|
||||
|
||||
this.swarm.on('update', () => {
|
||||
@ -22,9 +28,10 @@ class Client extends EventEmitter {
|
||||
}
|
||||
|
||||
joinChatRoom() {
|
||||
this.discovery = this.swarm.join(this.topicBuffer, { client: true, server: true });
|
||||
this.discovery = this.swarm.join(this.topicBuffer, {client: true, server: true});
|
||||
this.discovery.flushed().then(() => {
|
||||
console.log(`Bot ${this.botName} joined the chat room.`);
|
||||
this.emit('onBotJoinRoom');
|
||||
});
|
||||
}
|
||||
|
||||
@ -32,7 +39,7 @@ class Client extends EventEmitter {
|
||||
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
|
||||
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp
|
||||
for (const peer of peers) peer.write(data);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user