@jadl/cmd
Version:
A command handler for JADL
90 lines (89 loc) • 3.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModalRunner = void 0;
const builders_1 = require("@jadl/builders");
const v10_1 = require("discord-api-types/v10");
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");
const createCustomId = (title) => title.split(' ').join('-').toLowerCase();
class ModalRunner extends WorkerInject_1.WorkerInject {
constructor(title, customId = createCustomId(title)) {
super();
this.title = title;
this.customId = customId;
this.data = {
custom_id: '',
components: [],
title: ''
};
this.data.title = title;
this.data.custom_id = customId;
}
setHandle(handle) {
this.handle = handle;
return this;
}
addTextInput(name, data, customId) {
customId = customId || createCustomId(name);
this.data.components.push({
type: v10_1.ComponentType.ActionRow,
components: [
{
type: v10_1.ComponentType.TextInput,
custom_id: customId,
label: name,
...data
}
]
});
return this;
}
async _onInteraction(int, worker) {
if (int.type !== v9_1.InteractionType.ModalSubmit)
return;
if (int.data.custom_id !== this.customId)
return;
const data = int.data.components.map((x) => x.components).flat();
let options = {};
const components = this.data.components.map((x) => x.components).flat();
data.forEach((opt) => {
options[components.find((x) => x.custom_id === opt.custom_id.split('$')[0]).label] = opt.value;
});
const dataComponent = int.data.components[0]?.components?.[0];
if (dataComponent) {
const [customId, possibleData] = dataComponent.custom_id.split('$');
dataComponent.custom_id = customId;
if (possibleData) {
const obj = Object.fromEntries(new URLSearchParams(possibleData).entries());
options = { ...options, ...obj };
}
}
const res = await this.handle?.(options, worker, int);
if (!res) {
await CommandHandler_1.CommandHandler.callback(worker, { type: v10_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: v10_1.InteractionResponseType.ChannelMessageWithSource,
data: (0, builders_1.parseMessage)(res)
}, int);
}
}
render(extraInfo) {
let data = this.data;
if (extraInfo) {
data = JSON.parse(JSON.stringify(this.data));
data.components[0].components[0].custom_id += `$${new URLSearchParams(extraInfo).toString()}`;
}
return new InteractionCommandResponse_1.InteractionCommandResponse({
type: v10_1.InteractionResponseType.Modal,
data: data
});
}
}
exports.ModalRunner = ModalRunner;