@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
68 lines (66 loc) • 2.47 kB
JavaScript
import { Events } from "../types/Events.mjs";
import { InteractionHandler, InteractionHandlerTypes } from "./InteractionHandler.mjs";
import { Store } from "@sapphire/pieces";
import { Result } from "@sapphire/result";
//#region src/lib/structures/InteractionHandlerStore.ts
var InteractionHandlerStore = class extends Store {
constructor() {
super(InteractionHandler, { name: "interaction-handlers" });
}
async run(interaction) {
if (this.size === 0) return false;
const promises = [];
for (const handler of this.values()) {
if (!InteractionHandlerFilters.get(handler.interactionHandlerType)?.(interaction)) continue;
(await Result.fromAsync(() => handler.parse(interaction))).match({
ok: (option) => {
this.container.client.emit(Events.InteractionHandlerParseSuccess, option, {
interaction,
handler
});
option.match({
some: (value) => {
this.container.client.emit(Events.InteractionHandlerParseSome, option, {
interaction,
handler,
value
});
const promise = Result.fromAsync(() => handler.run(interaction, value)).then((res) => res.mapErr((error) => ({
handler,
error
})));
promises.push(promise);
},
none: () => this.container.client.emit(Events.InteractionHandlerParseNone, option, {
interaction,
handler
})
});
},
err: (error) => {
this.container.client.emit(Events.InteractionHandlerParseError, error, {
interaction,
handler
});
}
});
}
if (promises.length === 0) return false;
const results = await Promise.allSettled(promises);
for (const result of results) result.value.inspectErr((value) => this.container.client.emit(Events.InteractionHandlerError, value.error, {
interaction,
handler: value.handler
}));
return true;
}
};
const InteractionHandlerFilters = new Map([
[InteractionHandlerTypes.Button, (interaction) => interaction.isButton()],
[InteractionHandlerTypes.SelectMenu, (interaction) => interaction.isAnySelectMenu()],
[InteractionHandlerTypes.ModalSubmit, (interaction) => interaction.isModalSubmit()],
[InteractionHandlerTypes.MessageComponent, (interaction) => interaction.isMessageComponent()],
[InteractionHandlerTypes.Autocomplete, (Interaction) => Interaction.isAutocomplete()]
]);
//#endregion
export { InteractionHandlerFilters, InteractionHandlerStore };
//# sourceMappingURL=InteractionHandlerStore.mjs.map