@wilcosp/rex
Version:
Rex is an automated command manager for discord js
70 lines (69 loc) • 2.46 kB
JavaScript
/*!
* 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 { z } from "zod";
import { RexApplicationCommandBase } from "../applicationCommandBase.js";
import { RexMessageContextmenuInteraction } from "../interactions/context/message.js";
import { RexUserContextmenuInteraction } from "../interactions/context/user.js";
export class RexContextMenuCommand extends RexApplicationCommandBase {
constructor(name, type, description = "") {
super(name, description, type);
this.name = name;
}
get commandInfo() {
const ref = this._commandInfo?.deref();
if (ref) {
return ref;
}
return {
id: this._commandId,
description: this._description,
name: this.name,
type: this._type,
nsfw: this.nsfw,
nameLocalizations: this.nameLocals,
descriptionLocalizations: this.descriptionLocals,
};
}
setExecute(fun) {
super.setExecute(fun);
return this;
}
toCommand() {
if (!this._execute) {
throw Error(`execute function of contextmenu command ${this.name} is missing`);
}
return super.toCommand();
}
run(inter) {
if (this._execute) {
return this._execute.apply(this.commandInfo, [
createInteraction(inter, {
autoDefer: this.autoDefer,
deferEphemeral: this.deferEphemeral,
debounceDelay: this.debounceDelay,
auditDelay: this.auditDelay,
useFailOver: this.useFailover,
}),
]);
}
}
static checkName(name) {
z.string()
.regex(/^[ \p{L}\p{N}\p{sc=Deva}\p{sc=Thai}_-]{1,32}$/u, `${name} is not valid to the regex /^[ \p{L}\p{N}\p{sc=Deva}\p{sc=Thai}_-]{1,32}/u`)
.parse(name);
}
static checkDescription(name, description) {
}
}
function createInteraction(inter, opts) {
if (inter.isUserContextMenuCommand()) {
return new RexUserContextmenuInteraction(inter, opts);
}
if (inter.isMessageContextMenuCommand()) {
return new RexMessageContextmenuInteraction(inter, opts);
}
throw new Error("context menu interactio not supported");
}