detritus-client
Version:
A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.
253 lines (252 loc) • 9.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationCommandPermission = exports.ApplicationCommandPermissions = exports.ApplicationCommandOptionChoice = exports.ApplicationCommandOption = exports.ApplicationCommand = void 0;
const Crypto = require("crypto");
const basecollection_1 = require("../collections/basecollection");
const baseset_1 = require("../collections/baseset");
const constants_1 = require("../constants");
const basestructure_1 = require("./basestructure");
const keysApplicationCommand = new baseset_1.BaseSet([
constants_1.DiscordKeys.APPLICATION_ID,
constants_1.DiscordKeys.DEFAULT_PERMISSION,
constants_1.DiscordKeys.DESCRIPTION,
constants_1.DiscordKeys.GUILD_ID,
constants_1.DiscordKeys.ID,
constants_1.DiscordKeys.NAME,
constants_1.DiscordKeys.OPTIONS,
constants_1.DiscordKeys.TYPE,
constants_1.DiscordKeys.VERSION,
]);
/**
* Application Command Structure
* @category Structure
*/
class ApplicationCommand extends basestructure_1.BaseStructure {
constructor(client, data, isClone) {
super(client, undefined, isClone);
this._keys = keysApplicationCommand;
this.applicationId = '';
this.defaultPermission = true;
this.description = '';
this.id = '';
this.name = '';
this.type = constants_1.ApplicationCommandTypes.CHAT_INPUT;
this.version = '';
this.merge(data);
}
get _optionsKey() {
return (this.options) ? this.options.map((x) => x.key).join(':') : '';
}
get hash() {
return Crypto.createHash('md5').update(this.key).digest('hex');
}
get key() {
return `${this.name}-${this.description}-${this.type}-${this._optionsKey}`;
}
edit(options) {
if (this.guildId) {
return this.client.rest.editApplicationGuildCommand(this.client.clientId, this.guildId, this.id, options);
}
return this.client.rest.editApplicationCommand(this.client.clientId, this.id, options);
}
delete() {
if (this.guildId) {
return this.client.rest.deleteApplicationGuildCommand(this.client.clientId, this.guildId, this.id);
}
return this.client.rest.deleteApplicationCommand(this.client.clientId, this.id);
}
mergeValue(key, value) {
switch (key) {
case constants_1.DiscordKeys.OPTIONS:
{
if (value) {
if (!this.options) {
this.options = new basecollection_1.BaseCollection();
}
this.options.clear();
for (let raw of value) {
const option = new ApplicationCommandOption(this, raw, this.isClone);
this.options.set(option.name, option);
}
}
else {
this.options = undefined;
}
}
;
return;
}
super.mergeValue(key, value);
}
}
exports.ApplicationCommand = ApplicationCommand;
const keysApplicationCommandOption = new baseset_1.BaseSet([
constants_1.DiscordKeys.CHOICES,
constants_1.DiscordKeys.DESCRIPTION,
constants_1.DiscordKeys.NAME,
constants_1.DiscordKeys.OPTIONS,
constants_1.DiscordKeys.REQUIRED,
constants_1.DiscordKeys.TYPE,
]);
/**
* Application Command Option Structure
* @category Structure
*/
class ApplicationCommandOption extends basestructure_1.BaseStructure {
constructor(command, data, isClone) {
super(command.client, undefined, isClone);
this._keys = keysApplicationCommandOption;
this.description = '';
this.name = '';
this.required = false;
this.type = constants_1.ApplicationCommandOptionTypes.SUB_COMMAND;
this.command = command;
this.merge(data);
Object.defineProperty(this, 'command', { enumerable: false });
}
get _choicesKey() {
return (this.choices) ? this.choices.map((x) => x.key).join(':') : '';
}
get _optionsKey() {
return (this.options) ? this.options.map((x) => x.key).join(':') : '';
}
get key() {
return `${this.name}-${this.description}-${this.type}-${!!this.required}-${this._optionsKey}-${this._choicesKey}`;
}
mergeValue(key, value) {
switch (key) {
case constants_1.DiscordKeys.CHOICES:
{
if (value) {
if (!this.choices) {
this.choices = new basecollection_1.BaseCollection();
}
this.choices.clear();
for (let raw of value) {
const choice = new ApplicationCommandOptionChoice(this, raw, this.isClone);
this.choices.set(choice.name, choice);
}
}
else {
this.choices = undefined;
}
}
;
return;
case constants_1.DiscordKeys.OPTIONS:
{
if (value) {
if (!this.options) {
this.options = new basecollection_1.BaseCollection();
}
this.options.clear();
for (let raw of value) {
const option = new ApplicationCommandOption(this.command, raw, this.isClone);
this.options.set(option.name, option);
}
}
else {
this.options = undefined;
}
}
;
return;
}
super.mergeValue(key, value);
}
}
exports.ApplicationCommandOption = ApplicationCommandOption;
const keysApplicationCommandOptionChoice = new baseset_1.BaseSet([
constants_1.DiscordKeys.NAME,
constants_1.DiscordKeys.VALUE,
]);
/**
* Application Command Option Choice Structure
* @category Structure
*/
class ApplicationCommandOptionChoice extends basestructure_1.BaseStructure {
constructor(option, data, isClone) {
super(option.client, undefined, isClone);
this._keys = keysApplicationCommandOptionChoice;
this.name = '';
this.value = '';
this.option = option;
this.merge(data);
Object.defineProperty(this, 'option', { enumerable: false });
}
get key() {
return `${this.name}-${this.value}-${typeof (this.value)}`;
}
}
exports.ApplicationCommandOptionChoice = ApplicationCommandOptionChoice;
const keysApplicationCommandPermissions = new baseset_1.BaseSet([
constants_1.DiscordKeys.APPLICATION_ID,
constants_1.DiscordKeys.GUILD_ID,
constants_1.DiscordKeys.ID,
constants_1.DiscordKeys.PERMISSIONS,
]);
/**
* Application Command Permissions Structure
* @category Structure
*/
class ApplicationCommandPermissions extends basestructure_1.BaseStructure {
constructor(client, data, isClone) {
super(client, undefined, isClone);
this._keys = keysApplicationCommandPermissions;
this.applicationId = '';
this.guildId = '';
this.id = '';
this.permissions = new basecollection_1.BaseCollection();
this.merge(data);
}
mergeValue(key, value) {
switch (key) {
case constants_1.DiscordKeys.PERMISSIONS:
{
this.permissions.clear();
for (let raw of value) {
const permission = new ApplicationCommandPermission(this, raw, this.isClone);
this.permissions.set(permission.id, permission);
}
}
;
return;
}
super.mergeValue(key, value);
}
}
exports.ApplicationCommandPermissions = ApplicationCommandPermissions;
const keysApplicationCommandPermission = new baseset_1.BaseSet([
constants_1.DiscordKeys.ID,
constants_1.DiscordKeys.PERMISSION,
constants_1.DiscordKeys.TYPE,
]);
/**
* Application Command Permission Structure
* @category Structure
*/
class ApplicationCommandPermission extends basestructure_1.BaseStructure {
constructor(commandPermissions, data, isClone) {
super(commandPermissions.client, undefined, isClone);
this._keys = keysApplicationCommandPermission;
this.id = '';
this.permission = false;
this.type = constants_1.ApplicationCommandPermissionTypes.ROLE;
this.commandPermissions = commandPermissions;
this.merge(data);
Object.defineProperty(this, 'commandPermissions', { enumerable: false });
}
get isRole() {
return this.type === constants_1.ApplicationCommandPermissionTypes.ROLE;
}
get isUser() {
return this.type === constants_1.ApplicationCommandPermissionTypes.USER;
}
get user() {
if (this.isUser) {
return this.client.users.get(this.id) || null;
}
return null;
}
}
exports.ApplicationCommandPermission = ApplicationCommandPermission;