Compare commits
8 Commits
dd8099649f
...
f045f5804d
Author | SHA1 | Date | |
---|---|---|---|
f045f5804d | |||
|
114875cddb | ||
|
e33244c1f1 | ||
|
0587f261b7 | ||
|
28be0822f6 | ||
9f514c31f9 | |||
|
83f75a4fc8 | ||
|
de00514397 |
@ -1,14 +1,10 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import chatBot from './includes/chatBot.js'; // Adjust the import path as necessary
|
import Client from './includes/client.js'; // Adjust the import path as necessary
|
||||||
import 'dotenv/config'
|
import 'dotenv/config';
|
||||||
|
|
||||||
// Generate a Buffer from the hexadecimal topic value
|
|
||||||
const topicHex = process.env.LINKUP_ROOM_ID;
|
|
||||||
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 = process.env.BOT_NAME;; // Replace 'MyBot' with the desired bot name
|
const botName = process.env.BOT_NAME; // Replace 'MyBot' with the desired bot name
|
||||||
|
|
||||||
// Load commands from the 'commands' directory
|
// Load commands from the 'commands' directory
|
||||||
const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname);
|
const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname);
|
||||||
@ -38,7 +34,11 @@ async function loadCommands() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadCommands().then(commands => {
|
loadCommands().then(commands => {
|
||||||
const onMessageReceived = (peer, message) => {
|
// Create the chatBot instance with the defined onMessageReceived function
|
||||||
|
const bot = new Client(process.env.LINKUP_ROOM_ID, botName);
|
||||||
|
|
||||||
|
// We use Event Emitter here to handle new messages
|
||||||
|
bot.on('onMessage', (peer, message) => {
|
||||||
if (message.type == "icon") return;
|
if (message.type == "icon") return;
|
||||||
console.log(message);
|
console.log(message);
|
||||||
|
|
||||||
@ -57,10 +57,7 @@ loadCommands().then(commands => {
|
|||||||
commandHandler.handler(bot, args, message);
|
commandHandler.handler(bot, args, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
|
|
||||||
// Create the chatBot instance with the defined onMessageReceived function
|
|
||||||
const bot = new chatBot(topicBuffer, botName, onMessageReceived);
|
|
||||||
bot.joinChatRoom();
|
bot.joinChatRoom();
|
||||||
|
|
||||||
// Wait for 2 seconds for the bot to connect
|
// Wait for 2 seconds for the bot to connect
|
||||||
|
@ -1,17 +1,18 @@
|
|||||||
import Hyperswarm from 'hyperswarm';
|
import Hyperswarm from 'hyperswarm';
|
||||||
|
import EventEmitter from 'node:events'
|
||||||
|
|
||||||
class chatBot {
|
class Client extends EventEmitter {
|
||||||
constructor(topicBuffer, botName, onMessageReceived) {
|
constructor(chatRoomID, botName) {
|
||||||
this.topicBuffer = topicBuffer;
|
super();
|
||||||
|
this.topicBuffer = Buffer.from(chatRoomID, 'hex');
|
||||||
this.botName = botName;
|
this.botName = botName;
|
||||||
this.swarm = new Hyperswarm();
|
this.swarm = new Hyperswarm();
|
||||||
this.onMessageReceived = onMessageReceived;
|
|
||||||
this.setupSwarm();
|
this.setupSwarm();
|
||||||
}
|
}
|
||||||
|
|
||||||
setupSwarm() {
|
setupSwarm() {
|
||||||
this.swarm.on('connection', (peer) => {
|
this.swarm.on('connection', (peer) => {
|
||||||
peer.on('data', message => this.onMessageReceived(peer, JSON.parse(message.toString())));
|
peer.on('data', message => this.emit("onMessage", peer, JSON.parse(message.toString())));
|
||||||
peer.on('error', e => console.log(`Connection error: ${e}`));
|
peer.on('error', e => console.log(`Connection error: ${e}`));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -41,4 +42,4 @@ class chatBot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default chatBot;
|
export default Client;
|
@ -22,6 +22,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"b4a": "^1.6.6",
|
"b4a": "^1.6.6",
|
||||||
"corestore": "^6.18.2",
|
"corestore": "^6.18.2",
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
"electron": "^30.0.8",
|
"electron": "^30.0.8",
|
||||||
"hypercore-crypto": "^3.4.1",
|
"hypercore-crypto": "^3.4.1",
|
||||||
"hyperdrive": "^11.8.1",
|
"hyperdrive": "^11.8.1",
|
||||||
|
Loading…
Reference in New Issue
Block a user