@mxgnus/slashcommands.js
Version:
A simple discord.js v13 & v14 slash command package
1,025 lines (1,024 loc) • 39.9 kB
JavaScript
"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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteAllSlashcommands = exports.deleteAllGuildSlashcommands = exports.deleteSlashcommand = exports.deleteGuildSlashcommand = exports.fetchSlashcommandByName = exports.fetchSlashcommandById = exports.fetchSlashcommands = exports.fetchGuildSlashcommands = exports.GuildSlashCommand = exports.SlashCommandOptionChoice = exports.SlashcommandOption = exports.Slashcommand = exports.Slash = void 0;
const discord_js_1 = require("discord.js");
const events_1 = require("events");
const colors_1 = __importDefault(require("colors"));
let that;
let emitter = new events_1.EventEmitter();
const options = {
debug: false,
guildId: undefined,
};
class Slash {
/**
* Initializes the Slash class.
* @param {Client} client - Discord.Client - The discord client.
* @param {Options} clientOptions - Options = {}
* @returns The class instance.
*/
constructor(client, clientOptions = {}) {
this.GuildSlashCommand = GuildSlashCommand;
this.fetchGuildSlashcommands = fetchGuildSlashcommands;
this.deleteGuildSlashcommand = deleteGuildSlashcommand;
this.deleteAllGuildSlashcommands = deleteAllGuildSlashcommands;
this.Slashcommand = Slashcommand;
this.fetchSlashcommands = fetchSlashcommands;
this.fetchSlashcommandById = fetchSlashcommandById;
this.fetchSlashcommandByName = fetchSlashcommandByName;
this.deleteSlashcommand = deleteSlashcommand;
this.deleteAllSlashcommands = deleteAllSlashcommands;
this.client = client;
that = client;
if (clientOptions.debug)
options.debug = clientOptions.debug;
if (clientOptions.guildId) {
validateGuildId(clientOptions.guildId);
options.guildId = clientOptions.guildId;
}
debugLogger(`Slash initialized.`);
if (!isReady()) {
client.on('ready', () => {
emitter.emit('ready');
});
}
return this;
}
}
exports.Slash = Slash;
class GuildSlashCommand {
/**
* Creates a new guild slash command.
* @returns The class instance.
*/
constructor() {
this.name = null;
this.description = null;
this.options = [];
this.guildId = null;
this.type = null;
this.dmPermission = null;
this.nameLocalizations = null;
this.descriptionLocalizations = null;
this.defaultMemberPermissions = null;
if (!isInit()) {
throw new Error('Slash command not initialized');
}
if (options.guildId)
this.guildId = options.guildId;
return this;
}
/**
* This function sets the guildId property of the class instance to the value of the guildId
* parameter, and returns the class instance.
* @param {string} guildId - The ID of the guild.
* @returns The instance of the class.
*/
setGuildId(guildId) {
validateGuildId(guildId);
this.guildId = guildId;
return this;
}
/**
* This function takes a string, validates it, and then sets the name property of the object to the
* lowercase version of the string.
* @param {string} name - The name of the slash command.
* @returns The instance of the class.
*/
setName(name) {
validateName(name);
this.name = name.toLowerCase();
return this;
}
/**
* The setDescription function takes a string as an argument, validates it, and then sets the
* description property of the object to the value of the argument.
* @param {string} description - The description of the slash command.
* @returns The instance of the class.
*/
setDescription(description) {
validateDescription(description);
this.description = description;
return this;
}
/**
* This function takes an array of objects and checks if the objects have a name, type and
* description property. If they don't, it throws an error.
* @param {ApplicationCommandOptionData[]} options - ApplicationCommandOptionData[]
* @returns The instance of the class.
*/
setOptions(options) {
if (!validateOptions(this.options)) {
throw new Error('Invalid options, slashcommand option must have a name, type and description');
}
this.options = formatOptions(options);
return this;
}
/**
* This function adds an option to the command
* @param {ApplicationCommandOptionData} option - ApplicationCommandOptionData
* @returns The ApplicationCommandData object.
*/
addOption(option) {
this.options.push(formatOptions([option])[0]);
return this;
}
/**
* This function sets the dmPermission property to the value of the dmPermission parameter, and if
* the type of the dmPermission parameter is not a boolean, it throws an error.
* @param {boolean} dmPermission - boolean - Whether or not slashcommand can be used in DMs.
* @returns The class instance.
*/
setDmPermission(dmPermission) {
this.dmPermission = dmPermission;
if (typeof this.dmPermission !== 'boolean') {
throw new Error('DM permission must be a boolean');
}
return this;
}
/**
* This function sets the nameLocalizations property of the object to the value of the
* nameLocalizations parameter.
* @param {LocalizationMap} nameLocalizations - LocalizationMap
* @returns The class instance.
*/
setNameLocalizations(nameLocalizations) {
this.nameLocalizations = nameLocalizations;
return this;
}
/**
* This function sets the descriptionLocalizations property of the object to the value of the
* descriptionLocalizations parameter.
* @param {LocalizationMap} descriptionLocalizations - LocalizationMap
* @returns The object itself.
*/
setDescriptionLocalizations(descriptionLocalizations) {
this.descriptionLocalizations = descriptionLocalizations;
return this;
}
/**
* This function sets the default member permissions for the slash command.
* @param {PermissionResolvable} defaultMemberPermissions - PermissionResolvable
* @returns The instance of the class.
*/
setDefaultMemberPermissions(defaultMemberPermissions) {
this.defaultMemberPermissions = defaultMemberPermissions;
return this;
}
/**
* This function creates a slash command in a guild, and returns the slash command object.
* @returns The slashcommand object.
*/
register() {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
if (!this.name) {
throw new Error('Name is not set');
}
if (!this.description) {
throw new Error('Description is not set');
}
if (!this.guildId) {
throw new Error('Guild ID is not set');
}
if (!validateOptions(this.options)) {
throw new Error('Invalid options, slashcommand option must have a name, type and description');
}
if (!isReady()) {
yield waitForReady();
}
const guild = that.guilds.cache.get(this.guildId) ||
(yield that.guilds.fetch(this.guildId));
if (!guild) {
throw new Error('Guild not found');
}
let err = null;
debugLogger(`Registering slash command ${this.name} in guild ${guild.id}`);
const slashcommand = yield guild.commands
.create({
name: this.name,
description: this.description,
options: this.options,
dmPermission: (_a = this.dmPermission) !== null && _a !== void 0 ? _a : undefined,
nameLocalizations: (_b = this.nameLocalizations) !== null && _b !== void 0 ? _b : undefined,
descriptionLocalizations: (_c = this.descriptionLocalizations) !== null && _c !== void 0 ? _c : undefined,
defaultMemberPermissions: (_d = this.defaultMemberPermissions) !== null && _d !== void 0 ? _d : undefined,
})
.catch((e) => {
err = e;
throw e;
});
if (err !== null || !slashcommand) {
}
return slashcommand;
});
}
}
exports.GuildSlashCommand = GuildSlashCommand;
class Slashcommand {
/**
* Create a new slashcommand.
* @returns The instance of the class.
*/
constructor() {
this.name = null;
this.description = null;
this.options = [];
this.dmPermission = null;
this.nameLocalizations = null;
this.descriptionLocalizations = null;
this.defaultMemberPermissions = null;
if (!isInit()) {
throw new Error('Slash command not initialized');
}
return this;
}
/**
* This function takes a string, validates it, and then sets the name property of the object to the
* lowercase version of the string.
* @param {string} name - The name of the new slash command.
* @returns The instance of the class.
*/
setName(name) {
validateName(name);
this.name = name.toLowerCase();
return this;
}
/**
* The function takes a string as an argument, validates it, and then sets the description property
* of the object to the value of the argument
* @param {string} description - The description of the slashcommand.
* @returns The instance of the class.
*/
setDescription(description) {
validateDescription(description);
this.description = description;
return this;
}
/**
* This function takes an array of objects and checks if the objects have a name, type and
* description property. If they don't, it throws an error.
* @param {ApplicationCommandOptionData[]} options - ApplicationCommandOptionData[]
* @returns The instance of the class.
*/
setOptions(options) {
if (!validateOptions(this.options)) {
throw new Error('Invalid options, slashcommand option must have a name, type and description');
}
this.options = formatOptions(options);
return this;
}
/**
* This function adds an option to the command
* @param {ApplicationCommandOptionData} option - ApplicationCommandOptionData
* @returns The ApplicationCommandData object.
*/
addOption(option) {
this.options.push(formatOptions([option])[0]);
return this;
}
/**
* This function sets the dmPermission property to the value of the dmPermission parameter, and if
* the type of the dmPermission parameter is not a boolean, it throws an error.
* @param {boolean} dmPermission - boolean - Whether or not slashcommand can be used in DMs.
* @returns The class instance.
*/
setDmPermission(dmPermission) {
this.dmPermission = dmPermission;
if (typeof this.dmPermission !== 'boolean') {
throw new Error('DM permission must be a boolean');
}
return this;
}
/**
* This function sets the nameLocalizations property of the object to the value of the
* nameLocalizations parameter.
* @param {LocalizationMap} nameLocalizations - LocalizationMap
* @returns The instance of the class.
*/
setNameLocalizations(nameLocalizations) {
this.nameLocalizations = nameLocalizations;
return this;
}
/**
* This function sets the descriptionLocalizations property of the object to the value of the
* descriptionLocalizations parameter.
* @param {LocalizationMap} descriptionLocalizations - LocalizationMap
* @returns The instance of the class.
*/
setDescriptionLocalizations(descriptionLocalizations) {
this.descriptionLocalizations = descriptionLocalizations;
return this;
}
/**
* This function sets the default member permissions for the slash command.
* @param {PermissionResolvable} defaultMemberPermissions - PermissionResolvable
* @returns The instance of the class.
*/
setDefaultMemberPermissions(defaultMemberPermissions) {
this.defaultMemberPermissions = defaultMemberPermissions;
return this;
}
/**
* Creates the slash command.
* @returns The slashcommand object.
*/
register() {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
if (!this.name) {
throw new Error('Name is not set');
}
if (!this.description) {
throw new Error('Description is not set');
}
if (!validateOptions(this.options)) {
throw new Error('Invalid options, slashcommand option must have a name, type and description');
}
if (!isReady()) {
yield waitForReady();
}
if (!that.application) {
throw new Error('Application in client not found');
}
debugLogger(`Registering slash command ${this.name} in application ${that.application.name ? that.application.name : 'unknown'} (${that.application.id})`);
let err = false;
const slashcommand = yield that.application.commands
.create({
name: this.name,
description: this.description,
options: this.options,
dmPermission: (_a = this.dmPermission) !== null && _a !== void 0 ? _a : undefined,
nameLocalizations: (_b = this.nameLocalizations) !== null && _b !== void 0 ? _b : undefined,
descriptionLocalizations: (_c = this.descriptionLocalizations) !== null && _c !== void 0 ? _c : undefined,
defaultMemberPermissions: (_d = this.defaultMemberPermissions) !== null && _d !== void 0 ? _d : undefined,
})
.catch((e) => {
err = true;
throw e;
});
if (!slashcommand || err)
return;
return slashcommand;
});
}
}
exports.Slashcommand = Slashcommand;
class SlashCommandOptionChoice {
/**
* Create a new slash command option choice.
* @returns The object that is being created.
*/
constructor() {
this.name = null;
this.value = null;
this.nameLocalizations = null;
return this;
}
/**
* The setName function takes a string, number, or boolean as an argument and sets the name property
* of the Choice object to the value of the argument.
* @param {string | number | boolean} name - The name of the choice.
* @returns The instance of the class.
*/
setName(name) {
validateChoiceName(name);
this.name = name;
return this;
}
/**
* This function sets the value of the choice to the value passed in, and returns the choice.
* @param {string | number | boolean} value - string | number | boolean
* @returns The value of the property "value"
*/
setValue(value) {
validateChoiceValue(value);
this.value = value;
return this;
}
/**
* This function sets the nameLocalizations property of the object to the value of the
* nameLocalizations parameter.
* @param {LocalizationMap} nameLocalizations - LocalizationMap
* @returns The object itself.
*/
setNameLocalizations(nameLocalizations) {
this.nameLocalizations = nameLocalizations;
return this;
}
}
exports.SlashCommandOptionChoice = SlashCommandOptionChoice;
class SlashcommandOption {
/**
* Creates a new SlashcommandOption object.
* @returns The object that is being created.
*/
constructor() {
this.name = null;
this.description = null;
this.type = null;
this.required = false;
this.autocomplete = false;
this.nameLocalizations = null;
this.descriptionLocalizations = null;
this.options = null;
return this;
}
/**
* This function takes a string, validates it, and then sets the name property of the object to the
* lowercase version of the string.
* @param {string} name - The name of the new user.
* @returns The instance of the class.
*/
setName(name) {
validateName(name);
this.name = name.toLowerCase();
return this;
}
/**
* The function takes a string as an argument, validates it, and then sets the description property
* of the object to the value of the argument
* @param {string} description - The description of the task.
* @returns The instance of the class.
*/
setDescription(description) {
validateDescription(description);
this.description = description;
return this;
}
/**
* This function sets the type of the option to the type passed in as a parameter and returns the
* option.
* @param {SlashcommandOptionType} type - The type of option.
* @returns The object itself.
*/
setType(type) {
if (typeof type === 'string') {
const formattedType = (type.charAt(0).toUpperCase() +
type.slice(1).toLowerCase());
const applicationCommandType = discord_js_1.ApplicationCommandOptionType[formattedType];
if (!applicationCommandType) {
throw new Error(`Unknown slashcommand option type ${type}`);
}
this.type = applicationCommandType;
}
else {
this.type = type;
}
return this;
}
/**
* This function sets the required property of the class to the value passed in.
* @param {boolean} required - boolean
* @returns The instance of the class.
*/
setRequired(required) {
if (typeof required !== 'boolean') {
throw new Error('Required must be a boolean');
}
this.required = required;
return this;
}
/**
* This function sets the autocomplete property of the class to the value passed in as a parameter.
* @param {boolean} autocomplete - boolean
* @returns The object itself.
*/
setAutocomplete(autocomplete) {
this.autocomplete = autocomplete;
return this;
}
/**
* This function sets the allowed channel types for the slash command.
* @param {SlashcommandChannelType[]} channelTypes - SlashcommandChannelType[]
* @returns The object itself.
*/
setChannelTypes(channelTypes) {
this.channelTypes = channelTypes;
return this;
}
/**
* This function sets the maxValues property of the object to the value passed in as a parameter and
* returns the object.
* @param {number} maxValues - The maximum number of values to return.
* @returns The object itself.
*/
setMaxValues(maxValues) {
this.maxValue = maxValues;
return this;
}
/**
* This function sets the minValue property of the object to the value passed in as a parameter and
* returns the object.
* @param {number} minValue - The minimum value of the range.
* @returns The object itself.
*/
setMinValue(minValue) {
this.minValue = minValue;
return this;
}
/**
* This function sets the choices for the slash command
* @param {SlashCommandOptionChoice[]} choices - An array of SlashCommandOptionChoice objects.
* @returns The object itself.
*/
setChoices(choices) {
this.choices = choices;
return this;
}
/**
* This function sets the nameLocalizations property of the object to the value of the
* nameLocalizations parameter.
* @param {LocalizationMap} nameLocalizations - LocalizationMap
* @returns The instance of the class.
*/
setNameLocalizations(nameLocalizations) {
this.nameLocalizations = nameLocalizations;
return this;
}
/**
* This function sets the descriptionLocalizations property of the object to the value of the
* descriptionLocalizations parameter.
* @param {LocalizationMap} descriptionLocalizations - LocalizationMap
* @returns The instance of the class.
*/
setDescriptionLocalizations(descriptionLocalizations) {
this.descriptionLocalizations = descriptionLocalizations;
return this;
}
/**
* It takes an array of objects
* @param {(ApplicationCommandOptionData| SlashcommandOption)[]} options -
* (ApplicationCommandOptionData| SlashcommandOption)[]
* @returns The object itself.
*/
setOptions(options) {
this.options = formatOptions(options);
return this;
}
/**
* This function adds an option to the option
* @param {ApplicationCommandOptionData | SlashcommandOption} option - ApplicationCommandOptionData
* | SlashcommandOption
* @returns The object itself.
*/
addOption(option) {
if (!this.options) {
this.options = [];
}
this.options.push(formatOptions([option])[0]);
return this;
}
}
exports.SlashcommandOption = SlashcommandOption;
function fetchGuildSlashcommands({ guildId } = {
guildId: '',
}) {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
if (options.guildId)
guildId = options.guildId;
if (!guildId) {
throw new Error('Guild ID is not set');
}
validateGuildId(guildId);
if (!isReady()) {
yield waitForReady();
}
debugLogger(`Fetching slash commands for guild ${guildId}`);
let err = false;
const guild = that.guilds.cache.get(guildId) || (yield that.guilds.fetch(guildId));
if (!guild) {
throw new Error('Guild not found');
}
const slashcommands = yield guild.commands.fetch().catch((e) => {
err = true;
throw e;
});
if (err || !slashcommands)
return;
return slashcommands;
});
}
exports.fetchGuildSlashcommands = fetchGuildSlashcommands;
function fetchSlashcommandById({ id, guildId, }) {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
else if (!isReady()) {
yield waitForReady();
}
else if (!id) {
throw new Error('Id is not set');
}
if (!that.application) {
throw new Error('Application in client not found');
}
debugLogger(`Fetching slash command ${id}`);
let err = false;
const slashcommand = yield that.application.commands
.fetch(id, {
guildId,
})
.catch((e) => {
err = true;
throw e;
});
if (err || !slashcommand)
return;
return slashcommand;
});
}
exports.fetchSlashcommandById = fetchSlashcommandById;
function fetchSlashcommands() {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
if (!isReady()) {
yield waitForReady();
}
if (!that.application) {
throw new Error('Application in client not found');
}
debugLogger(`Fetching slash commands for application ${that.application.name} (${that.application.id})`);
let err = false;
const slashcommands = yield that.application.commands.fetch().catch((e) => {
err = true;
throw e;
});
if (err || !slashcommands)
return;
return slashcommands;
});
}
exports.fetchSlashcommands = fetchSlashcommands;
function fetchSlashcommandByName({ name, guildId, }) {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
else if (!isReady()) {
yield waitForReady();
}
else if (!name) {
throw new Error('Name is not set');
}
if (!that.application) {
throw new Error('Application in client not found');
}
debugLogger(`Fetching slash command ${name}`);
let err = false;
if (guildId) {
const guildCommands = yield fetchGuildSlashcommands({ guildId });
if (!(guildCommands === null || guildCommands === void 0 ? void 0 : guildCommands.size)) {
return null;
}
const slashcommand = guildCommands.find((command) => command.name === name);
return slashcommand || null;
}
else {
const slashcommands = yield fetchSlashcommands();
if (!(slashcommands === null || slashcommands === void 0 ? void 0 : slashcommands.size)) {
return null;
}
const slashcommand = slashcommands.find((command) => command.name === name);
return slashcommand || null;
}
});
}
exports.fetchSlashcommandByName = fetchSlashcommandByName;
function deleteGuildSlashcommand({ guildId, name, id, } = {
guildId: '',
}) {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
if (options.guildId)
guildId = options.guildId;
if (!guildId) {
throw new Error('Guild ID is not set');
}
validateGuildId(guildId);
if (!name && !id) {
throw new Error('Name or ID is not set. Please specify one');
}
if (!isReady()) {
yield waitForReady();
}
let cmdId = id;
if (!id && name) {
const slashcommands = yield fetchGuildSlashcommands({ guildId });
if (!slashcommands)
throw new Error('Invalid slashcommand id');
const slashcommand = slashcommands.find((c) => c.name === name);
if (!slashcommand)
throw new Error('Invalid slashcommand name');
cmdId = slashcommand.id;
}
if (!cmdId)
throw new Error('Invalid slashcommand id');
debugLogger(`Deleting slash command ${cmdId} for guild ${guildId}`);
let err = false;
const guild = that.guilds.cache.get(guildId) || (yield that.guilds.fetch(guildId));
if (!guild) {
throw new Error('Guild not found');
}
const slashcommand = yield guild.commands.delete(cmdId).catch((e) => {
err = true;
throw e;
});
if (err || !slashcommand)
return;
return slashcommand;
});
}
exports.deleteGuildSlashcommand = deleteGuildSlashcommand;
function deleteSlashcommand({ name, id, } = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
if (!name && !id) {
throw new Error('Name or ID is not set. Please specify one');
}
if (!isReady()) {
yield waitForReady();
}
if (!that.application) {
throw new Error('Application in client not found');
}
let cmdId = id;
if (!id && name) {
const slashcommands = yield fetchSlashcommands();
if (!slashcommands)
throw new Error('Invalid slashcommand id');
const slashcommand = slashcommands.find((c) => c.name === name);
if (!slashcommand)
throw new Error('Invalid slashcommand name');
cmdId = slashcommand.id;
}
if (!cmdId)
throw new Error('Invalid slashcommand id');
debugLogger(`Deleting slash command ${cmdId} for application ${that.application.name} (${that.application.id})`);
let err = false;
const slashcommand = yield that.application.commands
.delete(cmdId)
.catch((e) => {
err = true;
throw e;
});
if (err || !slashcommand)
return;
return slashcommand;
});
}
exports.deleteSlashcommand = deleteSlashcommand;
function deleteAllGuildSlashcommands({ guildId, } = {
guildId: '',
}) {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
if (options.guildId)
guildId = options.guildId;
if (!guildId) {
throw new Error('Guild ID is not set');
}
validateGuildId(guildId);
if (!isReady()) {
yield waitForReady();
}
debugLogger(`Deleting all slash commands for guild ${guildId}`);
let err = false;
const guild = that.guilds.cache.get(guildId) || (yield that.guilds.fetch(guildId));
if (!guild) {
throw new Error('Guild not found');
}
const slashcommands = yield guild.commands.fetch().catch((e) => {
err = true;
throw e;
});
if (err || !slashcommands)
return;
const deleted = yield Promise.all(slashcommands.map((c) => guild.commands.delete(c.id)));
return deleted;
});
}
exports.deleteAllGuildSlashcommands = deleteAllGuildSlashcommands;
function deleteAllSlashcommands() {
return __awaiter(this, void 0, void 0, function* () {
if (!isInit()) {
throw new Error('Slash command not initialized');
}
if (!isReady()) {
yield waitForReady();
}
if (!that.application) {
throw new Error('Application in client not found');
}
debugLogger(`Deleting all slash commands for application ${that.application.name} (${that.application.id})`);
let err = false;
const slashcommands = yield that.application.commands.fetch().catch((e) => {
err = true;
throw e;
});
if (err || !slashcommands)
return;
const deleted = yield Promise.all(slashcommands.map((c) => {
if (!that.application)
return;
that.application.commands.delete(c.id);
}));
return deleted;
});
}
exports.deleteAllSlashcommands = deleteAllSlashcommands;
function validateOptions(options) {
return options.every((option) => {
return option.name && option.description && option.type;
});
}
function isInit() {
return !!that;
}
function validateGuildId(guildId) {
if (typeof guildId !== 'string')
throw new Error('GuildId must be a string');
if (!/^[0-9]{17,19}$/.test(guildId)) {
throw new Error('Invalid guild ID');
}
}
function validateName(name) {
if (typeof name !== 'string')
throw new Error('Name must be a string');
if (name.length > 32)
throw new Error('Name has to be less than 32 characters');
if (name.length < 1)
throw new Error('Name has to be more than 1 characters');
if (name.match(/[^a-zA-Z0-9_]/g))
throw new Error('Name can only contain letters, numbers and underscores');
return;
}
function validateDescription(description) {
if (typeof description !== 'string')
throw new Error('Description must be a string');
if (description.length > 100)
throw new Error('Description has to be less than 100 characters');
if (description.length < 1)
throw new Error('Description has to be more than 1 characters');
return;
}
function formatOptions(options) {
var _a, _b;
const optionsArray = [];
for (const option of options) {
if (option instanceof SlashcommandOption) {
if (typeof option.name !== 'string' ||
typeof option.description !== 'string' ||
typeof option.type !== 'number') {
throw new Error('Invalid option ' + JSON.stringify(option));
}
if (option.type === discord_js_1.ApplicationCommandOptionType.Subcommand ||
option.type === discord_js_1.ApplicationCommandOptionType.SubcommandGroup) {
throw new Error('Subcommand and subcommand group are not supported yet');
}
else if (option.type === discord_js_1.ApplicationCommandOptionType.Integer ||
option.type === discord_js_1.ApplicationCommandOptionType.Number) {
if (option.choices &&
!option.choices.every((c) => typeof c.value === 'number')) {
throw new Error('Invalid choices, choice value must be a number! For option ' +
JSON.stringify(option));
}
optionsArray.push({
name: option.name,
description: option.description,
type: option.type,
minValue: option.minValue,
maxValue: option.maxValue,
autocomplete: ((_a = option.choices) === null || _a === void 0 ? void 0 : _a.length)
? false
: option.autocomplete,
choices: option.choices ||
undefined,
nameLocalizations: option.nameLocalizations || undefined,
descriptionLocalizations: option.descriptionLocalizations || undefined,
required: option.required,
});
}
else if (option.type === discord_js_1.ApplicationCommandOptionType.String) {
if (option.choices &&
!option.choices.every((c) => typeof c.value === 'string')) {
throw new Error('Invalid choices, choice value must be a string! For option ' +
JSON.stringify(option));
}
optionsArray.push({
name: option.name,
description: option.description,
type: option.type,
nameLocalizations: option.nameLocalizations || undefined,
descriptionLocalizations: option.descriptionLocalizations || undefined,
required: option.required,
maxLength: option.maxValue,
minLength: option.minValue,
autocomplete: ((_b = option.choices) === null || _b === void 0 ? void 0 : _b.length)
? false
: option.autocomplete,
choices: option.choices ||
undefined,
});
}
else if (option.type === discord_js_1.ApplicationCommandOptionType.Boolean) {
optionsArray.push({
name: option.name,
description: option.description,
channelTypes: option.channelTypes || undefined,
descriptionLocalizations: option.descriptionLocalizations || undefined,
nameLocalizations: option.nameLocalizations || undefined,
type: option.type,
required: option.required,
});
}
else {
optionsArray.push({
name: option.name,
description: option.description,
channelTypes: option.channelTypes || undefined,
descriptionLocalizations: option.descriptionLocalizations || undefined,
nameLocalizations: option.nameLocalizations || undefined,
type: option.type,
required: option.required,
});
}
}
else {
optionsArray.push(option);
}
}
return optionsArray;
}
function isReady() {
return that.readyAt instanceof Date;
}
function debugLogger(message) {
if (options.debug) {
const date = new Date();
const time = `${date.getDay().toString().padStart(2, '0')}.${date.getMonth() + 1}.${date.getFullYear()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
console.debug(colors_1.default.grey('[' +
colors_1.default.blue('DEBUG') +
' | ' +
colors_1.default.blue(time) +
'] => ' +
colors_1.default.cyan(message)));
}
}
function validateChoiceName(name) {
if (typeof name !== 'string')
throw new Error('Name must be a string');
if (name.length > 100)
throw new Error('Choice name has to be less than 100 characters');
if (name.length < 1)
throw new Error('Choice name has to be more than 1 characters');
return;
}
function validateChoiceValue(value) {
if (value.toString().length > 100)
throw new Error('Choice value has to be less than 100 characters');
if (value.toString().length < 1)
throw new Error('Choice value has to be more than 1 characters');
return;
}
function waitForReady() {
return __awaiter(this, void 0, void 0, function* () {
if (!isReady()) {
yield new Promise((resolve) => {
const interval = setInterval(() => {
if (isReady()) {
clearInterval(interval);
resolve(null);
}
}, 500);
});
}
else {
return;
}
});
}