Added lots of comments for docs generation
This commit is contained in:
parent
0259ce1605
commit
ba2347966c
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.idea/
|
.idea/
|
||||||
package-lock.json
|
package-lock.json
|
||||||
node_modules
|
node_modules
|
||||||
|
docs
|
21
jsdoc.json
21
jsdoc.json
@ -14,25 +14,6 @@
|
|||||||
"encoding": "utf8",
|
"encoding": "utf8",
|
||||||
"destination": "docs/",
|
"destination": "docs/",
|
||||||
"recurse": true,
|
"recurse": true,
|
||||||
"verbose": true,
|
"verbose": true
|
||||||
"template": "better-docs"
|
|
||||||
},
|
|
||||||
"templates": {
|
|
||||||
"cleverLinks": false,
|
|
||||||
"monospaceLinks": false,
|
|
||||||
"search": true,
|
|
||||||
"better-docs": {
|
|
||||||
"name": "Sample Documentation",
|
|
||||||
"title": "Test", // HTML title
|
|
||||||
"css": "style.css",
|
|
||||||
"trackingCode": "tracking-code-which-will-go-to-the-HEAD",
|
|
||||||
"hideGenerator": false,
|
|
||||||
"navLinks": [
|
|
||||||
{
|
|
||||||
"label": "Git",
|
|
||||||
"href": "https://git.ssh.surf/MiTask/LinkUp-Bot-Lib"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,7 +3,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "src/Client.js",
|
"main": "src/Client.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"generate-docs": "jsdoc --configure jsdoc.json"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
|
@ -13,9 +13,15 @@ import fs from 'fs';
|
|||||||
import ServeDrive from 'serve-drive';
|
import ServeDrive from 'serve-drive';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Raven
|
* This class is the core component of the bot system. It handles connections to the Hyperswarm network, manages message sending and receiving, and emits events for various actions.
|
||||||
*/
|
*/
|
||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
|
/**
|
||||||
|
* @param {String} botName The name of the bot.
|
||||||
|
* @since 1.0
|
||||||
|
* @constructor
|
||||||
|
* @author snxraven
|
||||||
|
*/
|
||||||
constructor(botName) {
|
constructor(botName) {
|
||||||
super();
|
super();
|
||||||
if (!botName) return console.error("Bot Name is not defined!");
|
if (!botName) return console.error("Bot Name is not defined!");
|
||||||
@ -55,6 +61,11 @@ class Client extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Initializes the ServeDrive for serving files and audio.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
*/
|
||||||
async initializeServeDrive() {
|
async initializeServeDrive() {
|
||||||
try {
|
try {
|
||||||
this.servePort = this.getRandomPort();
|
this.servePort = this.getRandomPort();
|
||||||
@ -68,11 +79,22 @@ class Client extends EventEmitter {
|
|||||||
console.error('Error initializing ServeDrive:', error);
|
console.error('Error initializing ServeDrive:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @description Returns a random port number.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
* @return {Number} Random port number.
|
||||||
|
*/
|
||||||
getRandomPort() {
|
getRandomPort() {
|
||||||
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Fetches and sets the bot's avatar from a local file.
|
||||||
|
* @param {String} filePath path to the local avatar file.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
*/
|
||||||
async fetchAvatar(filePath) {
|
async fetchAvatar(filePath) {
|
||||||
try {
|
try {
|
||||||
await this.drive.ready();
|
await this.drive.ready();
|
||||||
@ -87,6 +109,11 @@ class Client extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Sets up the Hyperswarm network and connection handlers.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
*/
|
||||||
setupSwarm() {
|
setupSwarm() {
|
||||||
this.swarm.on('connection', (peer) => {
|
this.swarm.on('connection', (peer) => {
|
||||||
// Send the cached icon message to the new peer
|
// Send the cached icon message to the new peer
|
||||||
@ -132,10 +159,16 @@ class Client extends EventEmitter {
|
|||||||
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
|
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @description Joins a specified chat room.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
* @param {String} chatRoomID Chat room topic string
|
||||||
|
*/
|
||||||
joinChatRoom(chatRoomID) {
|
joinChatRoom(chatRoomID) {
|
||||||
if (!chatRoomID || typeof chatRoomID !== 'string') {
|
if (!chatRoomID || typeof chatRoomID !== 'string') {
|
||||||
return console.error("Invalid chat room ID!");
|
console.error("Invalid chat room ID!");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms
|
this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms
|
||||||
@ -147,11 +180,24 @@ class Client extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Sends a text message.
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
* @param {String} message Text message to send to the bot's current chat room.
|
||||||
|
*/
|
||||||
sendTextMessage(message) {
|
sendTextMessage(message) {
|
||||||
console.log(`Preparing to send text message: ${message}`);
|
console.log(`Preparing to send text message: ${message}`);
|
||||||
this.sendMessage(TextMessage.new(this, message));
|
this.sendMessage(TextMessage.new(this, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Sends a file message.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
* @param {String} filePath Path to the file to send.
|
||||||
|
* @param {String} fileType Type of the file to send.
|
||||||
|
*/
|
||||||
async sendFileMessage(filePath, fileType) {
|
async sendFileMessage(filePath, fileType) {
|
||||||
try {
|
try {
|
||||||
await this.drive.ready();
|
await this.drive.ready();
|
||||||
@ -166,6 +212,13 @@ class Client extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Sends an audio message.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
* @param {String} filePath Path to the audio file to send.
|
||||||
|
* @param {String} audioType Type of the audio file to send.
|
||||||
|
*/
|
||||||
async sendAudioMessage(filePath, audioType) {
|
async sendAudioMessage(filePath, audioType) {
|
||||||
try {
|
try {
|
||||||
await this.drive.ready();
|
await this.drive.ready();
|
||||||
@ -180,6 +233,13 @@ class Client extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Sends a generic message.
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
* @param {Message} message Message class (TextMessage, FileMessage or AudioMessage)
|
||||||
|
*/
|
||||||
sendMessage(message) {
|
sendMessage(message) {
|
||||||
if (!(message instanceof Message)) {
|
if (!(message instanceof Message)) {
|
||||||
console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message);
|
console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message);
|
||||||
@ -205,6 +265,11 @@ class Client extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Disconnects the bot and shuts down the Hyperswarm network.
|
||||||
|
* @since 1.0
|
||||||
|
* @author snxraven
|
||||||
|
*/
|
||||||
async destroy() {
|
async destroy() {
|
||||||
await this.swarm.destroy();
|
await this.swarm.destroy();
|
||||||
console.log(`Bot ${this.botName} disconnected.`);
|
console.log(`Bot ${this.botName} disconnected.`);
|
||||||
|
@ -1,4 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* @description Base class for all messages
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
*/
|
||||||
class Message {
|
class Message {
|
||||||
|
/**
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
* @constructor
|
||||||
|
* @param {String} messageType Type of the message (text, file, audio, icon)
|
||||||
|
* @param {String} peerName Peer username
|
||||||
|
* @param {String} peerAvatar Peer avatar URL
|
||||||
|
* @param {String} topic Chat room topic string
|
||||||
|
* @param {Number} timestamp UNIX Timestamp
|
||||||
|
*/
|
||||||
constructor(messageType, peerName, peerAvatar, topic, timestamp) {
|
constructor(messageType, peerName, peerAvatar, topic, timestamp) {
|
||||||
this.type = messageType;
|
this.type = messageType;
|
||||||
this.peerName = peerName;
|
this.peerName = peerName;
|
||||||
@ -7,6 +22,11 @@ class Message {
|
|||||||
this.timestamp = timestamp;
|
this.timestamp = timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
* @returns {{name: String, topic: String, avatar: String, type: String, timestamp: Number}} JSON Object with all of the information about the message
|
||||||
|
*/
|
||||||
toJson() {
|
toJson() {
|
||||||
return {
|
return {
|
||||||
type: this.type,
|
type: this.type,
|
||||||
|
@ -1,11 +1,31 @@
|
|||||||
import Message from "./Message.js";
|
import Message from "./Message.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Class for text messages
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
*/
|
||||||
class TextMessage extends Message {
|
class TextMessage extends Message {
|
||||||
|
/**
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
* @constructor
|
||||||
|
* @param {String} peerName Peer username
|
||||||
|
* @param {String} peerAvatar Peer avatar URL
|
||||||
|
* @param {String} topic Chat room topic string
|
||||||
|
* @param {Number} timestamp UNIX Timestamp
|
||||||
|
* @param {String} message Text of the message
|
||||||
|
*/
|
||||||
constructor(peerName, peerAvatar, topic, timestamp, message) {
|
constructor(peerName, peerAvatar, topic, timestamp, message) {
|
||||||
super("message", peerName, peerAvatar, topic, timestamp);
|
super("message", peerName, peerAvatar, topic, timestamp);
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
* @returns {String} JSON String with all of the information about the message
|
||||||
|
*/
|
||||||
toJsonString() {
|
toJsonString() {
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
...this.toJson(),
|
...this.toJson(),
|
||||||
@ -13,6 +33,13 @@ class TextMessage extends Message {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 1.0
|
||||||
|
* @author MiTask
|
||||||
|
* @param {Client} bot Bot Client class
|
||||||
|
* @param {String} message Text of the message
|
||||||
|
* @returns {TextMessage} TextMessage class
|
||||||
|
*/
|
||||||
static new(bot, message) {
|
static new(bot, message) {
|
||||||
return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message);
|
return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user