darkcord
Version:
A NodeJS Package to interact with Discord API
148 lines (147 loc) • 6.06 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManager = void 0;
const Application = __importStar(require("../resources/Application"));
const AuditLog = __importStar(require("../resources/AuditLog"));
const Base = __importStar(require("../resources/Base"));
const BitField = __importStar(require("../resources/BitField"));
const AnyChannel = __importStar(require("../resources/Channel"));
const EmojiOrReaction = __importStar(require("../resources/Emoji"));
const AnyGuild = __importStar(require("../resources/Guild"));
const Integration = __importStar(require("../resources/Integration"));
const AnyInteraction = __importStar(require("../resources/Interaction"));
const Invite = __importStar(require("../resources/Invite"));
const AnyMember = __importStar(require("../resources/Member"));
const Message = __importStar(require("../resources/Message"));
const Webhook = __importStar(require("../resources/Webhook"));
const index_1 = require("./index");
const node_path_1 = require("node:path");
class PluginManager {
client;
resources;
plugins;
constructor(client) {
this.client = client;
this.resources = [
{ baseFile: "Channel", exports: AnyChannel },
{ baseFile: "Guild", exports: AnyGuild },
{ baseFile: "Interaction", exports: AnyInteraction },
{ baseFile: "Invite", exports: Invite },
{ baseFile: "Emoji", exports: EmojiOrReaction },
{ baseFile: "Webhook", exports: Webhook },
{ baseFile: "Integration", exports: Integration },
{ baseFile: "Member", exports: AnyMember },
{ baseFile: "Message", exports: Message },
{ baseFile: "BitField", exports: BitField },
{ baseFile: "Base", exports: Base },
{ baseFile: "AuditLog", exports: AuditLog },
{ baseFile: "Application", exports: Application },
];
this.plugins = new Map();
}
addProperty(key, value) {
return (this.client[key] = value);
}
emit(event, ...args) {
this.client.emit(event, ...args);
}
getResourcePack(resourceName) {
return this.resources[resourceName];
}
getResourceInPack(resourceName) {
const pack = this.resources.find((resource) => Object.keys(resource.exports).includes(resourceName));
return pack && pack.exports[resourceName];
}
/**
* Extends a resource
* @param resourceName resource to be extended
* @param replacer replacer to extend resource
*/
extends(resourceName, replacer) {
const resource = this.resources.find((resource) => Object.keys(resource.exports).includes(resourceName));
if (resource) {
const modified = replacer(resource.exports[resourceName]);
const toBeModified = require(`../resources/${resource.baseFile}`)[resourceName];
for (const prop of Object.getOwnPropertyNames(modified.prototype)) {
Object.defineProperty(toBeModified.prototype, prop, Object.getOwnPropertyDescriptor(modified.prototype, prop));
}
// Static methods
for (const prop of Object.getOwnPropertyNames(modified).filter((prop) => !["prototype"].includes(prop))) {
Object.defineProperty(toBeModified, prop, Object.getOwnPropertyDescriptor(modified, prop));
}
}
}
/**
*
* @param resources resources names to be extended's
* @param replacer replacer to extends resources
*/
extendsMultiple(resources, replacer) {
for (const resource of resources) {
this.extends(resource, replacer);
}
}
/**
*
* @param path Path of file to be override
* @param replacer replacer function to replace file
*/
override(path, replacer) {
path = (0, node_path_1.join)("../", path);
const fullPath = require.resolve(path);
const modified = replacer(require(fullPath));
const original = require(fullPath);
for (const prop of Object.getOwnPropertyNames(modified)) {
original[prop] = modified[prop];
}
}
load(pluginFn) {
const plugin = pluginFn(this);
if (!plugin.name) {
throw (0, index_1.MakeError)({
name: "MissingPluginName",
message: "The plugin needs to have a name",
});
}
let err;
try {
plugin.load();
plugin.onStart();
}
catch (e) {
err = e instanceof Error ? e : new Error(e);
}
if (err) {
const PluginError = new Error(`Failed to load Plugin \x1b[37m${plugin.name}\x1b[0m: ${err.message}`);
PluginError.name = "PluginError";
PluginError.stack = err.stack;
PluginError.cause = err.cause;
throw PluginError;
}
this.plugins.set(plugin.name, plugin);
}
}
exports.PluginManager = PluginManager;