This commit is contained in:
ITOH
2021-04-06 22:25:26 +02:00
parent 484e995140
commit 96d26605ca
20 changed files with 143 additions and 80 deletions
+12 -7
View File
@@ -1,7 +1,11 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { CreateGlobalApplicationCommand } from "../../types/interactions/create_global_application_command.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
import {
camelKeysToSnakeCase,
validateSlashCommands,
} from "../../util/utils.ts";
/**
* There are two kinds of Slash Commands: global commands and guild commands. Global commands are available for every guild that adds your app; guild commands are specific to the guild you specify when making them. Command names are unique per application within each scope (global and guild). That means:
@@ -14,17 +18,18 @@ import { validateSlashCommands } from "../../util/utils.ts";
* Global commands are cached for **1 hour**. That means that new global commands will fan out slowly across all guilds, and will be guaranteed to be updated in an hour.
* Guild commands update **instantly**. We recommend you use guild commands for quick testing, and global commands when they're ready for public use.
*/
export async function createSlashCommand(options: CreateSlashCommandOptions) {
export async function createSlashCommand(
options: CreateGlobalApplicationCommand,
guildId: string,
) {
validateSlashCommands([options], true);
const result = await rest.runMethod(
"post",
options.guildId
? endpoints.COMMANDS_GUILD(applicationId, options.guildId)
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
{
...options,
},
camelKeysToSnakeCase(options),
);
return result;
+10 -6
View File
@@ -3,12 +3,16 @@ import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Deletes a slash command. */
export function deleteSlashCommand(id: string, guildId?: string) {
if (!guildId) {
return rest.runMethod("delete", endpoints.COMMANDS_ID(applicationId, id));
}
return rest.runMethod(
export async function deleteSlashCommand(
id: string,
guildId?: string,
): Promise<undefined> {
const result = await rest.runMethod(
"delete",
endpoints.COMMANDS_GUILD_ID(applicationId, guildId, id),
guildId
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, id)
: endpoints.COMMANDS_ID(applicationId, id),
);
return result;
}
@@ -3,7 +3,10 @@ import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** To delete your response to a slash command. If a message id is not provided, it will default to deleting the original response. */
export async function deleteSlashResponse(token: string, messageId?: string) {
export async function deleteSlashResponse(
token: string,
messageId?: string,
): Promise<undefined> {
const result = await rest.runMethod(
"delete",
messageId
+24 -13
View File
@@ -1,13 +1,16 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordenoEditWebhookMessage } from "../../types/discordeno/edit_webhook_message.ts";
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
import { DiscordMessage } from "../../types/messages/message.ts";
import { Errors } from "../../types/misc/errors.ts";
import { endpoints } from "../../util/constants.ts";
/** To edit your response to a slash command. If a messageId is not provided it will default to editing the original response. */
export async function editSlashResponse(
token: string,
options: EditSlashResponseOptions,
options: DiscordenoEditWebhookMessage,
) {
if (options.content && options.content.length > 2000) {
throw Error(Errors.MESSAGE_MAX_LENGTH);
@@ -17,31 +20,39 @@ export async function editSlashResponse(
options.embeds.splice(10);
}
if (options.allowed_mentions) {
if (options.allowed_mentions.users?.length) {
if (options.allowed_mentions.parse.includes("users")) {
options.allowed_mentions.parse = options.allowed_mentions.parse.filter(
if (options.allowedMentions) {
if (options.allowedMentions.users?.length) {
if (
options.allowedMentions.parse.includes(
DiscordAllowedMentionsTypes.UserMentions,
)
) {
options.allowedMentions.parse = options.allowedMentions.parse.filter(
(p) => p !== "users",
);
}
if (options.allowed_mentions.users.length > 100) {
options.allowed_mentions.users = options.allowed_mentions.users.slice(
if (options.allowedMentions.users.length > 100) {
options.allowedMentions.users = options.allowedMentions.users.slice(
0,
100,
);
}
}
if (options.allowed_mentions.roles?.length) {
if (options.allowed_mentions.parse.includes("roles")) {
options.allowed_mentions.parse = options.allowed_mentions.parse.filter(
if (options.allowedMentions.roles?.length) {
if (
options.allowedMentions.parse.includes(
DiscordAllowedMentionsTypes.RoleMentions,
)
) {
options.allowedMentions.parse = options.allowedMentions.parse.filter(
(p) => p !== "roles",
);
}
if (options.allowed_mentions.roles.length > 100) {
options.allowed_mentions.roles = options.allowed_mentions.roles.slice(
if (options.allowedMentions.roles.length > 100) {
options.allowedMentions.roles = options.allowedMentions.roles.slice(
0,
100,
);
@@ -61,7 +72,7 @@ export async function editSlashResponse(
if (!options.messageId) return result;
const message = await structures.createMessageStruct(
result as MessageCreateOptions,
result as DiscordMessage,
);
return message;
}
+2 -1
View File
@@ -1,5 +1,6 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { ApplicationCommand } from "../../types/interactions/application_command.ts";
import { endpoints } from "../../util/constants.ts";
/** Fetchs the global command for the given Id. If a guildId is provided, the guild command will be fetched. */
@@ -11,5 +12,5 @@ export async function getSlashCommand(commandId: string, guildId?: string) {
: endpoints.COMMANDS_ID(applicationId, commandId),
);
return result as SlashCommand;
return result as ApplicationCommand;
}
+2 -1
View File
@@ -1,5 +1,6 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { ApplicationCommand } from "../../types/interactions/application_command.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
@@ -10,7 +11,7 @@ export async function getSlashCommands(guildId?: string) {
guildId
? endpoints.COMMANDS_GUILD(applicationId, guildId)
: endpoints.COMMANDS(applicationId),
)) as SlashCommand[];
)) as ApplicationCommand[];
return new Collection(result.map((command) => [command.name, command]));
}
@@ -1,6 +1,7 @@
import { applicationId } from "../../bot.ts";
import { cache } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { DiscordenoInteractionResponse } from "../../types/discordeno/interaction_response.ts";
import { endpoints } from "../../util/constants.ts";
/**
@@ -12,7 +13,7 @@ import { endpoints } from "../../util/constants.ts";
export async function sendInteractionResponse(
id: string,
token: string,
options: SlashCommandResponseOptions,
options: DiscordenoInteractionResponse,
) {
// If its already been executed, we need to send a followup response
if (cache.executedSlashCommands.has(token)) {
@@ -30,12 +31,12 @@ export async function sendInteractionResponse(
// If the user wants this as a private message mark it ephemeral
if (options.private) {
options.data.flags = 64;
options.data = { ...options.data, flags: 64 };
}
// If no mentions are provided, force disable mentions
if (!options.data.allowed_mentions) {
options.data.allowed_mentions = { parse: [] };
if (!options.data?.allowedMentions) {
options.data = { ...options.data, allowedMentions: { parse: [] } };
}
const result = await rest.runMethod(
+2 -1
View File
@@ -1,5 +1,6 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { EditGlobalApplicationCommand } from "../../types/interactions/edit_global_application_command.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
@@ -8,7 +9,7 @@ import { validateSlashCommands } from "../../util/utils.ts";
*/
export async function upsertSlashCommand(
commandId: string,
options: UpsertSlashCommandOptions,
options: EditGlobalApplicationCommand,
guildId?: string,
) {
validateSlashCommands([options]);
@@ -1,5 +1,6 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { EditGlobalApplicationCommand } from "../../types/interactions/edit_global_application_command.ts";
import { endpoints } from "../../util/constants.ts";
import { validateSlashCommands } from "../../util/utils.ts";
@@ -9,7 +10,7 @@ import { validateSlashCommands } from "../../util/utils.ts";
* **NOTE:** Any slash commands that are not specified in this function will be **deleted**. If you don't provide the commandId and rename your command, the command gets a new Id.
*/
export async function upsertSlashCommands(
options: UpsertSlashCommandsOptions[],
options: EditGlobalApplicationCommand[],
guildId?: string,
) {
validateSlashCommands(options);