-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
upsertGuildApplicationCommands.ts
39 lines (37 loc) · 1.65 KB
/
upsertGuildApplicationCommands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import type { Bot } from "../../../bot.ts";
import { ApplicationCommand } from "../../../transformers/applicationCommand.ts";
import { BigString, 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: BigString,
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];
}),
);
}