@evolvejs/core
Version:
An advanced Discord API wrapper with TS and JS support
211 lines (210 loc) • 7.13 kB
JavaScript
"use strict";
/* eslint-disable no-constant-condition */
/* eslint-disable no-mixed-spaces-and-tabs */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvolveBuilder = void 0;
const Oauth2_1 = require("../Oauth2/Oauth2");
const Constants_1 = require("../Utils/Constants");
const EvolveClient_1 = require("./EvolveClient");
const ShardManager_1 = require("./Websocket/ShardManager");
class EvolveBuilder {
constructor(token, useDefaultSettings = true) {
this.shards = 1;
this.intents = 0;
this.cache = new Set();
this.encoding = "json";
if (token) {
this.token = token;
}
if (useDefaultSettings) {
this.enableCache(Constants_1.CacheOptions.GUILD, Constants_1.CacheOptions.CHANNELS, Constants_1.CacheOptions.USERS);
this.enableIntents(Constants_1.GatewayIntents.GUILD +
Constants_1.GatewayIntents.GUILD_MESSAGES +
Constants_1.GatewayIntents.DIRECT_MESSAGES);
}
}
/**
*
* @param token
* @returns The EvolveBuilder Class
*/
setToken(token) {
this.token = token;
return this;
}
/**
*
* @param encoding
*/
setEncoding(encoding) {
this.encoding = encoding;
return this;
}
/**
*
* @param totalShards
* @note It must be greater than 0
* @returns The EvolveBuilder Class
*/
setShards(totalShards) {
this.shards = totalShards;
return this;
}
/**
*
* @param activity
* @note The input should be the same as given in the discord api docs
* @returns The EvolveBuilder Class
*/
setActivity(activity) {
this.activity = activity;
return this;
}
/**
*
* @param cache
* @enables The Cache Options for the library
* @returns The EvolveBuilder Client
*/
enableCache(...caches) {
for (const cache of caches) {
this.cache.add(cache);
}
return this;
}
/**
*
* @param cache
* @disables The Cache Options for the Library
* @returns EvolveBuilder Class
*/
disableCache(...caches) {
for (const cache of caches) {
this.cache.add(cache);
}
return this;
}
/**
*
* @param intents
* @enables The Required Intents for the Bot
* @returns EvolveBuilder Class
* @warning No intents are applied at default so you wont receive any events except some exceptions
*/
enableIntents(...intents) {
for (const intent of intents) {
this.intents = this.intents + intent;
}
return this;
}
/**
*
* @param intents
* @disables The Intents for your bot
* @returns EvolveBuilder Class
*/
disableIntents(...intents) {
for (const intent of intents) {
this.intents = this.intents - intent;
}
return this;
}
setSecret(clientSecret) {
this.secret = clientSecret;
return this;
}
setStructureClass(structure) {
this.structure = structure;
return this;
}
setClientClass(client) {
this.typeOfclient = client;
return this;
}
setCacheProviders(providers) {
this._providers = providers;
return this;
}
/**
* @param none
* @returns {EvolveClient} A Initialized EvolveClient Instance
*/
build() {
if (!this.typeOfclient) {
this.client = new EvolveClient_1.EvolveClient(this.token, {
enableGuildCache: this.cache.has(Constants_1.CacheOptions.GUILD)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableChannelCache: this.cache.has(Constants_1.CacheOptions.CHANNELS)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableEmojiCache: this.cache.has(Constants_1.CacheOptions.EMOJI)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableUsersCache: this.cache.has(Constants_1.CacheOptions.USERS)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableMessageCache: this.cache.has(Constants_1.CacheOptions.MESSAGES)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
});
}
else {
this.client = new this.typeOfclient(this.token, {
enableGuildCache: this.cache.has(Constants_1.CacheOptions.GUILD)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableChannelCache: this.cache.has(Constants_1.CacheOptions.CHANNELS)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableEmojiCache: this.cache.has(Constants_1.CacheOptions.EMOJI)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableUsersCache: this.cache.has(Constants_1.CacheOptions.USERS)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
enableMessageCache: this.cache.has(Constants_1.CacheOptions.MESSAGES)
? true
: this.cache.has(Constants_1.CacheOptions.ALL),
});
}
if (this._providers) {
if (this._providers.guilds) {
this.client.guilds = this._providers.guilds;
}
if (this._providers.channels) {
this.client.channels = this._providers.channels;
}
if (this._providers.emojis) {
this.client.emojis = this._providers.emojis;
}
if (this._providers.users) {
this.client.users = this._providers.users;
}
if (this._providers.messages) {
this.client.messages = this._providers.messages;
}
if (this._providers.roles) {
this.client.roles = this._providers.roles;
}
}
if (!this.token) {
throw this.client.transformer.error("EvolveBuilder#build Error.. -> No token Provided for EvolveClient to be initialized");
}
if (this.shards <= 0)
throw this.client.transformer.error("Total shards must be more than 0!");
if (this.intents == 0) {
throw this.client.transformer.error("No Intents are given, you will not get any events except some...");
}
if (this.secret) {
this.client.secret = this.secret;
this.client.oauth2 = new Oauth2_1.Oauth2(this.client);
}
if (this.structure)
this.client.structures = this.structure;
this.client.sharder = new ShardManager_1.ShardManager(this);
this.client.sharder.spawnAll();
return this.client;
}
}
exports.EvolveBuilder = EvolveBuilder;