ts3-ladon
Version:
Ladon is a versatile framework designed for creating powerful TS3 (TeamSpeak 3) query bots. With Ladon, developers can effortlessly implement commands, handle events, and utilize a variety of utility functions to enhance their bot's capabilities. Whether
119 lines (118 loc) • 4.73 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionManager = void 0;
const logger_1 = require("../logger");
const ts3_nodejs_library_1 = require("ts3-nodejs-library");
const uid_1 = require("uid");
const command_manager_1 = require("../command-manager");
class ConnectionManager {
constructor() {
this._connections = new Map();
}
static get instance() {
if (!this._instance) {
this._instance = new ConnectionManager();
this._instance._connections = new Map();
}
return this._instance;
}
/**
* @description Spawns a new connection
* @param {Partial<TeamSpeak.ConnectionParams>} options options
* @returns {ExtendedConnection | null} connectionId
*/
spawn(options) {
return __awaiter(this, void 0, void 0, function* () {
const connectionId = (0, uid_1.uid)(32);
let connection = null;
try {
const { botColor, prefix } = options, rest = __rest(options, ["botColor", "prefix"]);
const tsInstance = new ts3_nodejs_library_1.TeamSpeak(rest);
connection = yield tsInstance.connect();
connection.connectionId = connectionId;
if (botColor) {
connection.botColor = botColor;
}
else {
connection.botColor = "#ff0000";
}
if (prefix) {
connection.botPrefix = prefix;
}
else {
connection.botPrefix = "!";
}
connection.on("textmessage", (textMessage) => {
if (textMessage.msg.startsWith(connection.botPrefix)) {
command_manager_1.Command.callCommand(connectionId, textMessage.invoker, textMessage.msg);
}
});
this._connections.set(connectionId, connection);
logger_1.Logger.instance.success("connection-manager", {
message: `Connection ${connectionId} spawned`,
});
}
catch (error) {
logger_1.Logger.instance.error("connection-manager", {
message: `Failed to spawn connection ${connectionId}`,
error: error,
});
}
return connection;
});
}
/**
* @description Destroys a connection
* @param {string} connectionId connectionId
*/
destroy(connectionId) {
const connection = this._connections.get(connectionId);
if (!connection) {
logger_1.Logger.instance.error("connection-manager", {
message: `Connection ${connectionId} not found`,
});
return;
}
connection.forceQuit();
this._connections.delete(connectionId);
logger_1.Logger.instance.success("connection-manager", {
message: `Connection ${connectionId} destroyed`,
});
}
/**
* @description Get a connection
* @param {string} connectionId connectionId
* @returns {TeamSpeak | undefined} connection
*/
get(connectionId) {
return this._connections.get(connectionId);
}
/**
* @description Get all connections
* @returns {Map<string, TeamSpeak>} connections
*/
getAll() {
return this._connections;
}
}
exports.ConnectionManager = ConnectionManager;
;