UNPKG

@beni69/cmd

Version:

The command handler from my discord bot

134 lines 5.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Trigger = void 0; const discord_js_1 = require("discord.js"); const yargs_parser_1 = __importDefault(require("yargs-parser")); const Utils_1 = require("./Utils"); class Trigger { constructor(handler, command, source) { this.isClassic = () => "author" in this.source; this.isSlash = () => !this.isClassic(); this.reply = async (msg) => { if (this.isClassic()) { this.response = await this.source.reply(msg); return this.response; } else if (this.isSlash()) return await this.source.reply( // @ts-ignore typeof msg === "string" ? { ephemeral: this.opts.ephemeral, content: msg } : { ephemeral: this.opts.ephemeral, ...msg }); }; this.fetchReply = async () => { if (this.isClassic()) return this.response; else if (this.isSlash()) return (await this.source.fetchReply()); }; this.edit = (msg) => { if (this.isClassic()) { if (!this.response) throw new Error("you haven't responded to this message yet!"); return this.response.edit(msg); } else if (this.isSlash()) { return this.source.editReply(msg); } }; this.followUp = (msg) => { if (this.isClassic()) { return this.source.reply(msg); } else if (this.isSlash()) { return this.source.followUp(msg); } }; this.args2Opt = (args) => { var _a; const opts = []; (_a = this.command.opts.options) === null || _a === void 0 ? void 0 : _a.forEach((o, i) => { const res = (0, Utils_1.resolveMention)( // if the guild doesn't exist, the client is enough this.guild || { client: this.client }, args[i]); if (res.type !== o.type) { this.error = new Error("invalid argument type"); return; } opts.push({ ...res, name: o.name }); }); // private constructor // @ts-ignore return new discord_js_1.CommandInteractionOptionResolver(this.client, opts); }; this.args2Yargs = (args) => { var _a; const opts = []; const { argvAliases } = this.command.opts; const y = (0, yargs_parser_1.default)(args, { alias: argvAliases, }); // set the default args to the option key which has an alias of "_" if (argvAliases) { const k = Object.keys(argvAliases).find(k => argvAliases[k].includes("_")); if (k) y[k] = y["_"].join(" "); } (_a = this.command.opts.options) === null || _a === void 0 ? void 0 : _a.forEach(o => { const res = (0, Utils_1.resolveMention)( // if the guild doesn't exist, the client is enough this.guild || { client: this.client }, y[o.name]); if (res.type !== o.type) { this.error = new Error("invalid argument type"); return; } opts.push({ ...res, name: o.name }); }); // private constructor // @ts-ignore return new discord_js_1.CommandInteractionOptionResolver(this.client, opts); }; this.handler = handler; this.command = command; this.source = source; this.opts = { ephemeral: !!command.opts.ephemeral, deferred: !!command.opts.deferred, }; this.id = source.id; this.client = source.client; this.guild = source.guild; this.channel = source.channel; this.member = source.member; this.createdAt = source.createdAt; this.createdTimestamp = source.createdTimestamp; if ("author" in source) { this.author = source.author; this.content = source.content; this.argv = command.opts.yargs ? this.args2Yargs(this.args) : this.args2Opt(this.args); } else { this.author = source.user; this.content = `/${source.commandName} ${source.options && source.options.data.map(o => o.value)}`.trim(); this.argv = source.options; } } get args() { if (this.isClassic()) return this.content .slice(this.handler.getOpts.prefix.length) .trim() .split(/\s+/) .slice(1); else return this.source.options.data.map(o => o.value); } } exports.Trigger = Trigger; exports.default = Trigger; //# sourceMappingURL=Trigger.js.map