mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +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:
214
packages/core/src/api/webhook.ts
Normal file
214
packages/core/src/api/webhook.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
import { makeURLSearchParams, type RawFile, type REST } from '@discordjs/rest';
|
||||
import {
|
||||
Routes,
|
||||
type RESTGetAPIChannelMessageResult,
|
||||
type RESTGetAPIWebhookResult,
|
||||
type RESTPatchAPIWebhookJSONBody,
|
||||
type RESTPatchAPIWebhookResult,
|
||||
type RESTPatchAPIWebhookWithTokenMessageJSONBody,
|
||||
type RESTPatchAPIWebhookWithTokenMessageResult,
|
||||
type RESTPostAPIChannelWebhookJSONBody,
|
||||
type RESTPostAPIWebhookWithTokenGitHubQuery,
|
||||
type RESTPostAPIWebhookWithTokenJSONBody,
|
||||
type RESTPostAPIWebhookWithTokenQuery,
|
||||
type RESTPostAPIWebhookWithTokenResult,
|
||||
type RESTPostAPIWebhookWithTokenSlackQuery,
|
||||
type RESTPostAPIWebhookWithTokenWaitResult,
|
||||
type Snowflake,
|
||||
} from 'discord-api-types/v10';
|
||||
|
||||
export class WebhooksAPI {
|
||||
public constructor(private readonly rest: REST) {}
|
||||
|
||||
/**
|
||||
* Fetches a webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
*/
|
||||
public async get(id: Snowflake, token?: string) {
|
||||
return this.rest.get(Routes.webhook(id, token)) as Promise<RESTGetAPIWebhookResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new webhook
|
||||
*
|
||||
* @param channelId - The id of the channel to create the webhook in
|
||||
* @param data - The data to use when creating the webhook
|
||||
* @param reason - The reason for creating the webhook
|
||||
*/
|
||||
public async create(channelId: Snowflake, data: RESTPostAPIChannelWebhookJSONBody, reason?: string) {
|
||||
return this.rest.post(Routes.channelWebhooks(channelId), {
|
||||
reason,
|
||||
body: data,
|
||||
}) as Promise<RESTPostAPIWebhookWithTokenResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits a webhook
|
||||
*
|
||||
* @param id - The id of the webhook to edit
|
||||
* @param webhook - The new webhook data
|
||||
* @param options - The options to use when editing the webhook
|
||||
*/
|
||||
public async edit(
|
||||
id: Snowflake,
|
||||
webhook: RESTPatchAPIWebhookJSONBody,
|
||||
{ token, reason }: { reason?: string; token?: string } = {},
|
||||
) {
|
||||
return this.rest.patch(Routes.webhook(id, token), { reason, body: webhook }) as Promise<RESTPatchAPIWebhookResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a webhook
|
||||
*
|
||||
* @param id - The id of the webhook to delete
|
||||
* @param options - The options to use when deleting the webhook
|
||||
*/
|
||||
public async delete(id: Snowflake, { token, reason }: { reason?: string; token?: string } = {}) {
|
||||
await this.rest.delete(Routes.webhook(id, token), { reason });
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a webhook and returns the created message
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param data - The data to use when executing the webhook
|
||||
*/
|
||||
public async execute(
|
||||
id: Snowflake,
|
||||
token: string,
|
||||
data: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[]; wait: true },
|
||||
): Promise<RESTPostAPIWebhookWithTokenWaitResult>;
|
||||
|
||||
/**
|
||||
* Executes a webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param data - The data to use when executing the webhook
|
||||
*/
|
||||
public async execute(
|
||||
id: Snowflake,
|
||||
token: string,
|
||||
data: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[]; wait?: false },
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* Executes a webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param data - The data to use when executing the webhook
|
||||
*/
|
||||
public async execute(
|
||||
id: Snowflake,
|
||||
token: string,
|
||||
{
|
||||
wait,
|
||||
thread_id,
|
||||
files,
|
||||
...body
|
||||
}: RESTPostAPIWebhookWithTokenJSONBody & RESTPostAPIWebhookWithTokenQuery & { files?: RawFile[] },
|
||||
) {
|
||||
return this.rest.post(Routes.webhook(id, token), {
|
||||
query: makeURLSearchParams({ wait, thread_id }),
|
||||
files,
|
||||
body,
|
||||
auth: false,
|
||||
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
||||
}) as Promise<RESTPostAPIWebhookWithTokenWaitResult | void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a slack webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param options - The options to use when executing the webhook
|
||||
*/
|
||||
public async executeSlack(
|
||||
id: Snowflake,
|
||||
token: string,
|
||||
body: unknown,
|
||||
options: RESTPostAPIWebhookWithTokenSlackQuery = {},
|
||||
) {
|
||||
await this.rest.post(Routes.webhookPlatform(id, token, 'slack'), {
|
||||
query: makeURLSearchParams(options as Record<string, unknown>),
|
||||
body,
|
||||
auth: false,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a github webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param options - The options to use when executing the webhook
|
||||
*/
|
||||
public async executeGitHub(
|
||||
id: Snowflake,
|
||||
token: string,
|
||||
body: unknown,
|
||||
options: RESTPostAPIWebhookWithTokenGitHubQuery = {},
|
||||
) {
|
||||
await this.rest.post(Routes.webhookPlatform(id, token, 'github'), {
|
||||
query: makeURLSearchParams(options as Record<string, unknown>),
|
||||
body,
|
||||
auth: false,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches an associated message from a webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param messageId - The id of the message to fetch
|
||||
* @param options - The options to use when fetching the message
|
||||
*/
|
||||
public async getMessage(id: Snowflake, token: string, messageId: Snowflake, options: { thread_id?: string } = {}) {
|
||||
return this.rest.get(Routes.webhookMessage(id, token, messageId), {
|
||||
query: makeURLSearchParams(options as Record<string, unknown>),
|
||||
auth: false,
|
||||
}) as Promise<RESTGetAPIChannelMessageResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits an associated message from a webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param messageId - The id of the message to edit
|
||||
* @param data - The data to use when editing the message
|
||||
*/
|
||||
public async editMessage(
|
||||
id: Snowflake,
|
||||
token: string,
|
||||
messageId: Snowflake,
|
||||
{ thread_id, ...body }: RESTPatchAPIWebhookWithTokenMessageJSONBody & { thread_id?: string },
|
||||
) {
|
||||
return this.rest.patch(Routes.webhookMessage(id, token, messageId), {
|
||||
query: makeURLSearchParams({ thread_id }),
|
||||
auth: false,
|
||||
body,
|
||||
}) as Promise<RESTPatchAPIWebhookWithTokenMessageResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an associated message from a webhook
|
||||
*
|
||||
* @param id - The id of the webhook
|
||||
* @param token - The token of the webhook
|
||||
* @param messageId - The id of the message to delete
|
||||
* @param options - The options to use when deleting the message
|
||||
*/
|
||||
public async deleteMessage(id: Snowflake, token: string, messageId: Snowflake, options: { thread_id?: string } = {}) {
|
||||
await this.rest.delete(Routes.webhookMessage(id, token, messageId), {
|
||||
query: makeURLSearchParams(options as Record<string, unknown>),
|
||||
auth: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user