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/
|
||||
package-lock.json
|
||||
node_modules
|
||||
node_modules
|
||||
docs
|
21
jsdoc.json
21
jsdoc.json
@ -14,25 +14,6 @@
|
||||
"encoding": "utf8",
|
||||
"destination": "docs/",
|
||||
"recurse": 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
"verbose": true
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@
|
||||
"version": "1.0.0",
|
||||
"main": "src/Client.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"generate-docs": "jsdoc --configure jsdoc.json"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
|
@ -13,9 +13,15 @@ import fs from 'fs';
|
||||
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 {
|
||||
/**
|
||||
* @param {String} botName The name of the bot.
|
||||
* @since 1.0
|
||||
* @constructor
|
||||
* @author snxraven
|
||||
*/
|
||||
constructor(botName) {
|
||||
super();
|
||||
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() {
|
||||
try {
|
||||
this.servePort = this.getRandomPort();
|
||||
@ -68,11 +79,22 @@ class Client extends EventEmitter {
|
||||
console.error('Error initializing ServeDrive:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a random port number.
|
||||
* @since 1.0
|
||||
* @author snxraven
|
||||
* @return {Number} Random port number.
|
||||
*/
|
||||
getRandomPort() {
|
||||
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) {
|
||||
try {
|
||||
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() {
|
||||
this.swarm.on('connection', (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}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Joins a specified chat room.
|
||||
* @since 1.0
|
||||
* @author snxraven
|
||||
* @param {String} chatRoomID Chat room topic string
|
||||
*/
|
||||
joinChatRoom(chatRoomID) {
|
||||
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
|
||||
@ -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) {
|
||||
console.log(`Preparing to send text message: ${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) {
|
||||
try {
|
||||
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) {
|
||||
try {
|
||||
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) {
|
||||
if (!(message instanceof 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() {
|
||||
await this.swarm.destroy();
|
||||
console.log(`Bot ${this.botName} disconnected.`);
|
||||
|
@ -1,4 +1,19 @@
|
||||
/**
|
||||
* @description Base class for all messages
|
||||
* @since 1.0
|
||||
* @author MiTask
|
||||
*/
|
||||
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) {
|
||||
this.type = messageType;
|
||||
this.peerName = peerName;
|
||||
@ -7,6 +22,11 @@ class Message {
|
||||
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() {
|
||||
return {
|
||||
type: this.type,
|
||||
|
@ -1,11 +1,31 @@
|
||||
import Message from "./Message.js";
|
||||
|
||||
/**
|
||||
* @description Class for text messages
|
||||
* @since 1.0
|
||||
* @author MiTask
|
||||
*/
|
||||
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) {
|
||||
super("message", peerName, peerAvatar, topic, timestamp);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.0
|
||||
* @author MiTask
|
||||
* @returns {String} JSON String with all of the information about the message
|
||||
*/
|
||||
toJsonString() {
|
||||
return JSON.stringify({
|
||||
...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) {
|
||||
return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user