mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
feat: Add @discordjs/core (#8736)
* feat: add @discordjs/core * chore: lint * chore: add all gateway events * chore: add the rest of the rest routes * chore: cleanup gateway * chore: rename gateway to client * chore: rename gateway to client * fix: don't spread unless we need to * refactor: use classes and make requested changes * chore: show shardId on emit * chore: add interface for intrinsic props * refactor: scope dispatch data instead of spreading * chore: add utility for uploading files for messages and interactions * feat: finish up form data handling * chore: add readme * chore: update api-extractor stuff * chore: bump deps * chore: make requested changes * chore: make requested changes * Update package.json * chore: make requested changes * fix: add missing interaction responses * chore: make some requested changes * chore: remove `return await` * chore: use autoModeration instead of automod * refactor: use snowflakes and -types results * chore: sort imports, fix return type on editUserVoiceState * chore: rename bots to users * feat: add automod dispatch events * refactor: move templates and members into guild * fix: use users instead of bots in api class * chore: imports * chore: make requested changes * fix: don't make files required on interaction replies * fix: rename sendMessage to createMessage * feat: add application command routes * feat: add webhook.execute overloads and options to invites.get * chore: use create prefixes * chore: seperate interaction params * chore: use Id * chore: make requested changes * chore: make requested changes * chore: make requested changes * chore: for -> from * Apply suggestions from code review Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * Update packages/core/README.md Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * chore: make requested changes * chore: update -types * chore: bump vitest * fix: sticker uploading * fix: lockfile * chore: make requested changes * chore: make requested changes * Update packages/core/src/api/applicationCommands.ts Co-authored-by: Almeida <almeidx@pm.me> * Apply suggestions from code review Co-authored-by: Aura Román <kyradiscord@gmail.com> * Update packages/core/README.md Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: almeidx <almeidx@pm.me> Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: Aura Román <kyradiscord@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
240
packages/core/src/api/applicationCommands.ts
Normal file
240
packages/core/src/api/applicationCommands.ts
Normal file
@@ -0,0 +1,240 @@
|
||||
import { makeURLSearchParams, type REST } from '@discordjs/rest';
|
||||
import {
|
||||
Routes,
|
||||
type RESTGetAPIApplicationCommandPermissionsResult,
|
||||
type RESTGetAPIApplicationCommandResult,
|
||||
type RESTGetAPIApplicationCommandsResult,
|
||||
type RESTGetAPIGuildApplicationCommandsPermissionsResult,
|
||||
type RESTPatchAPIApplicationCommandJSONBody,
|
||||
type RESTPatchAPIApplicationCommandResult,
|
||||
type RESTPostAPIApplicationCommandsJSONBody,
|
||||
type RESTPostAPIApplicationCommandsResult,
|
||||
type RESTPutAPIApplicationCommandPermissionsJSONBody,
|
||||
type RESTPutAPIApplicationCommandPermissionsResult,
|
||||
type RESTPutAPIApplicationCommandsJSONBody,
|
||||
type RESTGetAPIApplicationCommandsQuery,
|
||||
type RESTPutAPIApplicationCommandsResult,
|
||||
type RESTGetAPIApplicationGuildCommandsQuery,
|
||||
type Snowflake,
|
||||
} from 'discord-api-types/v10';
|
||||
|
||||
export class ApplicationCommandsAPI {
|
||||
public constructor(private readonly rest: REST) {}
|
||||
|
||||
/**
|
||||
* Fetches all global commands for a application
|
||||
*
|
||||
* @param applicationId - The application id to fetch commands for
|
||||
* @param options - The options to use when fetching commands
|
||||
*/
|
||||
public async getGlobalCommands(applicationId: Snowflake, options: RESTGetAPIApplicationCommandsQuery = {}) {
|
||||
return this.rest.get(Routes.applicationCommands(applicationId), {
|
||||
query: makeURLSearchParams(options as Record<string, unknown>),
|
||||
}) as Promise<RESTGetAPIApplicationCommandsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new global command
|
||||
*
|
||||
* @param applicationId - The application id to create the command for
|
||||
* @param data - The data to use when creating the command
|
||||
*/
|
||||
public async createGlobalCommand(applicationId: Snowflake, data: RESTPostAPIApplicationCommandsJSONBody) {
|
||||
return this.rest.post(Routes.applicationCommands(applicationId), {
|
||||
body: data,
|
||||
}) as Promise<RESTPostAPIApplicationCommandsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a global command
|
||||
*
|
||||
* @param applicationId - The application id to fetch the command from
|
||||
* @param commandId - The command id to fetch
|
||||
*/
|
||||
public async getGlobalCommand(applicationId: Snowflake, commandId: Snowflake) {
|
||||
return this.rest.get(
|
||||
Routes.applicationCommand(applicationId, commandId),
|
||||
) as Promise<RESTGetAPIApplicationCommandResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a global command
|
||||
*
|
||||
* @param applicationId - The application id of the command
|
||||
* @param commandId - The id of the command to edit
|
||||
* @param data - The data to use when editing the command
|
||||
*/
|
||||
public async editGlobalCommand(
|
||||
applicationId: Snowflake,
|
||||
commandId: Snowflake,
|
||||
data: RESTPatchAPIApplicationCommandJSONBody,
|
||||
) {
|
||||
return this.rest.patch(Routes.applicationCommand(applicationId, commandId), {
|
||||
body: data,
|
||||
}) as Promise<RESTPatchAPIApplicationCommandResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a global command
|
||||
*
|
||||
* @param applicationId - The application id of the command
|
||||
* @param commandId - The id of the command to delete
|
||||
*/
|
||||
public async deleteGlobalCommand(applicationId: Snowflake, commandId: Snowflake) {
|
||||
await this.rest.delete(Routes.applicationCommand(applicationId, commandId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrites global commands
|
||||
*
|
||||
* @param applicationId - The application id to overwrite commands for
|
||||
* @param data - The data to use when overwriting commands
|
||||
*/
|
||||
public async bulkOverwriteGlobalCommands(applicationId: Snowflake, data: RESTPutAPIApplicationCommandsJSONBody) {
|
||||
return this.rest.put(Routes.applicationCommands(applicationId), {
|
||||
body: data,
|
||||
}) as Promise<RESTPutAPIApplicationCommandsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all commands for a guild
|
||||
*
|
||||
* @param applicationId - The application id to fetch commands for
|
||||
* @param guildId - The guild id to fetch commands for
|
||||
* @param data - The data to use when fetching commands
|
||||
*/
|
||||
public async getGuildCommands(
|
||||
applicationId: Snowflake,
|
||||
guildId: Snowflake,
|
||||
data: RESTGetAPIApplicationGuildCommandsQuery = {},
|
||||
) {
|
||||
return this.rest.get(Routes.applicationGuildCommands(applicationId, guildId), {
|
||||
query: makeURLSearchParams(data as Record<string, unknown>),
|
||||
}) as Promise<RESTGetAPIApplicationCommandsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new command for a guild
|
||||
*
|
||||
* @param applicationId - The application id to create the command for
|
||||
* @param guildId - The guild id to create the command for
|
||||
* @param data - The data to use when creating the command
|
||||
*/
|
||||
public async createGuildCommand(
|
||||
applicationId: Snowflake,
|
||||
guildId: Snowflake,
|
||||
data: RESTPostAPIApplicationCommandsJSONBody,
|
||||
) {
|
||||
return this.rest.post(Routes.applicationGuildCommands(applicationId, guildId), {
|
||||
body: data,
|
||||
}) as Promise<RESTPostAPIApplicationCommandsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a guild command
|
||||
*
|
||||
* @param applicationId - The application id to fetch the command from
|
||||
* @param guildId - The guild id to fetch the command from
|
||||
* @param commandId - The command id to fetch
|
||||
*/
|
||||
public async getGuildCommand(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) {
|
||||
return this.rest.get(
|
||||
Routes.applicationGuildCommand(applicationId, guildId, commandId),
|
||||
) as Promise<RESTGetAPIApplicationCommandResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a guild command
|
||||
*
|
||||
* @param applicationId - The application id of the command
|
||||
* @param guildId - The guild id of the command
|
||||
* @param commandId - The command id to edit
|
||||
* @param data - The data to use when editing the command
|
||||
*/
|
||||
public async editGuildCommand(
|
||||
applicationId: Snowflake,
|
||||
guildId: Snowflake,
|
||||
commandId: Snowflake,
|
||||
data: RESTPatchAPIApplicationCommandJSONBody,
|
||||
) {
|
||||
return this.rest.patch(Routes.applicationGuildCommand(applicationId, guildId, commandId), {
|
||||
body: data,
|
||||
}) as Promise<RESTPatchAPIApplicationCommandResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a guild command
|
||||
*
|
||||
* @param applicationId - The application id of the command
|
||||
* @param guildId - The guild id of the command
|
||||
* @param commandId - The id of the command to delete
|
||||
*/
|
||||
public async deleteGuildCommand(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) {
|
||||
await this.rest.delete(Routes.applicationGuildCommand(applicationId, guildId, commandId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk overwrites guild commands
|
||||
*
|
||||
* @param applicationId - The application id to overwrite commands for
|
||||
* @param guildId - The guild id to overwrite commands for
|
||||
* @param data - The data to use when overwriting commands
|
||||
*/
|
||||
public async bulkOverwriteGuildCommands(
|
||||
applicationId: Snowflake,
|
||||
guildId: Snowflake,
|
||||
data: RESTPutAPIApplicationCommandsJSONBody,
|
||||
) {
|
||||
return this.rest.put(Routes.applicationGuildCommands(applicationId, guildId), {
|
||||
body: data,
|
||||
}) as Promise<RESTPutAPIApplicationCommandsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the permissions for a guild command
|
||||
*
|
||||
* @param applicationId - The application id to get the permissions for
|
||||
* @param guildId - The guild id of the command
|
||||
* @param commandId - The command id to get the permissions for
|
||||
*/
|
||||
public async getGuildCommandPermissions(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) {
|
||||
return this.rest.get(
|
||||
Routes.applicationCommandPermissions(applicationId, guildId, commandId),
|
||||
) as Promise<RESTGetAPIApplicationCommandPermissionsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all permissions for all commands in a guild
|
||||
*
|
||||
* @param applicationId - The application id to get the permissions for
|
||||
* @param guildId - The guild id to get the permissions for
|
||||
*/
|
||||
public async getGuildCommandsPermissions(applicationId: Snowflake, guildId: Snowflake) {
|
||||
return this.rest.get(
|
||||
Routes.guildApplicationCommandsPermissions(applicationId, guildId),
|
||||
) as Promise<RESTGetAPIGuildApplicationCommandsPermissionsResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the permissions for a guild command
|
||||
*
|
||||
* @param userToken - The token of the user to edit permissions on behalf of
|
||||
* @param applicationId - The application id to edit the permissions for
|
||||
* @param guildId - The guild id to edit the permissions for
|
||||
* @param commandId - The id of the command to edit the permissions for
|
||||
* @param data - The data to use when editing the permissions
|
||||
*/
|
||||
public async editGuildCommandPermissions(
|
||||
userToken: string,
|
||||
applicationId: Snowflake,
|
||||
guildId: Snowflake,
|
||||
commandId: Snowflake,
|
||||
data: RESTPutAPIApplicationCommandPermissionsJSONBody,
|
||||
) {
|
||||
return this.rest.put(Routes.applicationCommandPermissions(applicationId, guildId, commandId), {
|
||||
headers: { Authorization: `Bearer ${userToken.replace('Bearer ', '')}` },
|
||||
auth: false,
|
||||
body: data,
|
||||
}) as Promise<RESTPutAPIApplicationCommandPermissionsResult>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user