mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 03:18:17 +00:00
hopefully the last dir change
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
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";
|
||||
|
||||
/** Batch edits permissions for all commands in a guild. Takes an array of partial GuildApplicationCommandPermissions objects including `id` and `permissions`. */
|
||||
export async function batchEditSlashCommandPermissions(
|
||||
guildId: bigint,
|
||||
options: { id: string; permissions: ApplicationCommandPermissions[] }[],
|
||||
) {
|
||||
return await rest.runMethod(
|
||||
"put",
|
||||
endpoints.COMMANDS_PERMISSIONS(applicationId, guildId),
|
||||
snakelize(options),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
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";
|
||||
|
||||
/**
|
||||
* 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:
|
||||
*
|
||||
* - Your app **cannot** have two global commands with the same name
|
||||
* - Your app **cannot** have two guild commands within the same name **on the same guild**
|
||||
* - Your app **can** have a global and guild command with the same name
|
||||
* - Multiple apps **can** have commands with the same names
|
||||
*
|
||||
* 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,
|
||||
) {
|
||||
validateSlashCommands([options], true);
|
||||
|
||||
return await rest.runMethod<ApplicationCommand>(
|
||||
"post",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD(applicationId, guildId)
|
||||
: endpoints.COMMANDS(applicationId),
|
||||
snakelize(options),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
|
||||
/** Deletes a slash command. */
|
||||
export async function deleteSlashCommand(
|
||||
id: bigint,
|
||||
guildId?: bigint,
|
||||
) {
|
||||
return await rest.runMethod<undefined>(
|
||||
"delete",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, id)
|
||||
: endpoints.COMMANDS_ID(applicationId, id),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
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?: bigint,
|
||||
) {
|
||||
return await rest.runMethod<undefined>(
|
||||
"delete",
|
||||
messageId
|
||||
? endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(
|
||||
applicationId,
|
||||
token,
|
||||
messageId,
|
||||
)
|
||||
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
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";
|
||||
|
||||
/** Edits command permissions for a specific command for your application in a guild. */
|
||||
export async function editSlashCommandPermissions(
|
||||
guildId: bigint,
|
||||
commandId: bigint,
|
||||
options: ApplicationCommandPermissions[],
|
||||
) {
|
||||
return await rest.runMethod(
|
||||
"put",
|
||||
endpoints.COMMANDS_PERMISSION(applicationId, guildId, commandId),
|
||||
{ permissions: snakelize(options) },
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
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";
|
||||
|
||||
/** 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,
|
||||
) {
|
||||
if (options.content && options.content.length > 2000) {
|
||||
throw Error(Errors.MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
if (options.embeds && options.embeds.length > 10) {
|
||||
options.embeds.splice(10);
|
||||
}
|
||||
|
||||
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.allowedMentions.users.length > 100) {
|
||||
options.allowedMentions.users = options.allowedMentions.users.slice(
|
||||
0,
|
||||
100,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.allowedMentions.roles?.length) {
|
||||
if (
|
||||
options.allowedMentions.parse?.includes(
|
||||
DiscordAllowedMentionsTypes.RoleMentions,
|
||||
)
|
||||
) {
|
||||
options.allowedMentions.parse = options.allowedMentions.parse.filter(
|
||||
(p) => p !== "roles",
|
||||
);
|
||||
}
|
||||
|
||||
if (options.allowedMentions.roles.length > 100) {
|
||||
options.allowedMentions.roles = options.allowedMentions.roles.slice(
|
||||
0,
|
||||
100,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await rest.runMethod(
|
||||
"patch",
|
||||
options.messageId
|
||||
? endpoints.WEBHOOK_MESSAGE(applicationId, token, options.messageId)
|
||||
: endpoints.INTERACTION_ORIGINAL_ID_TOKEN(applicationId, token),
|
||||
options,
|
||||
);
|
||||
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommand } from "../../../types/interactions/commands/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. */
|
||||
export async function getSlashCommand(commandId: bigint, guildId?: bigint) {
|
||||
return await rest.runMethod<ApplicationCommand>(
|
||||
"get",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, commandId)
|
||||
: endpoints.COMMANDS_ID(applicationId, commandId),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
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";
|
||||
|
||||
/** 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>(
|
||||
"get",
|
||||
endpoints.COMMANDS_PERMISSION(applicationId, guildId, commandId),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
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";
|
||||
|
||||
/** 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[]>(
|
||||
"get",
|
||||
endpoints.COMMANDS_PERMISSIONS(applicationId, guildId),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { applicationId } from "../../../bot.ts";
|
||||
import { rest } from "../../../rest/rest.ts";
|
||||
import type { ApplicationCommand } from "../../../types/interactions/commands/application_command.ts";
|
||||
import { Collection } from "../../../util/collection.ts";
|
||||
import { endpoints } from "../../../util/constants.ts";
|
||||
|
||||
/** Fetch all of the global commands for your application. */
|
||||
export async function getSlashCommands(guildId?: bigint) {
|
||||
const result = await rest.runMethod<ApplicationCommand[]>(
|
||||
"get",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD(applicationId, guildId)
|
||||
: endpoints.COMMANDS(applicationId),
|
||||
);
|
||||
|
||||
return new Collection(result.map((command) => [command.name, command]));
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
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";
|
||||
|
||||
/**
|
||||
* 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,
|
||||
) {
|
||||
validateSlashCommands([options]);
|
||||
|
||||
return await rest.runMethod<ApplicationCommand>(
|
||||
"patch",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD_ID(applicationId, guildId, commandId)
|
||||
: endpoints.COMMANDS_ID(applicationId, commandId),
|
||||
options,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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";
|
||||
|
||||
/**
|
||||
* Bulk edit existing slash commands. If a command does not exist, it will create it.
|
||||
*
|
||||
* **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: EditGlobalApplicationCommand[],
|
||||
guildId?: bigint,
|
||||
) {
|
||||
validateSlashCommands(options);
|
||||
|
||||
return await rest.runMethod<ApplicationCommand[]>(
|
||||
"put",
|
||||
guildId
|
||||
? endpoints.COMMANDS_GUILD(applicationId, guildId)
|
||||
: endpoints.COMMANDS(applicationId),
|
||||
options,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user