Files
discordeno/helpers/interactions/commands/upsertGuildApplicationCommands.ts
T
Dorian OszczędaandSkillz4Killz ec6b7459bd fix: Invalid HTTP method in upserting commands. (#2448)
* fix: Export of `ApplicationCommandOptionChoice`.

* fix: Invalid HTTP method for upserting application commands.

* fmt: Run Deno formatter.

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
2022-09-09 19:43:32 -05:00

40 lines
1.6 KiB
TypeScript

import type { Bot } from "../../../bot.ts";
import { ApplicationCommand } from "../../../transformers/applicationCommand.ts";
import { CreateApplicationCommand, DiscordApplicationCommand } from "../../../types/mod.ts";
import { Collection } from "../../../util/collection.ts";
/**
* Re-registers the list of application commands registered in a guild, overwriting the previous commands completely.
*
* @param bot - The bot instance to use to make the request.
* @param guildId - The ID of the guild whose list of commands to overwrite.
* @param commands - The list of commands to use to overwrite the previous list.
* @returns A collection of {@link ApplicationCommand} objects assorted by command ID.
*
* @remarks
* ❗ Commands that are not present in the `commands` array will be __deleted__.
*
* ⚠️ Commands that do not already exist will count towards the daily limit of _200_ new commands.
*
* @see {@link https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-guild-application-commands}
*/
export async function upsertGuildApplicationCommands(
bot: Bot,
guildId: bigint,
commands: CreateApplicationCommand[],
): Promise<Collection<bigint, ApplicationCommand>> {
const results = await bot.rest.runMethod<DiscordApplicationCommand[]>(
bot.rest,
"PUT",
bot.constants.routes.COMMANDS_GUILD(bot.applicationId, guildId),
commands.map((command) => bot.transformers.reverse.createApplicationCommand(bot, command)),
);
return new Collection(
results.map((result) => {
const command = bot.transformers.applicationCommand(bot, result);
return [command.id, command];
}),
);
}