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:
Suneet Tipirneni
2022-11-27 16:23:13 -05:00
committed by GitHub
parent 12553da135
commit 2127b32d26
33 changed files with 2979 additions and 2 deletions

View File

@@ -0,0 +1,181 @@
import type { RawFile, REST } from '@discordjs/rest';
import type { Snowflake } from 'discord-api-types/v10';
import {
InteractionResponseType,
Routes,
type APICommandAutocompleteInteractionResponseCallbackData,
type APIInteractionResponseCallbackData,
type APIModalInteractionResponseCallbackData,
type RESTGetAPIWebhookWithTokenMessageResult,
} from 'discord-api-types/v10';
import type { WebhooksAPI } from './webhook.js';
export class InteractionsAPI {
public constructor(private readonly rest: REST, private readonly webhooks: WebhooksAPI) {}
/**
* Replies to an interaction
*
* @param interactionId - The id of the interaction
* @param interactionToken - The token of the interaction
* @param data - The data to use when replying
*/
public async reply(
interactionId: Snowflake,
interactionToken: string,
{ files, ...data }: APIInteractionResponseCallbackData & { files?: RawFile[] },
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
files,
body: {
type: InteractionResponseType.ChannelMessageWithSource,
data,
},
});
}
/**
* Defers the reply to an interaction
*
* @param interactionId - The id of the interaction
* @param interactionToken - The token of the interaction
*/
public async defer(interactionId: Snowflake, interactionToken: string) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
body: {
type: InteractionResponseType.DeferredChannelMessageWithSource,
},
});
}
/**
* Defers an update from a message component interaction
*
* @param interactionId - The id of the interaction
* @param interactionToken - The token of the interaction
*/
public async deferMessageUpdate(interactionId: Snowflake, interactionToken: string) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
body: {
type: InteractionResponseType.DeferredMessageUpdate,
},
});
}
/**
* Reply to a deferred interaction
*
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
* @param data - The data to use when replying
*/
public async followUp(
applicationId: Snowflake,
interactionToken: string,
data: APIInteractionResponseCallbackData & { files?: RawFile[] },
) {
await this.webhooks.execute(applicationId, interactionToken, data);
}
/**
* Edits the initial reply to an interaction
*
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
* @param data - The data to use when editing the reply
* @param messageId - The id of the message to edit. If omitted, the original reply will be edited
*/
public async editReply(
applicationId: Snowflake,
interactionToken: string,
data: APIInteractionResponseCallbackData & { files?: RawFile[] },
messageId?: string,
) {
return this.webhooks.editMessage(applicationId, interactionToken, messageId ?? '@original', data);
}
/**
* Fetches the initial reply to an interaction
*
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
*/
public async getOriginalReply(applicationId: Snowflake, interactionToken: string) {
return this.webhooks.getMessage(
applicationId,
interactionToken,
'@original',
) as Promise<RESTGetAPIWebhookWithTokenMessageResult>;
}
/**
* Deletes the initial reply to an interaction
*
* @param applicationId - The application id of the interaction
* @param interactionToken - The token of the interaction
*/
public async deleteReply(applicationId: Snowflake, interactionToken: string) {
await this.webhooks.deleteMessage(applicationId, interactionToken, '@original');
}
/**
* Updates the the message the component interaction was triggered on
*
* @param interactionId - The id of the interaction
* @param interactionToken - The token of the interaction
* @param data - The data to use when updating the interaction
*/
public async updateMessage(
interactionId: Snowflake,
interactionToken: string,
{ files, ...data }: APIInteractionResponseCallbackData & { files?: RawFile[] },
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
files,
body: {
type: InteractionResponseType.UpdateMessage,
data,
},
});
}
/**
* Sends an autocomplete response to an interaction
*
* @param interactionId - The id of the interaction
* @param interactionToken - The token of the interaction
* @param data - Data for the autocomplete response
*/
public async createAutocompleteResponse(
interactionId: Snowflake,
interactionToken: string,
data: APICommandAutocompleteInteractionResponseCallbackData,
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
body: {
type: InteractionResponseType.ApplicationCommandAutocompleteResult,
data,
},
});
}
/**
* Sends a modal response to an interaction
*
* @param interactionId - The id of the interaction
* @param interactionToken - The token of the interaction
* @param data - The modal to send
*/
public async createModal(
interactionId: Snowflake,
interactionToken: string,
data: APIModalInteractionResponseCallbackData,
) {
await this.rest.post(Routes.interactionCallback(interactionId, interactionToken), {
body: {
type: InteractionResponseType.Modal,
data,
},
});
}
}