feat: return-based interaction reply edits

This commit is contained in:
TresAbhi committed 2023-06-16 02:01:06 +00:00
1 parent fbcd9e82e3
commit 4d9dc067af
19 files changed
+249 -306

No files matched your search

+50 -18
View File
@@ -5,7 +5,9 @@ import {
ButtonStyle,
CacheType,
ChatInputCommandInteraction,
EmbedBuilder,
Interaction,
InteractionEditReplyOptions,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
@@ -30,36 +32,44 @@ import negativeEmbed from '../core/interaction/negativeEmbed.js';
import { DISCORD_TOKEN } from '../core/process/args.js';
import getClientId from '../core/process/getClientId.js';
import isDev from '../core/process/isDev.js';
import render from '../core/ui/render.js';
import { handleError } from './error.js';
export type CommandReturnable = EmbedBuilder | ButtonBuilder | JSX.Element;
export type RichCommandReturnable = CommandReturnable | CommandReturnable[];
export interface CommandRegistry {
inDevelopment: boolean;
inProduction: boolean;
inPublic: boolean;
command: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
execute: (
interaction: ChatInputCommandInteraction<CacheType>,
) => RichCommandReturnable | Promise<RichCommandReturnable>;
autocomplete?: (interaction: AutocompleteInteraction<CacheType>) => void;
}
const rest = new REST().setToken(DISCORD_TOKEN);
const commands = [
debug,
eligible,
help,
inactive,
ownedtanks,
playerachievements,
playerinfo,
searchclans,
searchplayers,
searchtanks,
stats,
tankstats,
today,
verify,
].reduce<Record<string, CommandRegistry>>((accumulator, registry) => {
const commands = (
[
debug,
eligible,
help,
inactive,
ownedtanks,
playerachievements,
playerinfo,
searchclans,
searchplayers,
searchtanks,
stats,
tankstats,
today,
verify,
] as CommandRegistry[]
).reduce<Record<string, CommandRegistry>>((accumulator, registry) => {
return { ...accumulator, [registry.command.name]: registry };
}, {});
@@ -138,7 +148,29 @@ export default async function interactionCreate(
await interaction.deferReply();
try {
await command.execute(interaction);
const result = await command.execute(interaction);
const reply: InteractionEditReplyOptions = {};
await Promise.all(
(Array.isArray(result) ? result : [result]).map(async (item) => {
if (item instanceof EmbedBuilder) {
if (!reply.embeds) reply.embeds = [];
reply.embeds.push(item);
} else if (item instanceof ButtonBuilder) {
if (!reply.components)
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
(
reply.components[0] as ActionRowBuilder<ButtonBuilder>
).addComponents(item);
} else {
const image = await render(item);
if (!reply.files) reply.files = [];
reply.files.push(image);
}
}),
);
await interaction.editReply(reply);
} catch (error) {
const actionRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()