UNPKG

gbfcommands

Version:
336 lines (335 loc) 18.8 kB
"use strict"; 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GBF = exports.ColorCodes = exports.Emojis = exports.BuiltInCommands = exports.BuiltInEvents = void 0; const discord_js_1 = require("discord.js"); const chalk_1 = require("chalk"); const Engine_1 = require("../Utils/Engine"); const Command_Registry_1 = require("./Command Handlers/Command Registry"); const path_1 = __importDefault(require("path")); const mongoose_1 = require("mongoose"); const Slash_Handler_1 = require("./Command Handlers/Slash Handler"); const Utils_1 = require("../Utils/Utils"); const Context_Handler_1 = require("./Command Handlers/Context Handler"); const GBF_Errors_1 = require("../Utils/GBF Errors"); var BuiltInEvents; (function (BuiltInEvents) { BuiltInEvents["Ready"] = "GBFReady"; })(BuiltInEvents || (exports.BuiltInEvents = BuiltInEvents = {})); var BuiltInCommands; (function (BuiltInCommands) { BuiltInCommands["All"] = "AllCommands"; BuiltInCommands["Set Presence"] = "set_presence"; BuiltInCommands["Bot Ban"] = "bot_ban"; BuiltInCommands["Uptime"] = "uptime"; BuiltInCommands["Ping"] = "ping"; })(BuiltInCommands || (exports.BuiltInCommands = BuiltInCommands = {})); class Emojis { } exports.Emojis = Emojis; Emojis.Verify = "✅"; Emojis.Error = "❌"; Emojis.GBFLogo = "<:LogoTransparent:838994085527945266>"; class ColorCodes { } exports.ColorCodes = ColorCodes; ColorCodes.Default = "#e91e63"; ColorCodes.ErrorRed = "#FF0000"; ColorCodes.SuccessGreen = "#33a532"; ColorCodes.SalmonPink = "#ff91a4"; ColorCodes.CardinalRed = "#C41E3A"; ColorCodes.Cherry = "#D2042D"; ColorCodes.PastelRed = "#FAA0A0"; ColorCodes.Cyan = "#00FFFF"; class GBF extends discord_js_1.Client { constructor(HandlerOptions) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t; super(HandlerOptions); this.LogActionsMessage = ""; this.HandlerEvents = new discord_js_1.Collection(); this.Aliases = new discord_js_1.Collection(); this.MessageCommands = new discord_js_1.Collection(); this.SlashCommands = new discord_js_1.Collection(); this.ContextCommands = new discord_js_1.Collection(); const ConfigPath = HandlerOptions.BotConfig; const Config = Engine_1.Engine.LoadConfig(ConfigPath); this.BotConfig = Config; if (HandlerOptions.AutoLogin) this.login(); this.intents = ((_a = HandlerOptions.intents) === null || _a === void 0 ? void 0 : _a.length) || [discord_js_1.GatewayIntentBits.Guilds]; this.EventsFolder = HandlerOptions.EventsFolder; this.CommandsFolder = HandlerOptions.CommandsFolder; this.IgnoredEvents = (_b = HandlerOptions.IgnoredEvents) !== null && _b !== void 0 ? _b : []; this.Prefix = (_c = HandlerOptions.Prefix) !== null && _c !== void 0 ? _c : "!!"; this.Prefixes = (_d = HandlerOptions.Prefixes) !== null && _d !== void 0 ? _d : [this.Prefix]; this.HandlerVersion = "5.0.0"; this.LogActions = HandlerOptions.LogActions; this.DMEnabled = (_e = HandlerOptions.DMEnabled) !== null && _e !== void 0 ? _e : false; this.TestServers = (_f = HandlerOptions.TestServers) !== null && _f !== void 0 ? _f : []; this.Developers = (_g = HandlerOptions.Developers) !== null && _g !== void 0 ? _g : []; this.SupportServer = (_h = HandlerOptions.SupportServer) !== null && _h !== void 0 ? _h : "No Support Server."; this.AppealURL = (_j = HandlerOptions.AppealURL) !== null && _j !== void 0 ? _j : undefined; this.Version = (_k = HandlerOptions.Version) !== null && _k !== void 0 ? _k : "1.0.0"; this.LogsChannel = (_l = HandlerOptions.LogsChannel) !== null && _l !== void 0 ? _l : []; this.DisabledHandlerEvents = (_m = HandlerOptions.DisabledHandlerEvents) !== null && _m !== void 0 ? _m : []; this.DisabledHandlerCommands = (_o = HandlerOptions.DisabledHandlerCommands) !== null && _o !== void 0 ? _o : []; this.DisabledCommands = (_p = HandlerOptions.DisabledCommands) !== null && _p !== void 0 ? _p : []; this.DatabaseInteractions = (_q = HandlerOptions.DatabaseInteractions) !== null && _q !== void 0 ? _q : true; this.BuiltInHandlerCommands = [ BuiltInCommands["Bot Ban"], BuiltInCommands.Ping, BuiltInCommands["Set Presence"], BuiltInCommands.Uptime, ]; this.DefaultColor = (_r = HandlerOptions.DefaultColor) !== null && _r !== void 0 ? _r : ColorCodes.Default; ColorCodes.Default = this.DefaultColor; this.VerifyEmoji = (_s = HandlerOptions.VerifyEmoji) !== null && _s !== void 0 ? _s : Emojis.Verify; this.ErrorEmoji = (_t = HandlerOptions.ErrorEmoji) !== null && _t !== void 0 ? _t : Emojis.Error; if (this.DisabledCommands.length) this.DisabledCommands = this.DisabledCommands.map((Command) => Command.toLowerCase()); if (this.AppealURL && !(0, Utils_1.IsValidURL)(this.AppealURL)) { process.emitWarning((0, chalk_1.yellowBright)(`• Warning: The Appeal URL provided is not a valid URL.`), { code: "InvalidURL", detail: "Our validation system has decided that the Appeal URL you provided in the handler options is not a valid URL and will be set to undefined.", }); this.AppealURL = undefined; } this.LogActionsMessage += (0, chalk_1.greenBright)(`• GBF Handler v${this.HandlerVersion} is now online!`); } async LoadCommands(CommandsFolder, HandlerCommands = false) { var _a; try { if (!((_a = this.application) === null || _a === void 0 ? void 0 : _a.owner)) await this.application.fetch(); await (0, Command_Registry_1.RegisterCommands)(this, CommandsFolder); if (this.MessageCommands.size >= 1 && !HandlerCommands) this.LogActionsMessage += (0, chalk_1.blueBright)(`\n• Registered ${this.MessageCommands.size.toLocaleString("en-US")} Message Command${this.MessageCommands.size > 1 ? "s." : "."}`); const FilteredGuildSlashCommands = this.SlashCommands.filter((Command) => { return (!Command.CommandOptions.IgnoreCommand && Command.CommandOptions.development && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase()) && (this.DisabledHandlerCommands.includes(BuiltInCommands.All) ? !this.BuiltInHandlerCommands.includes(Command.CommandOptions.name) : !this.DisabledHandlerCommands.includes(Command.CommandOptions.name)) && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase())); }); const FilteredGuildContextCommands = this.ContextCommands.filter((Command) => { return (!Command.CommandOptions.IgnoreCommand && Command.CommandOptions.development && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase()) && (this.DisabledHandlerCommands.includes(BuiltInCommands.All) ? !this.BuiltInHandlerCommands.includes(Command.CommandOptions.name) : !this.DisabledHandlerCommands.includes(Command.CommandOptions.name)) && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase())); }); const FilteredGlobalSlashCommands = this.SlashCommands.filter((Command) => { return (!Command.CommandOptions.IgnoreCommand && !Command.CommandOptions.development && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase()) && (this.DisabledHandlerCommands.includes(BuiltInCommands.All) ? !this.BuiltInHandlerCommands.includes(Command.CommandOptions.name) : !this.DisabledHandlerCommands.includes(Command.CommandOptions.name)) && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase())); }); const FilteredGlobalContextCommands = this.ContextCommands.filter((Command) => { return (!Command.CommandOptions.IgnoreCommand && !Command.CommandOptions.development && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase()) && (this.DisabledHandlerCommands.includes(BuiltInCommands.All) ? !this.BuiltInHandlerCommands.includes(Command.CommandOptions.name) : !this.DisabledHandlerCommands.includes(Command.CommandOptions.name)) && !this.DisabledCommands.includes(Command.CommandOptions.name.toLowerCase())); }); const GuildCommands = [ ...this.ToApplicationCommand(FilteredGuildSlashCommands), ...this.ToApplicationCommand(FilteredGuildContextCommands), ]; const GlobalCommands = [ ...this.ToApplicationCommand(FilteredGlobalSlashCommands), ...this.ToApplicationCommand(FilteredGlobalContextCommands), ]; const rest = new discord_js_1.REST().setToken(this.BotConfig.TOKEN); if (GuildCommands && GuildCommands.length && this.TestServers.length > 0) { await Promise.all(this.TestServers.map(async (ServerID) => { const TestServer = await this.guilds.fetch(ServerID); if (TestServer && TestServer instanceof discord_js_1.Guild) { const GuildID = TestServer.id; await rest .put(discord_js_1.Routes.applicationGuildCommands(this.user.id, GuildID), { body: GuildCommands, }) .then(() => { if (!HandlerCommands) this.LogActionsMessage += (0, chalk_1.magentaBright)(`\n• Registering Guild Only Commands in: ${TestServer.name}`); else this.LogActionsMessage += (0, chalk_1.magentaBright)(`\n• Registering Guild Only Handler Commands in: ${TestServer.name}`); }); } })); } if (GlobalCommands && GlobalCommands.length) { await rest.put(discord_js_1.Routes.applicationCommands(this.user.id), { body: GlobalCommands, }); if (!HandlerCommands) this.LogActionsMessage += (0, chalk_1.magentaBright)(`\n• Registered ${GlobalCommands.length} Global Command${GlobalCommands.length > 1 ? "s." : "."}`); else this.LogActionsMessage += (0, chalk_1.magentaBright)(`\n• Registered ${GlobalCommands.length} Global Handler Command${GlobalCommands.length > 1 ? "s." : "."}`); } } catch (CommandSetError) { throw new Error((0, chalk_1.redBright)(`• Error Setting Commands\n${CommandSetError}`)) .message; } } async LoadEvents(EventsFolder, SkippedEvents = this.IgnoredEvents) { const EventFiles = Engine_1.Engine.ReadFiles(EventsFolder, [".ts", ".js"]); const IgnoredNamesLower = Array.isArray(SkippedEvents) && SkippedEvents.map((EventName) => EventName.toLowerCase()); for (const File of EventFiles) { try { const EventModule = await Promise.resolve(`${File}`).then(s => __importStar(require(s))); let EventFunction = EventModule.default ? EventModule.default : Object.values(EventModule)[0]; if (typeof EventFunction !== "function") throw new Error((0, chalk_1.redBright)(`• "${File}" does not have a callable export.`)); const LowerFunctionName = EventFunction.name.toLowerCase(); if (SkippedEvents === "All") { this.LogActionsMessage += (0, chalk_1.blueBright)("\n• Will not register events."); return; } if (Array.isArray(IgnoredNamesLower) && IgnoredNamesLower.includes(LowerFunctionName)) { if (!Object.values(BuiltInEvents) .map((Event) => Event.toLowerCase()) .some((Event) => IgnoredNamesLower.includes(Event))) { this.LogActionsMessage += (0, chalk_1.blueBright)(`\n• Ignoring event: ${EventFunction.name}`); } continue; } this.HandlerEvents.set(EventFunction.name, EventFunction); EventFunction(this, this.BotConfig); } catch (EventLoadError) { throw new GBF_Errors_1.GBFError(`Error Loading Events in "${File}"\n${EventLoadError}`); } } } async login(ProvidedToken, ProvidedMongoURI) { const BotToken = ProvidedToken !== null && ProvidedToken !== void 0 ? ProvidedToken : this.BotConfig.TOKEN; const MongoURI = ProvidedMongoURI !== null && ProvidedMongoURI !== void 0 ? ProvidedMongoURI : this.BotConfig.MongoURI; try { await super.login(BotToken); } catch (LoginError) { throw new GBF_Errors_1.GBFError(`Error Logging In\n${LoginError}`); } if (this.EventsFolder) try { await this.LoadEvents(this.EventsFolder); } catch (EventsError) { throw new GBF_Errors_1.GBFError(`Error Registering Custom Event Files\n${EventsError}`); } try { await this.LoadEvents(path_1.default.join(__dirname, "./Handler Events"), this.DisabledHandlerEvents); } catch (BuiltInEventsError) { throw new GBF_Errors_1.GBFError(`Error Registering Built-In Events\n${BuiltInEventsError}`); } if (!MongoURI) this.DatabaseInteractions = false; if (MongoURI && this.DatabaseInteractions) { try { await (0, mongoose_1.connect)(this.BotConfig.MongoURI, { bufferCommands: true, autoCreate: false, autoIndex: true, serverSelectionTimeoutMS: 30000, }); this.LogActionsMessage += (0, chalk_1.greenBright)("\n• Connected to MongoDB successfully."); } catch (MongoError) { if (this.DatabaseInteractions) this.DatabaseInteractions = false; throw new GBF_Errors_1.GBFError(`Couldn't connect to MongoDB\n${MongoError}`); } } else { process.emitWarning((0, chalk_1.yellowBright)("• Warning: No MongoURI provided, ensure MongoURI exists in your config, no database interactions will occur."), { code: "DataBaseWarn", detail: "No MongoURI was provided in the bot config, ensure that the MongoURI is set to the name MongoURI in your .env or JSON file, this instance will not use DataBase interactions until a valid URI is provided.", }); } if (!this.DisabledHandlerCommands.includes(BuiltInCommands.All)) try { await this.LoadCommands(path_1.default.join(__dirname, "./Handler Commands"), true); } catch (BuiltInCommandsError) { throw new GBF_Errors_1.GBFError(`Error Registering Built-In Commands\n${BuiltInCommandsError}`); } try { if (this.CommandsFolder) await this.LoadCommands(this.CommandsFolder); } catch (CommandsError) { console.log(CommandsError); process.exit(1); } if (this.LogActions) console.log(this.LogActionsMessage); return "Logged In"; } ToApplicationCommand(SlashCommands) { return SlashCommands.map((command) => { var _a, _b, _c; const CommandData = { name: command.CommandOptions.name, description: (_a = (command instanceof Slash_Handler_1.SlashCommand ? command.CommandOptions.description : null)) !== null && _a !== void 0 ? _a : null, options: (_b = (command instanceof Slash_Handler_1.SlashCommand ? command.CommandOptions.options : null)) !== null && _b !== void 0 ? _b : [], defaultMemberPermissions: command.CommandOptions.UserPermissions || [], defaultPermission: command.CommandOptions.development || false, dmPermission: command.CommandOptions.DMEnabled || this.DMEnabled, nsfw: command.CommandOptions.NSFW || false, type: (_c = (command instanceof Context_Handler_1.ContextCommand ? command.CommandOptions.ContextType : discord_js_1.ApplicationCommandType.ChatInput)) !== null && _c !== void 0 ? _c : discord_js_1.ApplicationCommandType.ChatInput, }; return CommandData; }); } } exports.GBF = GBF;