UNPKG

commandbot

Version:

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

222 lines (221 loc) 8.58 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.TargetID = exports.ObjectID = exports.InputParameter = exports.DefaultParameter = exports.Parameter = void 0; const errors_js_1 = require("../errors.js"); /** * Representation of command parameter * @class */ class Parameter { /** * @constructor * @param {Command} command - parameter parent command * @param {ParameterSchema} options - options used to compute a Parameter object */ constructor(command, options) { this.command = command; this.name = options.name; this.description = options.description || "No description"; this.optional = options.optional; this.type = options.type; this.choices = options.choices; if (!Parameter.nameRegExp.test(this.name)) { throw new Error(`Parameter name ${this.name} doesn't match the pattern`); } if (!Parameter.descriptionRegExp.test(this.description)) { throw new Error(`Parameter ${this.name}: Incorrect description`); } return; } } exports.Parameter = Parameter; /** * Parameter name check regular expression * @type {RegExp} * @public * @static */ Parameter.nameRegExp = /^[\w-]{1,32}$/; /** * Parameter description check regular expression * @type {RegExp} * @public * @static */ Parameter.descriptionRegExp = /^.{1,100}$/; class DefaultParameter extends Parameter { constructor(command) { super(command, { name: "input", description: "No description", type: "string", optional: true, }); } } exports.DefaultParameter = DefaultParameter; /** * Parameter with input value from interaction (reffered to as argument) * @class */ class InputParameter extends Parameter { /** * @constructor * @param {Parameter<T>} param - parent parameter * @param {?InputParameterValue<T>} value - input value */ constructor(param, value) { super(param.command, Object.assign({}, param)); let val = null; if ((value === null || value === undefined) && !this.optional) { throw new errors_js_1.MissingParameterError(this); } else if (value !== null && value !== undefined) { switch (this.type) { case "mentionable": case "channel": case "role": case "user": if (!(value instanceof ObjectID)) { throw new errors_js_1.ParameterTypeError(value, this.type); } val = value; break; case "boolean": if (typeof value === "string") { if (value.toLowerCase() === "true") { val = true; } else if (value.toLowerCase() === "false") { val = false; } else { throw new errors_js_1.ParameterTypeError(value, this.type); } } else if (value === true || value === false) { val = value; } else { throw new errors_js_1.ParameterTypeError(value, this.type); } break; case "number": const num = parseInt(value.toString()); if (isNaN(num)) { throw new errors_js_1.ParameterTypeError(value, this.type); } val = num; break; case "string": if (typeof value === "string" || value.toString()) { val = value.toString(); } else { throw new errors_js_1.ParameterTypeError(value, this.type); } if (this.choices && this.choices.findIndex((ch) => ch === value) === -1) { if (val !== "") { throw new TypeError(`Invalid choice. Please enter of the following options: ${this.choices.join(", ")}`); } else { val = null; } } break; } } this.value = val; } } exports.InputParameter = InputParameter; /** * Wrapped representation of Discord user, role, channel or other mentionable Discord arugment object * @class */ class ObjectID { /** * @constructor * @param {string} id - object Discord ID * @param {ObjectIdType} type - object type * @param {?Guild} [guild] - guild needed to convert the object */ constructor(id, type, guild) { this.id = id.replace(">", "").replace("<@!", "").replace("<#!", "").split(" ").join(""); this.type = type; this.guild = guild; } /** * Uses informations associated with the object to generate a Discord.js representation * @returns {?Promise<ObjectIdReturnType<T>>} A fetched object (or null) * @public * @async */ toObject() { var _a, _b, _c, _d, _e, _f; return __awaiter(this, void 0, void 0, function* () { switch (this.type) { case "channel": return (_b = (yield ((_a = this.guild) === null || _a === void 0 ? void 0 : _a.channels.fetch(this.id.toString() || "")))) !== null && _b !== void 0 ? _b : null; case "role": return (_d = (yield ((_c = this.guild) === null || _c === void 0 ? void 0 : _c.roles.fetch(this.id.toString() || "")))) !== null && _d !== void 0 ? _d : null; case "user": return (_f = (yield ((_e = this.guild) === null || _e === void 0 ? void 0 : _e.members.fetch(this.id.toString() || "")))) !== null && _f !== void 0 ? _f : null; default: return null; } }); } } exports.ObjectID = ObjectID; /** * Wrapped representation of Discord target object (target of context menu interactions) * @class */ class TargetID { /** * @constructor * @param {string} id - object Discord ID * @param {TargetType} - target type * @param {Interaction | Message} [interaction] - interaction associated with the target */ constructor(id, type, interaction) { this.id = id; this.type = type; this.interaction = interaction; } /** * Uses informations associated with the object to generate a Discord.js representation * @returns {?Promise<TargetIdReturnType<T>>} A fetched object (or null) * @public * @async */ toObject() { var _a, _b; switch (this.type) { case "MESSAGE": if (!this.interaction.channel) { throw new Error("Channel not found"); } return (_a = this.interaction.channel.messages.cache.get(this.id)) !== null && _a !== void 0 ? _a : null; case "USER": const guild = this.interaction.guild; if (!guild) { throw new Error("Guild not found"); } return (_b = guild.members.cache.get(this.id)) !== null && _b !== void 0 ? _b : null; default: return null; } } } exports.TargetID = TargetID;