{"size": 2838, "ext": "js", "max_stars_repo_path": "src/Client/ClientBase.js", "max_stars_repo_name": "chrissie1234/bloxy", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/Client/ClientBase.js", "max_issues_repo_name": "chrissie1234/bloxy", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/Client/ClientBase.js", "max_forks_repo_name": "chrissie1234/bloxy", "max_forks_repo_forks_event_min_datetime": "2021-07-18T16:44:22.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-18T16:44:22.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 26.523364486, "max_line_length": 109, "alphanum_fraction": 0.6899224806}
const EventEmitter = require("events");
const misc = require("../misc");
const structures = require("../structures");
const path = require("path");
const ChatManager = require("./src/Chat");
const request = require("request");

const DefaultOptions = {
	setup: {},
	callbacks: {},
	credentials: {}
};

module.exports = class ClientBase extends EventEmitter {
	constructor (options = {}) {
		super();

		this.util = misc.util;
		this._timeouts = new Set();
		this._intervals = new Set();
		this.options = this._setupOptions(options);
		this.errors = misc.errors;
		this.structures = structures;

		this.src = require("./src");
		this.constants = this.src.constants;
		this.token = new misc.managers.token(this);
		this.cache = new misc.managers.cache(this);
		this.request = new misc.managers.request(this);
		this.captcha = new misc.managers.captcha(this);
		this.chat = new ChatManager({ client: this });

		this._setupMethods();
		if (!this.options.jar) this.options.jar = request.jar();
	}

	_setupMethods () {
		require("./methods")({ client: this });
	}

	_setupOptions (options) {
		return Object.assign(JSON.parse(JSON.stringify(DefaultOptions)), options);
	}

	setupMethod (filePath, name) {
		filePath = path.join(__dirname, filePath, name);
		let file;
		try {
			file = require(filePath);
		} catch (error) {
			throw new Error(this.errors.methodRegister.failedMethodSetup(filePath, error));
		}
		if (!file || !file.conf) throw new Error(this.errors.methodRegister.noConf(filePath));
		const tmpStore = this.src.prepareFunctions.prepare.byConfigs({ client: this }, file.conf);
		if (!tmpStore || !tmpStore.register) throw new Error(this.errors.methodRegister.noFunctionStore());
		return tmpStore.register[name];
	}

	destroy () {
		for (const t of this._timeouts) this.clearTimeout(t);
		for (const i of this._intervals) this.clearInterval(i);
		this._timeouts.clear();
		this._intervals.clear();
	}

	setTimeout (fn, delay, ...args) {
		const timeout = setTimeout(() => {
			fn(...args);
			this._timeouts.delete(timeout);
		}, delay);
		this._timeouts.add(timeout);
		return timeout;
	}

	clearTimeout (timeout) {
		clearTimeout(timeout);
		this._timeouts.delete(timeout);
	}

	setInterval (fn, delay, ...args) {
		const interval = setInterval(fn, delay, ...args);
		this._intervals.add(interval);
		return interval;
	}

	clearInterval (interval) {
		clearInterval(interval);
		this._intervals.delete(interval);
	}

	loggedIn () {
		return this.user !== null;
	}

	extend (extendedPath, under) {
		if (this[under]) throw new Error(this.errors.methodRegister.alreadyUnder(under));

		this[under] = {};

		const preparedMethods = this.src.prepareFunctions.prepare.getConfigsByPath({ client: this }, extendedPath);

		for (let [key, value] of Object.entries(preparedMethods.register)) {
			this[under][key] = value;
		}
	}
};
