corde
Version:
A simple library for Discord bot tests
165 lines (122 loc) • 3.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true,
});
exports.runtime = void 0;
const tslib_1 = require("tslib");
const config_1 = require("./config");
const discord_js_1 = require("discord.js");
const cordeBot_1 = require("../core/cordeBot");
const errors_1 = require("../errors");
const path_1 = (0, tslib_1.__importDefault)(require("path"));
const utils_1 = require("../utils");
const consts_1 = require("../consts");
const Environment = {
isUnityTest: process.env.ENV === "UNITY_TEST",
isE2eTest: process.env.ENV === "E2E_TEST",
};
class Runtime {
constructor() {
this._configs = new config_1.Config();
}
get bot() {
if (!this._bot) {
this._bot = this.initBot();
}
return this._bot;
}
get isTestEnv() {
return this.environment.isE2eTest || this.environment.isUnityTest;
}
get environment() {
return Environment;
}
get exitOnFileReadingError() {
return this._configs.exitOnFileReadingError;
}
get extensions() {
return this._configs.extensions;
}
get events() {
return this.bot.events;
}
get configs() {
return this._configs;
}
get cordeBotToken() {
return this._configs.cordeBotToken;
}
get botTestId() {
return this._configs.botTestId;
}
get project() {
return this._configs.project;
}
get channelId() {
return this._configs.channelId;
}
get guildId() {
return this._configs.guildId;
}
get timeout() {
return this._configs.timeout;
}
get botPrefix() {
return this._configs.botPrefix;
}
get testMatches() {
return this._configs.testMatches;
}
get modulePathIgnorePatterns() {
return this._configs.modulePathIgnorePatterns;
}
static getInstance() {
if (!Runtime._instance) {
Runtime._instance = new Runtime();
}
return Runtime._instance;
}
setConfigs(_configs, forceUpdate) {
if (!_configs) {
throw new errors_1.ConfigError("Invalid _configs");
}
this._configs.setConfigs(_configs, forceUpdate);
}
replaceWithRootDir(text) {
if (this._configs.rootDir) {
return (0, utils_1.replaceAll)(text, consts_1.ROOT_DIR, this._configs.rootDir);
}
return text;
}
resolvePathWithRootDir(partialPath) {
if (this._configs.rootDir) {
return path_1.default.resolve(process.cwd(), this._configs.rootDir, partialPath);
}
return path_1.default.resolve(process.cwd(), partialPath);
}
isBotLoggedIn() {
return this.bot && this.bot.isLoggedIn();
}
logoffBot() {
if (this._bot) {
this._bot.logout();
}
}
loginBot(token) {
return this.bot.login(token);
}
injectBot(fn) {
return fn(this.bot);
}
initBot() {
return new cordeBot_1.CordeBot(
this._configs.botPrefix,
this._configs.guildId,
this._configs.channelId,
this._configs.botTestId,
new discord_js_1.Client(),
);
}
}
const runtime = Runtime.getInstance();
exports.runtime = runtime;