UNPKG

@wilcosp/rex

Version:

Rex is an automated command manager for discord js

94 lines (93 loc) 2.82 kB
/*! * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { ButtonStyle } from "discord-api-types/v10"; import { ButtonBuilder, isJSONEncodable } from "discord.js"; import { RexButtonInteraction } from "../interactions/components/button.js"; import { checkCustomId } from "../multiUse/component.js"; export class RexButtonComponent extends ButtonBuilder { constructor(data) { if (typeof data == "string") { data = { custom_id: data, }; } checkCustomId(data.customId ?? data.custom_id); super(data); this.deferEphemeral = false; } get customId() { return this.data.custom_id; } toJSON() { return super.toJSON(); } setStyle(style) { if (style == ButtonStyle.Link) { style = ButtonStyle.Primary; } return super.setStyle(style); } setExecute(fun) { this.execute = fun; return this; } setAutoDefer(defer, ephemeral = false) { this.autoDefer = defer; this.deferEphemeral = ephemeral; return this; } setDebounceDelay(delay) { this.debounceDelay = delay; return this; } setAuditDelay(delay) { this.auditDelay = delay; return this; } setUseFailOver(value) { this.useFailover = value; return this; } run(inter) { if (this.execute) { return this.execute.apply(null, [ new RexButtonInteraction(inter, { autoDefer: this.autoDefer, deferEphemeral: this.deferEphemeral, debounceDelay: this.debounceDelay, auditDelay: this.auditDelay, useFailOver: this.useFailover, }), ]); } } setURL() { console.warn("setURL not supported in RexButtonComponent"); return this; } copy() { const button = new RexButtonComponent(this.toJSON()).setExecute(this.execute); if (this.debounceDelay) { button.setDebounceDelay(this.debounceDelay); } if (this.auditDelay) { button.setAuditDelay(this.auditDelay); } if (this.autoDefer) { button.setAutoDefer(this.autoDefer, this.deferEphemeral); } if (this.useFailover) { button.setUseFailOver(this.useFailover); } return button; } static from(other) { if (isJSONEncodable(other)) { return new RexButtonComponent(other.toJSON()); } return new RexButtonComponent(other); } }