nestwhats
Version:
A whatsapp-web.js wrapper for NestJS to create WhatsApp bots
102 lines (101 loc) • 3.78 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var CommandsRegistryService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandsRegistryService = void 0;
const common_1 = require("@nestjs/common");
let CommandsRegistryService = CommandsRegistryService_1 = class CommandsRegistryService {
constructor() {
this.logger = new common_1.Logger(CommandsRegistryService_1.name);
this.cache = new Map();
this.prefixCache = new Map();
}
add(command) {
const customPrefix = command.getPrefix();
if (customPrefix !== undefined) {
this.addToPrefixCache(customPrefix, command);
}
else {
this.addToCache(command);
}
}
addToCache(command) {
const name = command.getName();
if (this.cache.has(name)) {
this.logger.warn(`Command : ${name} already exists`);
}
this.cache.set(name, command);
for (const alias of command.getAliases()) {
if (this.cache.has(alias)) {
this.logger.warn(`Command alias: ${alias} already exists`);
}
this.cache.set(alias, command);
}
}
addToPrefixCache(prefix, command) {
if (!this.prefixCache.has(prefix)) {
this.prefixCache.set(prefix, new Map());
}
const map = this.prefixCache.get(prefix);
const name = command.getName();
if (map.has(name)) {
this.logger.warn(`Command : ${name} with prefix "${prefix}" already exists`);
}
map.set(name, command);
for (const alias of command.getAliases()) {
if (map.has(alias)) {
this.logger.warn(`Command alias: ${alias} with prefix "${prefix}" already exists`);
}
map.set(alias, command);
}
}
getAll() {
const seen = new Set();
for (const command of this.cache.values())
seen.add(command);
for (const map of this.prefixCache.values())
for (const command of map.values())
seen.add(command);
return [...seen];
}
get(name) {
return this.cache.get(name);
}
getByPrefix(prefix, name) {
var _a;
return (_a = this.prefixCache.get(prefix)) === null || _a === void 0 ? void 0 : _a.get(name);
}
remove(name) {
const command = this.cache.get(name);
if (!command)
return;
this.cache.delete(name);
for (const alias of command.getAliases()) {
this.cache.delete(alias);
}
}
removeByPrefix(prefix, name) {
const map = this.prefixCache.get(prefix);
if (!map)
return;
const command = map.get(name);
if (!command)
return;
map.delete(name);
for (const alias of command.getAliases()) {
map.delete(alias);
}
if (map.size === 0) {
this.prefixCache.delete(prefix);
}
}
};
exports.CommandsRegistryService = CommandsRegistryService;
exports.CommandsRegistryService = CommandsRegistryService = CommandsRegistryService_1 = __decorate([
(0, common_1.Injectable)()
], CommandsRegistryService);