@jadl/cmd
Version:
A command handler for JADL
55 lines (54 loc) • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComponentRunner = void 0;
const builders_1 = require("@jadl/builders");
const v9_1 = require("discord-api-types/v9");
const CommandHandler_1 = require("../handler/CommandHandler");
const InteractionCommandResponse_1 = require("../structures/InteractionCommandResponse");
const WorkerInject_1 = require("../structures/WorkerInject");
class ComponentRunner extends WorkerInject_1.WorkerInject {
constructor(component) {
super();
this.component = component;
}
async _onInteraction(int, worker) {
if (int.type !== v9_1.InteractionType.MessageComponent)
return;
if (int.data.component_type !== this.component.type)
return;
if (int.data.custom_id.split('$')[0] !== this.component.custom_id)
return;
let data;
const [customId, possibleData] = int.data.custom_id.split('$');
int.data.custom_id = customId;
if (possibleData) {
data = Object.fromEntries(new URLSearchParams(possibleData).entries());
}
const res = await this.handle?.(int, worker, data);
if (!res) {
await CommandHandler_1.CommandHandler.callback(worker, { type: v9_1.InteractionResponseType.DeferredMessageUpdate }, int);
}
else if (res instanceof InteractionCommandResponse_1.InteractionCommandResponse) {
await CommandHandler_1.CommandHandler.callback(worker, res.data, int);
}
else {
await CommandHandler_1.CommandHandler.callback(worker, {
type: v9_1.InteractionResponseType.UpdateMessage,
data: (0, builders_1.parseMessage)(res)
}, int);
}
}
render(extraInfo) {
let component = this.component;
if (extraInfo) {
component = JSON.parse(JSON.stringify(component));
component.custom_id += `$${new URLSearchParams(extraInfo).toString()}`;
}
return component;
}
setHandle(handle) {
this.handle = handle;
return this;
}
}
exports.ComponentRunner = ComponentRunner;