UNPKG

commandbot

Version:

A framework that helps you create your own Discord bot easier.

196 lines (195 loc) 10.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Bot = void 0; const discord_js_1 = require("discord.js"); const events_1 = require("events"); const http = require("http"); const CommandManager_js_1 = require("./CommandManager.js"); const errors_js_1 = require("../errors.js"); const SystemMessage_js_1 = require("./SystemMessage.js"); const state_js_1 = require("../state.js"); /** * Application instance * @class * @extends {EventEmitter} */ class Bot extends events_1.EventEmitter { /** * Main bot constructor * @constructor * @param {InitOptions} options - instance properties ({@link InitOptions}) */ constructor({ name, token, applicationId, globalPrefix, argumentSeparator, commandSeparator, clientOptions, help }) { super(); this.name = name; this.client = new discord_js_1.Client(clientOptions !== null && clientOptions !== void 0 ? clientOptions : { intents: [ discord_js_1.Intents.FLAGS.GUILDS, discord_js_1.Intents.FLAGS.GUILD_BANS, discord_js_1.Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS, discord_js_1.Intents.FLAGS.GUILD_INTEGRATIONS, discord_js_1.Intents.FLAGS.GUILD_INVITES, discord_js_1.Intents.FLAGS.GUILD_MEMBERS, discord_js_1.Intents.FLAGS.GUILD_MESSAGES, discord_js_1.Intents.FLAGS.GUILD_MESSAGE_REACTIONS, discord_js_1.Intents.FLAGS.GUILD_MESSAGE_TYPING, discord_js_1.Intents.FLAGS.GUILD_PRESENCES, discord_js_1.Intents.FLAGS.GUILD_VOICE_STATES, discord_js_1.Intents.FLAGS.GUILD_WEBHOOKS, discord_js_1.Intents.FLAGS.DIRECT_MESSAGES, discord_js_1.Intents.FLAGS.DIRECT_MESSAGE_REACTIONS, discord_js_1.Intents.FLAGS.DIRECT_MESSAGE_TYPING, ], }); this.messages = new SystemMessage_js_1.SystemMessageManager(this); this.commands = new CommandManager_js_1.CommandManager(this, help !== null && help !== void 0 ? help : { enabled: true, title: "Help", description: "List of all available commands", bottomText: "List of all available commands", ephemeral: "INTERACTIONS", }, globalPrefix, argumentSeparator, commandSeparator); this.token = token; this.applicationId = applicationId; } /** * Starts your Discord bot * @param {?number} [port] - if specified, the app will create a http server that will be listening on the specified port (useful when hosting your bot on platforms like Heroku) * @param {?boolean} [register=true] - if *true* or *undefined*, the bot will register all interactions in the Discord API * @returns {Promise<boolean>} whether this operation has been completed successfully * @public * @async */ start(port, register) { return __awaiter(this, void 0, void 0, function* () { try { if (state_js_1.applicationState.running) { throw new Error("This bot is already running"); } if (state_js_1.applicationState.dev) { console.warn(`[⚠️ WARNING] You are using an unstable version of the CommandBot package. It is not recommended to use this version in production.`); } console.log(`\nBot name: ${this.name}`); console.log(`Global prefix: ${this.commands.prefix.globalPrefix || "/ (only slash commands)"} \n`); if (this.token === "") { throw new ReferenceError('No token specified. Please pass your Discord application token as an argument to the "start" method or in the constructor'); } if (port) { process.stdout.write(`Creating http server on port ${port}... `); http.createServer().listen(port); console.log("✔"); } state_js_1.applicationState.running = true; process.stdout.write("Connecting to Discord... "); this.client.login(this.token); this.client.on("ready", () => __awaiter(this, void 0, void 0, function* () { if (register === undefined || register === true) { console.log("✔"); process.stdout.write(`Registering commands... `); yield this.commands.register(); console.log("✔"); } else { console.log("✔\n"); } this.emit("READY"); })); this.client.on("messageCreate", (m) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (m.author.bot) return; let inputData = null; try { inputData = this.commands.fetch(m); if (inputData) { this.emit("COMMAND", m, inputData); yield inputData.command.start(inputData); } else { this.emit("MESSAGE", m); } } catch (e) { if (e instanceof errors_js_1.PermissionsError) { this.emit("ERROR", e); yield this.messages.send("PERMISSION", { user: (_a = m.member) !== null && _a !== void 0 ? _a : undefined, command: inputData === null || inputData === void 0 ? void 0 : inputData.command, }, m); } else if (e instanceof errors_js_1.OperationSuccess) { yield this.messages.send("SUCCESS", { command: e.command }, m); } else if (e instanceof errors_js_1.CommandNotFound) { yield this.messages.send("NOT_FOUND", { phrase: e.query, user: (_b = m.member) !== null && _b !== void 0 ? _b : undefined }, m); } else { this.emit("ERROR", e); yield this.messages.send("ERROR", { command: inputData === null || inputData === void 0 ? void 0 : inputData.command, user: (_c = m.member) !== null && _c !== void 0 ? _c : undefined, error: e, }, m); } return; } })); this.client.on("interactionCreate", (i) => __awaiter(this, void 0, void 0, function* () { var _d, _e; let inputData = null; try { inputData = this.commands.fetch(i); if (inputData) { this.emit("COMMAND", i, inputData); yield inputData.command.start(inputData); } } catch (e) { if (e instanceof errors_js_1.PermissionsError) { yield this.messages.send("PERMISSION", { user: (_d = i.member) !== null && _d !== void 0 ? _d : undefined, command: inputData === null || inputData === void 0 ? void 0 : inputData.command, }, i); this.emit("ERROR", e); } else if (e instanceof errors_js_1.OperationSuccess) { yield this.messages.send("SUCCESS", { command: e.command }, i); } else if (e instanceof errors_js_1.CommandNotFound) { yield this.messages.send("NOT_FOUND", { user: i.user, phrase: e.query }, i); } else { yield this.messages.send("ERROR", { command: inputData === null || inputData === void 0 ? void 0 : inputData.command, user: (_e = i.member) !== null && _e !== void 0 ? _e : undefined, error: e, }, i); this.emit("ERROR", e); } return; } })); this.client.on("error", (e) => { this.emit("ERROR", e); }); return true; } catch (e) { console.log("❌"); console.error(`[❌ ERROR] ${e}`); return false; } }); } } exports.Bot = Bot; exports.default = Bot;