mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 03:18:17 +00:00
Interactions helpers
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
import type { ApplicationCommandPermissions } from "../../../types/interactions/commands/application_command_permissions.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import { snakelize } from "../../../util/utils.ts";
|
||||
|
||||
/** Batch edits permissions for all commands in a guild. Takes an array of partial GuildApplicationCommandPermissions objects including `id` and `permissions`. */
|
||||
export async function batchEditSlashCommandPermissions(
|
||||
bot: Bot,
|
||||
guildId: bigint,
|
||||
options: { id: string; permissions: ApplicationCommandPermissions[] }[]
|
||||
) {
|
||||
return await rest.runMethod("put", endpoints.COMMANDS_PERMISSIONS(applicationId, guildId), snakelize(options));
|
||||
return await bot.rest.runMethod(bot.rest,"put", bot.constants.endpoints.COMMANDS_PERMISSIONS(bot.applicationId, guildId), options);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommand } from "../../../types/interactions/commands/application_command.ts";
|
||||
import type { CreateGlobalApplicationCommand } from "../../../types/interactions/commands/create_global_application_command.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import { snakelize, validateSlashCommands } from "../../../util/utils.ts";
|
||||
import type {Bot} from "../../../bot.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:
|
||||
@@ -16,12 +13,13 @@ import { snakelize, 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: CreateGlobalApplicationCommand, guildId?: bigint) {
|
||||
[options] = validateSlashCommands([options], true) as CreateGlobalApplicationCommand[];
|
||||
export async function createSlashCommand(bot: Bot, options: CreateGlobalApplicationCommand, guildId?: bigint) {
|
||||
[options] = bot.utils.validateSlashCommands([options], true) as CreateGlobalApplicationCommand[];
|
||||
|
||||
return await rest.runMethod<ApplicationCommand>(
|
||||
return await bot.rest.runMethod<ApplicationCommand>(
|
||||
bot.rest,
|
||||
"post",
|
||||
guildId ? endpoints.COMMANDS_GUILD(applicationId, guildId) : endpoints.COMMANDS(applicationId),
|
||||
snakelize(options)
|
||||
guildId ? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId) : bot.constants.endpoints.COMMANDS(bot.applicationId),
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import type { Bot} from "../../../bot.ts";
|
||||
|
||||
/** Deletes a slash command. */
|
||||
export async function deleteSlashCommand(id: bigint, guildId?: bigint) {
|
||||
return await rest.runMethod<undefined>(
|
||||
export async function deleteSlashCommand(bot: Bot, id: bigint, guildId?: bigint) {
|
||||
return await bot.rest.runMethod<undefined>(
|
||||
bot.rest,
|
||||
"delete",
|
||||
guildId ? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, id) : endpoints.COMMANDS_ID(applicationId, id)
|
||||
guildId ? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, id) : bot.constants.endpoints.COMMANDS_ID(bot.applicationId, id)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import type {Bot} from "../../../bot.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?: bigint) {
|
||||
return await rest.runMethod<undefined>(
|
||||
export async function deleteSlashResponse(bot: Bot, token: string, messageId?: bigint) {
|
||||
return await bot.rest.runMethod<undefined>(
|
||||
bot.rest,
|
||||
"delete",
|
||||
messageId
|
||||
? endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(applicationId, token, messageId)
|
||||
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token)
|
||||
? bot.constants.endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(bot.applicationId, token, messageId)
|
||||
: bot.constants.endpoints.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommandPermissions } from "../../../types/interactions/commands/application_command_permissions.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import { snakelize } from "../../../util/utils.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
|
||||
/** Edits command permissions for a specific command for your application in a guild. */
|
||||
export async function editSlashCommandPermissions(
|
||||
bot: Bot,
|
||||
guildId: bigint,
|
||||
commandId: bigint,
|
||||
options: ApplicationCommandPermissions[]
|
||||
) {
|
||||
return await rest.runMethod("put", endpoints.COMMANDS_PERMISSION(applicationId, guildId, commandId), {
|
||||
permissions: snakelize(options),
|
||||
return await bot.rest.runMethod(bot.rest,"put", bot.constants.endpoints.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId), {
|
||||
permissions: options,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import { structures } from "../../../structures/mod.ts";
|
||||
import type { DiscordenoEditWebhookMessage } from "../../../types/discordeno/edit_webhook_message.ts";
|
||||
import { Errors } from "../../../types/discordeno/errors.ts";
|
||||
import { DiscordAllowedMentionsTypes } from "../../../types/messages/allowed_mentions_types.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import { snakelize, validateComponents } from "../../../util/utils.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
import {DiscordAllowedMentionsTypes} from "../../../types/messages/allowed_mentions_types.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: DiscordenoEditWebhookMessage) {
|
||||
export async function editSlashResponse(bot: Bot, token: string, options: DiscordenoEditWebhookMessage) {
|
||||
if (options.content && options.content.length > 2000) {
|
||||
throw Error(Errors.MESSAGE_MAX_LENGTH);
|
||||
throw Error(bot.constants.Errors.MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
if (options.components?.length) {
|
||||
validateComponents(options.components);
|
||||
bot.utils.validateComponents(bot, options.components);
|
||||
}
|
||||
|
||||
if (options.embeds && options.embeds.length > 10) {
|
||||
@@ -43,17 +38,31 @@ export async function editSlashResponse(token: string, options: DiscordenoEditWe
|
||||
}
|
||||
}
|
||||
|
||||
const result = await rest.runMethod(
|
||||
const result = await bot.rest.runMethod(
|
||||
bot.rest,
|
||||
"patch",
|
||||
options.messageId
|
||||
? endpoints.WEBHOOK_MESSAGE(applicationId, token, options.messageId)
|
||||
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token),
|
||||
snakelize(options)
|
||||
? bot.constants.endpoints.WEBHOOK_MESSAGE(bot.applicationId, token, options.messageId)
|
||||
: bot.constants.endpoints.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
|
||||
{
|
||||
content: options.content,
|
||||
embeds: options.embeds,
|
||||
file: options.file,
|
||||
allowed_mentions: options.allowedMentions ? {
|
||||
parse: options.allowedMentions.parse,
|
||||
roles: options.allowedMentions.roles,
|
||||
users: options.allowedMentions.users,
|
||||
replied_user: options.allowedMentions.repliedUser
|
||||
} : undefined,
|
||||
attachments: options.attachments,
|
||||
// TODO: Snakelize components??
|
||||
components: options.components,
|
||||
message_id: options.messageId,
|
||||
}
|
||||
);
|
||||
|
||||
// If the original message was edited, this will not return a message
|
||||
if (!options.messageId) return result as undefined;
|
||||
|
||||
const message = await structures.createDiscordenoMessage(result);
|
||||
return message;
|
||||
return bot.transformers.message(result);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommand } from "../../../types/interactions/commands/application_command.ts";
|
||||
import { snowflakeToBigint } from "../../../util/bigint.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
|
||||
/** Fetchs the global command for the given Id. If a guildId is provided, the guild command will be fetched. */
|
||||
export async function getSlashCommand(commandId: bigint, guildId?: bigint) {
|
||||
const result = await rest.runMethod<ApplicationCommand>(
|
||||
/** Fetches the global command for the given Id. If a guildId is provided, the guild command will be fetched. */
|
||||
export async function getSlashCommand(bot: Bot, commandId: bigint, guildId?: bigint) {
|
||||
const result = await bot.rest.runMethod<ApplicationCommand>(
|
||||
"get",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, commandId)
|
||||
: endpoints.COMMANDS_ID(applicationId, commandId)
|
||||
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, commandId)
|
||||
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, commandId)
|
||||
);
|
||||
|
||||
return {
|
||||
...result,
|
||||
id: snowflakeToBigint(result.id),
|
||||
applicationId: snowflakeToBigint(result.applicationId),
|
||||
id: bot.transformers.snowflake(result.id),
|
||||
applicationId: bot.transformers.snowflake(result.applicationId),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { GuildApplicationCommandPermissions } from "../../../types/interactions/commands/guild_application_command_permissions.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
|
||||
/** Fetches command permissions for a specific command for your application in a guild. Returns a GuildApplicationCommandPermissions object. */
|
||||
export async function getSlashCommandPermission(guildId: bigint, commandId: bigint) {
|
||||
return await rest.runMethod<GuildApplicationCommandPermissions>(
|
||||
export async function getSlashCommandPermission(bot: Bot, guildId: bigint, commandId: bigint) {
|
||||
return await bot.rest.runMethod<GuildApplicationCommandPermissions>(
|
||||
bot.rest,
|
||||
"get",
|
||||
endpoints.COMMANDS_PERMISSION(applicationId, guildId, commandId)
|
||||
bot.constants.endpoints.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
import type { GuildApplicationCommandPermissions } from "../../../types/interactions/commands/guild_application_command_permissions.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
|
||||
/** Fetches command permissions for all commands for your application in a guild. Returns an array of GuildApplicationCommandPermissions objects. */
|
||||
export async function getSlashCommandPermissions(guildId: bigint) {
|
||||
return await rest.runMethod<GuildApplicationCommandPermissions[]>(
|
||||
export async function getSlashCommandPermissions(bot: Bot, guildId: bigint) {
|
||||
return await bot.rest.runMethod<GuildApplicationCommandPermissions[]>(
|
||||
bot.rest,
|
||||
"get",
|
||||
endpoints.COMMANDS_PERMISSIONS(applicationId, guildId)
|
||||
bot.constants.endpoints.COMMANDS_PERMISSIONS(bot.applicationId, guildId)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommand } from "../../../types/interactions/commands/application_command.ts";
|
||||
import { snowflakeToBigint } from "../../../util/bigint.ts";
|
||||
import { Collection } from "../../../util/collection.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
|
||||
/** Fetch all of the global commands for your application. */
|
||||
export async function getSlashCommands(guildId?: bigint) {
|
||||
const result = await rest.runMethod<ApplicationCommand[]>(
|
||||
/** Fetch all the global commands for your application. */
|
||||
export async function getSlashCommands(bot: Bot, guildId?: bigint) {
|
||||
const result = await bot.rest.runMethod<ApplicationCommand[]>(
|
||||
bot.rest,
|
||||
"get",
|
||||
guildId ? endpoints.COMMANDS_GUILD(applicationId, guildId) : endpoints.COMMANDS(applicationId)
|
||||
guildId ? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId) : bot.constants.endpoints.COMMANDS(bot.applicationId)
|
||||
);
|
||||
|
||||
return new Collection(
|
||||
result.map((command) => [
|
||||
command.name,
|
||||
{ ...command, id: snowflakeToBigint(command.id), applicationId: snowflakeToBigint(command.applicationId) },
|
||||
{ ...command, id: bot.transformers.snowflake(command.id), applicationId: bot.transformers.snowflake(command.applicationId) },
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommand } from "../../../types/interactions/commands/application_command.ts";
|
||||
import type { EditGlobalApplicationCommand } from "../../../types/interactions/commands/edit_global_application_command.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import { validateSlashCommands } from "../../../util/utils.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
|
||||
/**
|
||||
* Edit an existing slash command. If this command did not exist, it will create it.
|
||||
*/
|
||||
export async function upsertSlashCommand(commandId: bigint, options: EditGlobalApplicationCommand, guildId?: bigint) {
|
||||
[options] = validateSlashCommands([options]);
|
||||
export async function upsertSlashCommand(bot: Bot, commandId: bigint, options: EditGlobalApplicationCommand, guildId?: bigint) {
|
||||
[options] = bot.utils.validateSlashCommands([options]);
|
||||
|
||||
return await rest.runMethod<ApplicationCommand>(
|
||||
return await bot.rest.runMethod<ApplicationCommand>(
|
||||
bot.rest,
|
||||
"patch",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, commandId)
|
||||
: endpoints.COMMANDS_ID(applicationId, commandId),
|
||||
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, commandId)
|
||||
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, commandId),
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommand } from "../../../types/interactions/commands/application_command.ts";
|
||||
import type { EditGlobalApplicationCommand } from "../../../types/interactions/commands/edit_global_application_command.ts";
|
||||
import { MakeRequired } from "../../../types/util.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
import { validateSlashCommands } from "../../../util/utils.ts";
|
||||
import type { MakeRequired } from "../../../types/util.ts";
|
||||
import type {Bot} from "../../../bot.ts";
|
||||
|
||||
/**
|
||||
* Bulk edit existing slash commands. If a command does not exist, it will create it.
|
||||
@@ -12,14 +9,16 @@ 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(
|
||||
bot: Bot,
|
||||
options: MakeRequired<EditGlobalApplicationCommand, "name">[],
|
||||
guildId?: bigint
|
||||
) {
|
||||
options = validateSlashCommands(options) as MakeRequired<EditGlobalApplicationCommand, "name">[];
|
||||
options = bot.utils.validateSlashCommands(options) as MakeRequired<EditGlobalApplicationCommand, "name">[];
|
||||
|
||||
return await rest.runMethod<ApplicationCommand[]>(
|
||||
return await bot.rest.runMethod<ApplicationCommand[]>(
|
||||
bot.rest,
|
||||
"put",
|
||||
guildId ? endpoints.COMMANDS_GUILD(applicationId, guildId) : endpoints.COMMANDS(applicationId),
|
||||
guildId ? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId) : bot.constants.endpoints.COMMANDS(bot.applicationId),
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user