From 3ff874e14c38684dc4f6844635de43885938d140 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 18 Aug 2022 22:26:21 -0700 Subject: [PATCH 01/13] Add quick notes --- packages/discord-api/src/routes/commands.ts | 73 +++++++++++++------ .../discord-api/src/routes/interactions.ts | 14 +++- packages/discord-api/src/routes/webhook.ts | 61 ++++++++++++++++ packages/discord-api/test/webhook.test.ts | 27 +++++++ 4 files changed, 152 insertions(+), 23 deletions(-) create mode 100644 packages/discord-api/src/routes/webhook.ts create mode 100644 packages/discord-api/test/webhook.test.ts diff --git a/packages/discord-api/src/routes/commands.ts b/packages/discord-api/src/routes/commands.ts index 7ff1179c..ad66d81c 100644 --- a/packages/discord-api/src/routes/commands.ts +++ b/packages/discord-api/src/routes/commands.ts @@ -1,5 +1,7 @@ import type { + RESTGetAPIApplicationCommandResult, RESTGetAPIApplicationCommandsResult, + RESTGetAPIApplicationGuildCommandResult, RESTGetAPIApplicationGuildCommandsResult, RESTPatchAPIApplicationCommandJSONBody, RESTPatchAPIApplicationCommandResult, @@ -18,6 +20,10 @@ import { Routes } from "discord-api-types/v10"; import type { Snowflake } from "discord-snowflake"; import { client } from "../client"; +/** + * Global Commands + */ + // TODO: Test, Document export async function getGlobalApplicationCommands(applicationId: Snowflake) { return client.get( @@ -26,7 +32,7 @@ export async function getGlobalApplicationCommands(applicationId: Snowflake) { } // TODO: Test, Document -export async function postGlobalApplicationCommand( +export async function createGlobalApplicationCommand( applicationId: Snowflake, command: RESTPostAPIApplicationCommandsJSONBody ) { @@ -36,17 +42,17 @@ export async function postGlobalApplicationCommand( } // TODO: Test, Document -export async function putGlobalApplicationCommands( +export async function getGlobalApplicationCommand( applicationId: Snowflake, - commands: RESTPutAPIApplicationCommandsJSONBody + commandId: Snowflake ) { - return client.put(Routes.applicationCommands(applicationId), { - body: commands, - }) as Promise; + return client.get( + Routes.applicationCommand(applicationId, commandId) + ) as Promise; } // TODO: Test, Document -export async function patchGlobalApplicationCommand( +export async function editGlobalApplicationCommand( applicationId: Snowflake, command: RESTPatchAPIApplicationCommandJSONBody & { id: Snowflake } ) { @@ -63,6 +69,20 @@ export async function deleteGlobalApplicationCommand( return client.delete(Routes.applicationCommand(applicationId, commandId)); } +// TODO: Test, Document +export async function bulkOverwriteGlobalApplicationCommands( + applicationId: Snowflake, + commands: RESTPutAPIApplicationCommandsJSONBody +) { + return client.put(Routes.applicationCommands(applicationId), { + body: commands, + }) as Promise; +} + +/** + * Guild Commands + */ + // TODO: Test, Document export async function getGuildApplicationCommands( applicationId: Snowflake, @@ -74,7 +94,7 @@ export async function getGuildApplicationCommands( } // TODO: Test, Document -export async function postGuildApplicationCommand( +export async function createGuildApplicationCommand( applicationId: Snowflake, guildId: Snowflake, command: RESTPostAPIApplicationGuildCommandsJSONBody @@ -85,28 +105,25 @@ export async function postGuildApplicationCommand( } // TODO: Test, Document -export async function putGuildApplicationCommands( +export async function getGuildApplicationCommand( applicationId: Snowflake, guildId: Snowflake, - commands: RESTPutAPIApplicationGuildCommandsJSONBody + commandId: Snowflake ) { - return client.put(Routes.applicationGuildCommands(applicationId, guildId), { - body: commands, - }) as Promise; + return client.get( + Routes.applicationGuildCommand(applicationId, guildId, commandId) + ) as Promise; } // TODO: Test, Document -export async function patchGuildApplicationCommand( +export async function editGuildApplicationCommands( applicationId: Snowflake, guildId: Snowflake, - command: RESTPatchAPIApplicationGuildCommandJSONBody & { id: Snowflake } + commands: RESTPutAPIApplicationGuildCommandsJSONBody ) { - return client.patch( - Routes.applicationGuildCommand(applicationId, guildId, command.id), - { - body: command, - } - ) as Promise; + return client.put(Routes.applicationGuildCommands(applicationId, guildId), { + body: commands, + }) as Promise; } // TODO: Test, Document @@ -119,3 +136,17 @@ export async function deleteGuildApplicationCommand( Routes.applicationGuildCommand(applicationId, guildId, commandId) ); } + +// TODO: Test, Document +export async function bulkOverwriteGuildApplicationCommand( + applicationId: Snowflake, + guildId: Snowflake, + command: RESTPatchAPIApplicationGuildCommandJSONBody & { id: Snowflake } +) { + return client.patch( + Routes.applicationGuildCommand(applicationId, guildId, command.id), + { + body: command, + } + ) as Promise; +} diff --git a/packages/discord-api/src/routes/interactions.ts b/packages/discord-api/src/routes/interactions.ts index 4bc4243c..7f26390a 100644 --- a/packages/discord-api/src/routes/interactions.ts +++ b/packages/discord-api/src/routes/interactions.ts @@ -10,7 +10,7 @@ import type { Snowflake } from "discord-snowflake"; import { client } from "../client"; // TODO: Test, Document -export async function postInteractionFollowup( +export async function createInteractionFollowup( applicationId: Snowflake, interactionToken: string, data: RESTPostAPIInteractionFollowupJSONBody @@ -20,8 +20,18 @@ export async function postInteractionFollowup( }) as Promise; } +// export async function getInteractionFollowup( +// applicationId: Snowflake, +// interactionToken: string, +// messageId: Snowflake +// ) { +// return client.get( +// Routes.webhook(applicationId, interactionToken, messageId) +// ) as Promise; +// } + // TODO: Test, Document -export async function patchInteractionFollowup( +export async function editInteractionFollowup( applicationId: Snowflake, interactionToken: string, id: string, diff --git a/packages/discord-api/src/routes/webhook.ts b/packages/discord-api/src/routes/webhook.ts new file mode 100644 index 00000000..48adb3c5 --- /dev/null +++ b/packages/discord-api/src/routes/webhook.ts @@ -0,0 +1,61 @@ +import type { + RESTPostAPIChannelWebhookJSONBody, + RESTPostAPIChannelWebhookResult, +} from "discord-api-types/v10"; +import { Routes } from "discord-api-types/v10"; +import type { Snowflake } from "discord-snowflake"; +import { client } from "../client"; + +/** + * Creates a Channel Webhook + * @link https://discord.com/developers/docs/resources/webhook#create-webhook + * @param channelId The Channel to create the webhook in + * @param data The Webhook name and avatar + * @param auditLogReason An optional entry to add to the audit log + * @returns The created Webhook + */ +export async function createWebhook( + channelId: Snowflake, + data: RESTPostAPIChannelWebhookJSONBody, + auditLogReason?: string +) { + const headers = new Headers(); + + if (auditLogReason != null) { + headers.set("X-Audit-Log-Reason", auditLogReason); + } + + return client.post(Routes.channelWebhooks(channelId), { + body: data, + headers, + }) as Promise; +} + +/** + * createWebhook + * getChannelWebhooks + * getGuildWebhooks + * getWebhook + * getWebhookWithToken + * modifyWebhook + * modifyWebhookWithToken + * deleteWebhook + * deleteWebhookWithToken + * executeWebook + * executeSlackWebhook + * executeGitHubWebook + * getWebhookMessage + * editWebhookMessage + * deleteWebhookMessage + * + * or + * postWebhook + * getChannelWebhooks + * getGuildWebhooks + * getWebhook + * getWebhookWithToken + * patchWebhook + * patchWebhookWithToken + * deleteWebhook + * deleteWebhookWithToken + */ diff --git a/packages/discord-api/test/webhook.test.ts b/packages/discord-api/test/webhook.test.ts new file mode 100644 index 00000000..4a65920a --- /dev/null +++ b/packages/discord-api/test/webhook.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, test, vi } from "vitest"; +import { client } from "../src/client"; +import { createWebhook } from "../src/routes/webhook"; + +describe("createWebhook", () => { + test("Accepts an optional audit log entry", async () => { + const spy = vi + .spyOn(client, "post") + .mockImplementation(async () => Promise.resolve("")); + + await createWebhook( + "123", + { name: "Test Webhook", avatar: "avatar.png" }, + "Vitest Log" + ); + expect(spy).toHaveBeenCalledWith( + expect.anything, + { + body: expect.anything, + headers: { "X-Audit-Log-Reason": "Vitest Log" }, + }, + expect.anything + ); + }); + + test.todo("Includes webhook information"); +}); From 1985eaffabd9b4835265e6a48bb5435fd6796468 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 18 Aug 2022 23:26:33 -0700 Subject: [PATCH 02/13] Add Webhook Example --- package-lock.json | 6 ++-- package.json | 2 +- packages/discord-api/src/client.ts | 2 +- packages/discord-api/src/routes/commands.ts | 15 ++++------ packages/discord-api/test/webhook.test.ts | 29 +++++++++++++------ packages/discord-request/package.json | 2 +- packages/interaction-kit/src/cli/deploy.ts | 7 +++-- packages/interaction-kit/src/cli/dev.ts | 4 +-- .../application-command-interaction.ts | 8 ++--- .../message-component-interaction.ts | 8 ++--- packages/interaction-kit/src/interfaces.ts | 4 +-- 11 files changed, 48 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8952ce2b..aea1caae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22588,7 +22588,7 @@ } }, "packages/discord-error": { - "version": "0.0.1", + "version": "0.0.2", "license": "Apache-2.0", "engines": { "node": ">=18" @@ -22599,7 +22599,7 @@ "license": "Apache-2.0", "dependencies": { "debug": "^4.3.4", - "discord-error": "0.0.1", + "discord-error": "0.0.2", "discord-snowflake": "2.0.0" }, "engines": { @@ -28848,7 +28848,7 @@ "version": "file:packages/discord-request", "requires": { "debug": "^4.3.4", - "discord-error": "0.0.1", + "discord-error": "0.0.2", "discord-snowflake": "2.0.0" } }, diff --git a/package.json b/package.json index 7827d4fe..3be4930b 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "prettier": "turbo run prettier", "lint": "turbo run lint", "pretest": "npm run clean && npm run build", - "test": "vitest run", + "test": "NODE_OPTIONS='--es-module-specifier-resolution=node' vitest run", "version": "npx changeset version", "changeset": "npx changeset add", "publish": "npx changeset publish" diff --git a/packages/discord-api/src/client.ts b/packages/discord-api/src/client.ts index d78b72be..c6deceee 100644 --- a/packages/discord-api/src/client.ts +++ b/packages/discord-api/src/client.ts @@ -1,5 +1,5 @@ import Client from "discord-request"; -import pkg from "../package.json" assert { type: "json" }; +import pkg from "../package.json"; export class DiscordApiClient extends Client { get userAgent() { diff --git a/packages/discord-api/src/routes/commands.ts b/packages/discord-api/src/routes/commands.ts index ad66d81c..40c46d59 100644 --- a/packages/discord-api/src/routes/commands.ts +++ b/packages/discord-api/src/routes/commands.ts @@ -5,8 +5,6 @@ import type { RESTGetAPIApplicationGuildCommandsResult, RESTPatchAPIApplicationCommandJSONBody, RESTPatchAPIApplicationCommandResult, - RESTPatchAPIApplicationGuildCommandJSONBody, - RESTPatchAPIApplicationGuildCommandResult, RESTPostAPIApplicationCommandsJSONBody, RESTPostAPIApplicationCommandsResult, RESTPostAPIApplicationGuildCommandsJSONBody, @@ -138,15 +136,12 @@ export async function deleteGuildApplicationCommand( } // TODO: Test, Document -export async function bulkOverwriteGuildApplicationCommand( +export async function bulkOverwriteGuildApplicationCommands( applicationId: Snowflake, guildId: Snowflake, - command: RESTPatchAPIApplicationGuildCommandJSONBody & { id: Snowflake } + commands: RESTPutAPIApplicationGuildCommandsJSONBody ) { - return client.patch( - Routes.applicationGuildCommand(applicationId, guildId, command.id), - { - body: command, - } - ) as Promise; + return client.put(Routes.applicationGuildCommands(applicationId, guildId), { + body: commands, + }) as Promise; } diff --git a/packages/discord-api/test/webhook.test.ts b/packages/discord-api/test/webhook.test.ts index 4a65920a..a28fd4c9 100644 --- a/packages/discord-api/test/webhook.test.ts +++ b/packages/discord-api/test/webhook.test.ts @@ -13,15 +13,26 @@ describe("createWebhook", () => { { name: "Test Webhook", avatar: "avatar.png" }, "Vitest Log" ); - expect(spy).toHaveBeenCalledWith( - expect.anything, - { - body: expect.anything, - headers: { "X-Audit-Log-Reason": "Vitest Log" }, - }, - expect.anything - ); + expect(spy).toHaveBeenCalledWith(expect.anything(), { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + body: expect.anything(), + headers: new Headers({ "X-Audit-Log-Reason": "Vitest Log" }), + }); }); - test.todo("Includes webhook information"); + test("Includes webhook information", async () => { + const spy = vi + .spyOn(client, "post") + .mockImplementation(async () => Promise.resolve("")); + + await createWebhook("123", { name: "Test Webhook", avatar: "avatar.png" }); + expect(spy).toHaveBeenCalledWith("/channels/123/webhooks", { + body: { + name: "Test Webhook", + avatar: "avatar.png", + }, + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + headers: expect.any(Headers), + }); + }); }); diff --git a/packages/discord-request/package.json b/packages/discord-request/package.json index a691c692..43a89947 100644 --- a/packages/discord-request/package.json +++ b/packages/discord-request/package.json @@ -24,7 +24,7 @@ "node": ">=18" }, "dependencies": { - "discord-error": "0.0.1", + "discord-error": "0.0.2", "discord-snowflake": "2.0.0", "debug": "^4.3.4" } diff --git a/packages/interaction-kit/src/cli/deploy.ts b/packages/interaction-kit/src/cli/deploy.ts index ac2d1297..80d86bb2 100644 --- a/packages/interaction-kit/src/cli/deploy.ts +++ b/packages/interaction-kit/src/cli/deploy.ts @@ -1,4 +1,4 @@ -import { putGlobalApplicationCommands } from "discord-api"; +import { bulkOverwriteGlobalApplicationCommands } from "discord-api"; import type { RESTPutAPIApplicationCommandsJSONBody } from "discord-api-types/v10"; import { getApplicationEntrypoint } from "../scripts"; @@ -18,6 +18,9 @@ export default async function command(argv?: string[]) { const serializedCommands: RESTPutAPIApplicationCommandsJSONBody = application.commands.map((command) => command.serialize()); - await putGlobalApplicationCommands(application.id, serializedCommands); + await bulkOverwriteGlobalApplicationCommands( + application.id, + serializedCommands + ); process.exit(0); } diff --git a/packages/interaction-kit/src/cli/dev.ts b/packages/interaction-kit/src/cli/dev.ts index a9e2a377..89db765a 100644 --- a/packages/interaction-kit/src/cli/dev.ts +++ b/packages/interaction-kit/src/cli/dev.ts @@ -2,7 +2,7 @@ import arg from "arg"; import boxen from "boxen"; import chalk from "chalk"; import debug from "debug"; -import { putGuildApplicationCommands } from "discord-api"; +import { bulkOverwriteGuildApplicationCommands } from "discord-api"; import server from "discord-edge-runner"; import type { Snowflake } from "discord-snowflake"; import ngrok from "ngrok"; @@ -34,7 +34,7 @@ async function updateCommands(guildId: Snowflake) { try { if (devCommandChangeSet.hasChanges) { - await putGuildApplicationCommands( + await bulkOverwriteGuildApplicationCommands( application.id, guildId, serializedCommands diff --git a/packages/interaction-kit/src/interactions/application-commands/application-command-interaction.ts b/packages/interaction-kit/src/interactions/application-commands/application-command-interaction.ts index 92f27607..8278762f 100644 --- a/packages/interaction-kit/src/interactions/application-commands/application-command-interaction.ts +++ b/packages/interaction-kit/src/interactions/application-commands/application-command-interaction.ts @@ -1,7 +1,7 @@ import { + createInteractionFollowup, deleteInteractionFollowup, - patchInteractionFollowup, - postInteractionFollowup, + editInteractionFollowup, } from "discord-api"; import type { APIApplicationCommandInteraction, @@ -63,7 +63,7 @@ export default class ApplicationCommandInteraction implements Interaction { data: RESTPatchAPIInteractionFollowupJSONBody, id = "@original" ) => - patchInteractionFollowup( + editInteractionFollowup( Config.getApplicationId(), this.token, id, @@ -125,7 +125,7 @@ export default class ApplicationCommandInteraction implements Interaction { return "@original"; } - const responseData = await postInteractionFollowup( + const responseData = await createInteractionFollowup( Config.getApplicationId(), this.token, payload.data diff --git a/packages/interaction-kit/src/interactions/message-components/message-component-interaction.ts b/packages/interaction-kit/src/interactions/message-components/message-component-interaction.ts index c53e6b93..774c8f68 100644 --- a/packages/interaction-kit/src/interactions/message-components/message-component-interaction.ts +++ b/packages/interaction-kit/src/interactions/message-components/message-component-interaction.ts @@ -1,7 +1,7 @@ import { + createInteractionFollowup, deleteInteractionFollowup, - patchInteractionFollowup, - postInteractionFollowup, + editInteractionFollowup, } from "discord-api"; import type { APIInteractionGuildMember, @@ -67,7 +67,7 @@ export default class MessageComponentInteraction implements Interaction { data: RESTPatchAPIInteractionFollowupJSONBody, id = "@original" ) => - patchInteractionFollowup( + editInteractionFollowup( Config.getApplicationId(), this.token, id, @@ -135,7 +135,7 @@ export default class MessageComponentInteraction implements Interaction { return "@original"; } - const responseData = await postInteractionFollowup( + const responseData = await createInteractionFollowup( Config.getApplicationId(), this.token, payload.data diff --git a/packages/interaction-kit/src/interfaces.ts b/packages/interaction-kit/src/interfaces.ts index 355b10c9..630719c5 100644 --- a/packages/interaction-kit/src/interfaces.ts +++ b/packages/interaction-kit/src/interfaces.ts @@ -1,6 +1,6 @@ import type { deleteInteractionFollowup, - patchInteractionFollowup, + editInteractionFollowup, } from "discord-api"; import type { APIApplicationCommand, @@ -77,7 +77,7 @@ export type InteractionMessageModifiers = { edit: ( data: RESTPatchAPIInteractionFollowupJSONBody, id?: string - ) => ReturnType; + ) => ReturnType; delete: (id?: string) => ReturnType; }; From d6d0ea35102cf89fbe856da4049a273f2d105209 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 18 Aug 2022 23:31:04 -0700 Subject: [PATCH 03/13] cleanup --- packages/discord-api/test/webhook.test.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/discord-api/test/webhook.test.ts b/packages/discord-api/test/webhook.test.ts index a28fd4c9..50be3c1e 100644 --- a/packages/discord-api/test/webhook.test.ts +++ b/packages/discord-api/test/webhook.test.ts @@ -4,9 +4,7 @@ import { createWebhook } from "../src/routes/webhook"; describe("createWebhook", () => { test("Accepts an optional audit log entry", async () => { - const spy = vi - .spyOn(client, "post") - .mockImplementation(async () => Promise.resolve("")); + const spy = vi.spyOn(client, "post").mockImplementation(async () => ""); await createWebhook( "123", @@ -21,9 +19,7 @@ describe("createWebhook", () => { }); test("Includes webhook information", async () => { - const spy = vi - .spyOn(client, "post") - .mockImplementation(async () => Promise.resolve("")); + const spy = vi.spyOn(client, "post").mockImplementation(async () => ""); await createWebhook("123", { name: "Test Webhook", avatar: "avatar.png" }); expect(spy).toHaveBeenCalledWith("/channels/123/webhooks", { From c0a0ccdfa3b2da5b7caad8bb8240302736f7bfc0 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 18 Aug 2022 23:37:32 -0700 Subject: [PATCH 04/13] cleanup --- packages/discord-api/src/routes/webhook.ts | 29 ---------------------- 1 file changed, 29 deletions(-) diff --git a/packages/discord-api/src/routes/webhook.ts b/packages/discord-api/src/routes/webhook.ts index 48adb3c5..e6d42735 100644 --- a/packages/discord-api/src/routes/webhook.ts +++ b/packages/discord-api/src/routes/webhook.ts @@ -30,32 +30,3 @@ export async function createWebhook( headers, }) as Promise; } - -/** - * createWebhook - * getChannelWebhooks - * getGuildWebhooks - * getWebhook - * getWebhookWithToken - * modifyWebhook - * modifyWebhookWithToken - * deleteWebhook - * deleteWebhookWithToken - * executeWebook - * executeSlackWebhook - * executeGitHubWebook - * getWebhookMessage - * editWebhookMessage - * deleteWebhookMessage - * - * or - * postWebhook - * getChannelWebhooks - * getGuildWebhooks - * getWebhook - * getWebhookWithToken - * patchWebhook - * patchWebhookWithToken - * deleteWebhook - * deleteWebhookWithToken - */ From 91ef337a5ede7c261aedf5a91ee61077bb96606f Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 18 Aug 2022 23:52:37 -0700 Subject: [PATCH 05/13] add note --- packages/discord-request/src/queue.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/discord-request/src/queue.ts b/packages/discord-request/src/queue.ts index 1800a463..2cf9a172 100644 --- a/packages/discord-request/src/queue.ts +++ b/packages/discord-request/src/queue.ts @@ -47,6 +47,7 @@ export class Queue { async add(route: Route, resource: string, init: RequestInit) { return new Promise((resolve) => { + // TODO: Handle thrown errors from #process void this.#queue.then(async () => { const value = await this.#process(route, resource, init); resolve(value); From 035e71c63a28f139e8f1851ed9083c312def646d Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 25 Aug 2022 10:32:04 -0700 Subject: [PATCH 06/13] update --- package-lock.json | 13 +++---------- package.json | 2 +- packages/discord-api/src/routes/webhook.ts | 2 +- packages/discord-api/test/webhook.test.ts | 4 ++-- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index f1c0396c..03b3331c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21918,7 +21918,7 @@ } }, "packages/interaction-kit": { - "version": "0.2.0", + "version": "0.2.1", "license": "MIT", "dependencies": { "arg": "^5.0.2", @@ -21929,7 +21929,7 @@ "discord-edge-runner": "0.0.2", "discord-request": "0.0.4", "discord-snowflake": "2.0.0", - "discord-verify": "0.0.1", + "discord-verify": "1.0.0", "dotenv": "^16.0.1", "ngrok": "^4.3.3", "supports-color": "9.2.2" @@ -21951,10 +21951,6 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, - "packages/interaction-kit/node_modules/discord-verify": { - "version": "0.0.1", - "license": "ISC" - }, "packages/interaction-kit/node_modules/supports-color": { "version": "9.2.2", "license": "MIT", @@ -29155,7 +29151,7 @@ "discord-edge-runner": "0.0.2", "discord-request": "0.0.4", "discord-snowflake": "2.0.0", - "discord-verify": "0.0.1", + "discord-verify": "1.0.0", "dotenv": "^16.0.1", "ngrok": "^4.3.3", "supports-color": "9.2.2" @@ -29164,9 +29160,6 @@ "chalk": { "version": "5.0.1" }, - "discord-verify": { - "version": "0.0.1" - }, "supports-color": { "version": "9.2.2" } diff --git a/package.json b/package.json index 985fe276..2dd8ab94 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "prettier": "turbo run prettier", "lint": "turbo run lint", "pretest": "npm run clean && npm run build", - "test": "NODE_OPTIONS='--es-module-specifier-resolution=node' vitest run", + "test": "vitest run", "version": "npx changeset version", "changeset": "npx changeset add", "publish": "npm run build && npx changeset publish" diff --git a/packages/discord-api/src/routes/webhook.ts b/packages/discord-api/src/routes/webhook.ts index e6d42735..5da25ba7 100644 --- a/packages/discord-api/src/routes/webhook.ts +++ b/packages/discord-api/src/routes/webhook.ts @@ -4,7 +4,7 @@ import type { } from "discord-api-types/v10"; import { Routes } from "discord-api-types/v10"; import type { Snowflake } from "discord-snowflake"; -import { client } from "../client"; +import { client } from "../client.js"; /** * Creates a Channel Webhook diff --git a/packages/discord-api/test/webhook.test.ts b/packages/discord-api/test/webhook.test.ts index 50be3c1e..74b3a115 100644 --- a/packages/discord-api/test/webhook.test.ts +++ b/packages/discord-api/test/webhook.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test, vi } from "vitest"; -import { client } from "../src/client"; -import { createWebhook } from "../src/routes/webhook"; +import { client } from "../src/client.js"; +import { createWebhook } from "../src/routes/webhook.js"; describe("createWebhook", () => { test("Accepts an optional audit log entry", async () => { From 1d2df315b2ef571b2641422e5293c586a9a9a362 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Tue, 18 Oct 2022 15:10:19 -0700 Subject: [PATCH 07/13] save state --- packages/discord-api/test/webhook.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/discord-api/test/webhook.test.ts b/packages/discord-api/test/webhook.test.ts index 74b3a115..1c563292 100644 --- a/packages/discord-api/test/webhook.test.ts +++ b/packages/discord-api/test/webhook.test.ts @@ -1,8 +1,20 @@ import { describe, expect, test, vi } from "vitest"; import { client } from "../src/client.js"; import { createWebhook } from "../src/routes/webhook.js"; +import { runAuditLogTest } from "./helpers/headers.js"; describe("createWebhook", () => { + test("Accepts an optional audit log entry", async () => { + await runAuditLogTest("post", async () => { + await createWebhook( + "123", + { name: "Test Webhook", avatar: "avatar.png" }, + "Vitest Log" + ); + return "Vitest Log"; + }); + }); + test("Accepts an optional audit log entry", async () => { const spy = vi.spyOn(client, "post").mockImplementation(async () => ""); From 84b41b5e8f73f6389ed5f638a5446cfd129956ec Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Wed, 19 Oct 2022 20:42:30 -0700 Subject: [PATCH 08/13] excess files --- packages/discord-api/test/helpers/headers.ts | 18 +++++++++++++ packages/discord-api/test/index.test.ts | 27 ++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 packages/discord-api/test/helpers/headers.ts create mode 100644 packages/discord-api/test/index.test.ts diff --git a/packages/discord-api/test/helpers/headers.ts b/packages/discord-api/test/helpers/headers.ts new file mode 100644 index 00000000..eb9a05ca --- /dev/null +++ b/packages/discord-api/test/helpers/headers.ts @@ -0,0 +1,18 @@ +import { expect, vi } from "vitest"; +import { client } from "../../src/client.js"; + +export async function runAuditLogTest( + // TODO: Should probably infer this somehow instead of hardcoding it + method: "post" | "patch" | "delete" | "put" | "get", + callback: () => Promise +) { + const spy = vi.spyOn(client, method).mockImplementation(async () => ""); + + const reason = await callback(); + + expect(spy).toHaveBeenCalledWith(expect.anything(), { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + body: expect.anything(), + headers: new Headers({ "X-Audit-Log-Reason": reason }), + }); +} diff --git a/packages/discord-api/test/index.test.ts b/packages/discord-api/test/index.test.ts new file mode 100644 index 00000000..2399632e --- /dev/null +++ b/packages/discord-api/test/index.test.ts @@ -0,0 +1,27 @@ +import { expect, test } from "vitest"; +import * as API from "../src/index"; + +test("API is backwards compatible", () => { + expect(Object.keys(API)).toMatchInlineSnapshot(` + [ + "DiscordApiClient", + "client", + "getGlobalApplicationCommands", + "createGlobalApplicationCommand", + "getGlobalApplicationCommand", + "editGlobalApplicationCommand", + "deleteGlobalApplicationCommand", + "bulkOverwriteGlobalApplicationCommands", + "getGuildApplicationCommands", + "createGuildApplicationCommand", + "getGuildApplicationCommand", + "editGuildApplicationCommands", + "deleteGuildApplicationCommand", + "bulkOverwriteGuildApplicationCommands", + "getGuild", + "createInteractionFollowup", + "editInteractionFollowup", + "deleteInteractionFollowup", + ] + `); +}); From 5069c06e6c0adbc566a27a8f91a522f5a62b8b84 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Fri, 28 Oct 2022 21:16:24 -0700 Subject: [PATCH 09/13] Add Audit Log --- packages/discord-api/src/index.ts | 3 ++- .../{commands.ts => application-commands.ts} | 0 packages/discord-api/src/routes/audit-log.ts | 16 ++++++++++++++++ packages/discord-api/src/routes/webhook.ts | 10 +++++----- 4 files changed, 23 insertions(+), 6 deletions(-) rename packages/discord-api/src/routes/{commands.ts => application-commands.ts} (100%) create mode 100644 packages/discord-api/src/routes/audit-log.ts diff --git a/packages/discord-api/src/index.ts b/packages/discord-api/src/index.ts index 8aa57e21..82e204db 100644 --- a/packages/discord-api/src/index.ts +++ b/packages/discord-api/src/index.ts @@ -1,4 +1,5 @@ export * from "./client.js"; -export * from "./routes/commands.js"; +export * from "./routes/application-commands.js"; +export * from "./routes/audit-log.js"; export * from "./routes/guild.js"; export * from "./routes/interactions.js"; diff --git a/packages/discord-api/src/routes/commands.ts b/packages/discord-api/src/routes/application-commands.ts similarity index 100% rename from packages/discord-api/src/routes/commands.ts rename to packages/discord-api/src/routes/application-commands.ts diff --git a/packages/discord-api/src/routes/audit-log.ts b/packages/discord-api/src/routes/audit-log.ts new file mode 100644 index 00000000..9b09a7c3 --- /dev/null +++ b/packages/discord-api/src/routes/audit-log.ts @@ -0,0 +1,16 @@ +import type { RESTGetAPIAuditLogResult } from "discord-api-types/v10"; +import { Routes } from "discord-api-types/v10"; +import type { Snowflake } from "discord-snowflake"; +import { client } from "../client.js"; + +/** + * Get the Audit Log for a Guild. + * {@link https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log | Discord Documentation} + * @param guildId - The Guild ID to get the Audit Log for + * @returns The Audit Log for the Guild + */ +export async function getGuildAuditLog(guildId: Snowflake) { + return client.get( + Routes.guildAuditLog(guildId) + ) as Promise; +} diff --git a/packages/discord-api/src/routes/webhook.ts b/packages/discord-api/src/routes/webhook.ts index 5da25ba7..83945a02 100644 --- a/packages/discord-api/src/routes/webhook.ts +++ b/packages/discord-api/src/routes/webhook.ts @@ -7,11 +7,11 @@ import type { Snowflake } from "discord-snowflake"; import { client } from "../client.js"; /** - * Creates a Channel Webhook - * @link https://discord.com/developers/docs/resources/webhook#create-webhook - * @param channelId The Channel to create the webhook in - * @param data The Webhook name and avatar - * @param auditLogReason An optional entry to add to the audit log + * Creates a Channel Webhook. + * {@link https://discord.com/developers/docs/resources/webhook#create-webhook | Discord Documentation} + * @param channelId - The Channel to create the webhook in + * @param data - The Webhook name and avatar + * @param auditLogReason - An optional entry to add to the audit log * @returns The created Webhook */ export async function createWebhook( From d12c09f43c2d0f9116828a63b073d75c08e19bb6 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Fri, 28 Oct 2022 22:07:07 -0700 Subject: [PATCH 10/13] Add Auto Mod API --- package-lock.json | 20 ++-- packages/discord-api/package.json | 2 +- packages/discord-api/src/headers/audit-log.ts | 9 ++ packages/discord-api/src/index.ts | 1 + .../discord-api/src/routes/auto-moderation.ts | 97 +++++++++++++++++++ packages/discord-api/test/client.test.ts | 5 - packages/discord-api/test/helpers/headers.ts | 18 ---- packages/discord-api/test/webhook.test.ts | 46 --------- packages/interaction-kit/package.json | 2 +- 9 files changed, 119 insertions(+), 81 deletions(-) create mode 100644 packages/discord-api/src/headers/audit-log.ts create mode 100644 packages/discord-api/src/routes/auto-moderation.ts delete mode 100644 packages/discord-api/test/client.test.ts delete mode 100644 packages/discord-api/test/helpers/headers.ts delete mode 100644 packages/discord-api/test/webhook.test.ts diff --git a/package-lock.json b/package-lock.json index 32f43c9d..431fd811 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8031,9 +8031,9 @@ "link": true }, "node_modules/discord-api-types": { - "version": "0.37.4", - "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.4.tgz", - "integrity": "sha512-QgqYlUokWM++hdwvAtgVNLjmFumPBzFy+uWnnfVDiwBXKm+5jXHJPk2lx2eilkv/706UpAJPLSk/uVCY9NocjA==" + "version": "0.37.15", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.15.tgz", + "integrity": "sha512-2SpscsamTtctJflnz1J6cPp99PLs10DNGi2rrOdjID3KhP1nwsoJHPvcLe6zwcYxZnRdA/71M85RCNOQl83u1A==" }, "node_modules/discord-edge-runner": { "resolved": "packages/discord-edge-runner", @@ -22229,7 +22229,7 @@ "license": "MIT", "dependencies": { "debug": "^4.3.4", - "discord-api-types": "^0.37.4", + "discord-api-types": "^0.37.15", "discord-request": "0.0.5", "discord-snowflake": "2.0.0" }, @@ -22343,7 +22343,7 @@ "boxen": "7.0.0", "chalk": "^5.0.1", "discord-api": "0.0.3", - "discord-api-types": "^0.37.4", + "discord-api-types": "^0.37.15", "discord-edge-runner": "0.0.2", "discord-request": "0.0.5", "discord-snowflake": "2.0.0", @@ -27726,15 +27726,15 @@ "version": "file:packages/discord-api", "requires": { "debug": "^4.3.4", - "discord-api-types": "^0.37.4", + "discord-api-types": "^0.37.15", "discord-request": "0.0.5", "discord-snowflake": "2.0.0" } }, "discord-api-types": { - "version": "0.37.4", - "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.4.tgz", - "integrity": "sha512-QgqYlUokWM++hdwvAtgVNLjmFumPBzFy+uWnnfVDiwBXKm+5jXHJPk2lx2eilkv/706UpAJPLSk/uVCY9NocjA==" + "version": "0.37.15", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.15.tgz", + "integrity": "sha512-2SpscsamTtctJflnz1J6cPp99PLs10DNGi2rrOdjID3KhP1nwsoJHPvcLe6zwcYxZnRdA/71M85RCNOQl83u1A==" }, "discord-edge-runner": { "version": "file:packages/discord-edge-runner", @@ -29918,7 +29918,7 @@ "boxen": "7.0.0", "chalk": "^5.0.1", "discord-api": "0.0.3", - "discord-api-types": "^0.37.4", + "discord-api-types": "^0.37.15", "discord-edge-runner": "0.0.2", "discord-request": "0.0.5", "discord-snowflake": "2.0.0", diff --git a/packages/discord-api/package.json b/packages/discord-api/package.json index a41d1768..ba196361 100644 --- a/packages/discord-api/package.json +++ b/packages/discord-api/package.json @@ -25,7 +25,7 @@ "node": ">=18" }, "dependencies": { - "discord-api-types": "^0.37.4", + "discord-api-types": "^0.37.15", "discord-request": "0.0.5", "discord-snowflake": "2.0.0", "debug": "^4.3.4" diff --git a/packages/discord-api/src/headers/audit-log.ts b/packages/discord-api/src/headers/audit-log.ts new file mode 100644 index 00000000..4a722b55 --- /dev/null +++ b/packages/discord-api/src/headers/audit-log.ts @@ -0,0 +1,9 @@ +export function getAuditLogHeaders(auditLogReason: string | undefined) { + const headers = new Headers(); + + if (auditLogReason != null) { + headers.set("X-Audit-Log-Reason", auditLogReason); + } + + return headers; +} diff --git a/packages/discord-api/src/index.ts b/packages/discord-api/src/index.ts index 82e204db..c619548f 100644 --- a/packages/discord-api/src/index.ts +++ b/packages/discord-api/src/index.ts @@ -1,5 +1,6 @@ export * from "./client.js"; export * from "./routes/application-commands.js"; export * from "./routes/audit-log.js"; +export * from "./routes/auto-moderation.js"; export * from "./routes/guild.js"; export * from "./routes/interactions.js"; diff --git a/packages/discord-api/src/routes/auto-moderation.ts b/packages/discord-api/src/routes/auto-moderation.ts new file mode 100644 index 00000000..8df4ec94 --- /dev/null +++ b/packages/discord-api/src/routes/auto-moderation.ts @@ -0,0 +1,97 @@ +import type { + RESTDeleteAPIAutoModerationRuleResult, + RESTGetAPIAutoModerationRuleResult, + RESTGetAPIAutoModerationRulesResult, + RESTPatchAPIAutoModerationRuleResult, + RESTPostAPIAutoModerationRuleJSONBody, + RESTPostAPIAutoModerationRuleResult, +} from "discord-api-types/v10"; +import { Routes } from "discord-api-types/v10"; +import type { Snowflake } from "discord-snowflake"; +import { client } from "../client.js"; +import { getAuditLogHeaders } from "../headers/audit-log.js"; + +/** + * Retrieves a list of all Auto Moderation Rules. + * {@link https://discord.com/developers/docs/resources/auto-moderation#list-auto-moderation-rules-for-guild | Discord Documentation} + * @param guildId - The target Guild to view the Rules in. + * @returns A list of all the Auto Moderation Rules. + */ +export async function getAutoModerationRules(guildId: Snowflake) { + return client.get( + Routes.guildAutoModerationRules(guildId) + ) as Promise; +} + +/** + * Retrieves an Auto Moderation Rule. + * {@link https://discord.com/developers/docs/resources/auto-moderation#get-auto-moderation-rule | Discord Documentation} + * @param guildId - The target Guild to get the Rule in. + * @param ruleId - The target Rule to view. + * @returns The Auto Moderation Rule. + */ +export async function getAutoModerationRule( + guildId: Snowflake, + ruleId: Snowflake +) { + return client.get( + Routes.guildAutoModerationRule(guildId, ruleId) + ) as Promise; +} + +/** + * Creates an Auto Moderation Rule. + * {@link https://discord.com/developers/docs/resources/auto-moderation#create-auto-moderation-rule | Discord Documentation} + * @param guildId - The target Guild to create the Rule in. + * @param data - The data to use for the new Rule. + * @param auditLogReason - An optional Audit Log entry to record this action under. + * @returns The created Auto Moderation Rule. + */ +export async function createAutoModerationRule( + guildId: Snowflake, + data: RESTPostAPIAutoModerationRuleJSONBody, + auditLogReason?: string +) { + return client.post(Routes.guildAutoModerationRules(guildId), { + body: data, + headers: getAuditLogHeaders(auditLogReason), + }) as Promise; +} + +/** + * Updates an Auto Moderation Rule. + * {@link https://discord.com/developers/docs/resources/auto-moderation#modify-auto-moderation-rule | Discord Documentation} + * @param guildId - The target Guild to delete the Rule from. + * @param ruleId - The target Rule to update. + * @param data - The new data to use for the Rule. + * @param auditLogReason - An optional Audit Log entry to record this action under. + * @returns The updated Auto Moderation Rule. + */ +export async function updateAutoModerationRule( + guildId: Snowflake, + ruleId: Snowflake, + data: RESTPostAPIAutoModerationRuleJSONBody, + auditLogReason?: string +) { + return client.patch(Routes.guildAutoModerationRule(guildId, ruleId), { + body: data, + headers: getAuditLogHeaders(auditLogReason), + }) as Promise; +} + +/** + * Deletes an Auto Moderation Rule. + * {@link https://discord.com/developers/docs/resources/auto-moderation#delete-auto-moderation-rule | Discord Documentation} + * @param guildId - The target Guild to delete the Rule from. + * @param ruleId - The target Rule to delete. + * @param auditLogReason - An optional Audit Log entry to record this action under. + */ +export async function deleteAutoModerationRule( + guildId: Snowflake, + ruleId: Snowflake, + auditLogReason?: string +) { + return client.delete(Routes.guildAutoModerationRule(guildId, ruleId), { + headers: getAuditLogHeaders(auditLogReason), + }) as Promise; +} diff --git a/packages/discord-api/test/client.test.ts b/packages/discord-api/test/client.test.ts deleted file mode 100644 index 6388b9ce..00000000 --- a/packages/discord-api/test/client.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { expect, test } from "vitest"; - -test("discord-api", () => { - expect(true).toBe(true); -}); diff --git a/packages/discord-api/test/helpers/headers.ts b/packages/discord-api/test/helpers/headers.ts deleted file mode 100644 index eb9a05ca..00000000 --- a/packages/discord-api/test/helpers/headers.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { expect, vi } from "vitest"; -import { client } from "../../src/client.js"; - -export async function runAuditLogTest( - // TODO: Should probably infer this somehow instead of hardcoding it - method: "post" | "patch" | "delete" | "put" | "get", - callback: () => Promise -) { - const spy = vi.spyOn(client, method).mockImplementation(async () => ""); - - const reason = await callback(); - - expect(spy).toHaveBeenCalledWith(expect.anything(), { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - body: expect.anything(), - headers: new Headers({ "X-Audit-Log-Reason": reason }), - }); -} diff --git a/packages/discord-api/test/webhook.test.ts b/packages/discord-api/test/webhook.test.ts deleted file mode 100644 index 1c563292..00000000 --- a/packages/discord-api/test/webhook.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { describe, expect, test, vi } from "vitest"; -import { client } from "../src/client.js"; -import { createWebhook } from "../src/routes/webhook.js"; -import { runAuditLogTest } from "./helpers/headers.js"; - -describe("createWebhook", () => { - test("Accepts an optional audit log entry", async () => { - await runAuditLogTest("post", async () => { - await createWebhook( - "123", - { name: "Test Webhook", avatar: "avatar.png" }, - "Vitest Log" - ); - return "Vitest Log"; - }); - }); - - test("Accepts an optional audit log entry", async () => { - const spy = vi.spyOn(client, "post").mockImplementation(async () => ""); - - await createWebhook( - "123", - { name: "Test Webhook", avatar: "avatar.png" }, - "Vitest Log" - ); - expect(spy).toHaveBeenCalledWith(expect.anything(), { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - body: expect.anything(), - headers: new Headers({ "X-Audit-Log-Reason": "Vitest Log" }), - }); - }); - - test("Includes webhook information", async () => { - const spy = vi.spyOn(client, "post").mockImplementation(async () => ""); - - await createWebhook("123", { name: "Test Webhook", avatar: "avatar.png" }); - expect(spy).toHaveBeenCalledWith("/channels/123/webhooks", { - body: { - name: "Test Webhook", - avatar: "avatar.png", - }, - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - headers: expect.any(Headers), - }); - }); -}); diff --git a/packages/interaction-kit/package.json b/packages/interaction-kit/package.json index 827fe06a..0ddf8b8a 100644 --- a/packages/interaction-kit/package.json +++ b/packages/interaction-kit/package.json @@ -31,7 +31,7 @@ "boxen": "7.0.0", "chalk": "^5.0.1", "discord-api": "0.0.3", - "discord-api-types": "^0.37.4", + "discord-api-types": "^0.37.15", "discord-edge-runner": "0.0.2", "discord-request": "0.0.5", "discord-snowflake": "2.0.0", From d7dd28d27240dd2be25dd90fdbc38f34ab0f1eee Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 3 Nov 2022 09:41:13 -0700 Subject: [PATCH 11/13] Add Channel Functions --- packages/discord-api/src/routes/audit-log.ts | 4 +- .../discord-api/src/routes/auto-moderation.ts | 4 +- packages/discord-api/src/routes/channel.tsx | 56 +++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 packages/discord-api/src/routes/channel.tsx diff --git a/packages/discord-api/src/routes/audit-log.ts b/packages/discord-api/src/routes/audit-log.ts index 9b09a7c3..14c5b864 100644 --- a/packages/discord-api/src/routes/audit-log.ts +++ b/packages/discord-api/src/routes/audit-log.ts @@ -6,8 +6,8 @@ import { client } from "../client.js"; /** * Get the Audit Log for a Guild. * {@link https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log | Discord Documentation} - * @param guildId - The Guild ID to get the Audit Log for - * @returns The Audit Log for the Guild + * @param guildId - TThe target Guild to get the Audit Log in. + * @returns The Audit Log for the Guild. */ export async function getGuildAuditLog(guildId: Snowflake) { return client.get( diff --git a/packages/discord-api/src/routes/auto-moderation.ts b/packages/discord-api/src/routes/auto-moderation.ts index 8df4ec94..66d32faa 100644 --- a/packages/discord-api/src/routes/auto-moderation.ts +++ b/packages/discord-api/src/routes/auto-moderation.ts @@ -12,7 +12,7 @@ import { client } from "../client.js"; import { getAuditLogHeaders } from "../headers/audit-log.js"; /** - * Retrieves a list of all Auto Moderation Rules. + * Get a list of all Auto Moderation Rules. * {@link https://discord.com/developers/docs/resources/auto-moderation#list-auto-moderation-rules-for-guild | Discord Documentation} * @param guildId - The target Guild to view the Rules in. * @returns A list of all the Auto Moderation Rules. @@ -24,7 +24,7 @@ export async function getAutoModerationRules(guildId: Snowflake) { } /** - * Retrieves an Auto Moderation Rule. + * Get an Auto Moderation Rule. * {@link https://discord.com/developers/docs/resources/auto-moderation#get-auto-moderation-rule | Discord Documentation} * @param guildId - The target Guild to get the Rule in. * @param ruleId - The target Rule to view. diff --git a/packages/discord-api/src/routes/channel.tsx b/packages/discord-api/src/routes/channel.tsx new file mode 100644 index 00000000..b07c8a75 --- /dev/null +++ b/packages/discord-api/src/routes/channel.tsx @@ -0,0 +1,56 @@ +import type { + RESTDeleteAPIChannelResult, + RESTGetAPIChannelResult, + RESTPatchAPIChannelJSONBody, + RESTPatchAPIChannelResult, +} from "discord-api-types/v10"; +import { Routes } from "discord-api-types/v10"; +import type { Snowflake } from "discord-snowflake"; +import { client } from "../client.js"; +import { getAuditLogHeaders } from "../headers/audit-log.js"; + +/** + * Get a Channel. + * {@link https://discord.com/developers/docs/resources/channel#get-channel | Discord Documentation} + * @param channelId - The target Channel to get. + * @returns The Channel. + */ +export async function getChannel(channelId: Snowflake) { + return client.get( + Routes.channel(channelId) + ) as Promise; +} + +/** + * Updates a Channel. + * {@link https://discord.com/developers/docs/resources/channel#modify-channel | Discord Documentation} + * @param channelId - The target Channel to update. + * @param data - The new data to use for the Channel. + * @param auditLogReason - An optional Audit Log entry to record this action under. + * @returns The updated Channel. + */ +export async function updateChannel( + channelId: Snowflake, + data: RESTPatchAPIChannelJSONBody, + auditLogReason?: string +) { + return client.patch(Routes.channel(channelId), { + body: data, + headers: getAuditLogHeaders(auditLogReason), + }) as Promise; +} + +/** + * Deletes a channel or closes a private message. + * @param channelId - The target Channel to delete. + * @param auditLogReason - An optional Audit Log entry to record this action under. + * @returns The deleted Channel. + */ +export async function deleteChannel( + channelId: Snowflake, + auditLogReason?: string +) { + return client.delete(Routes.channel(channelId), { + headers: getAuditLogHeaders(auditLogReason), + }) as Promise; +} From 3b3ee63b5cfa3a5dddb523e0e664f6de6e2e9490 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 3 Nov 2022 09:58:35 -0700 Subject: [PATCH 12/13] Add Message Functions --- packages/discord-api/src/routes/messages.tsx | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 packages/discord-api/src/routes/messages.tsx diff --git a/packages/discord-api/src/routes/messages.tsx b/packages/discord-api/src/routes/messages.tsx new file mode 100644 index 00000000..8d3dd48b --- /dev/null +++ b/packages/discord-api/src/routes/messages.tsx @@ -0,0 +1,62 @@ +import type { + RESTGetAPIChannelMessageResult, + RESTGetAPIChannelMessagesQuery, + RESTGetAPIChannelMessagesResult, + RESTPostAPIChannelMessageJSONBody, + RESTPostAPIChannelMessageResult, +} from "discord-api-types/v10"; +import { Routes } from "discord-api-types/v10"; +import type { Snowflake } from "discord-snowflake"; +import { client } from "../client.js"; + +/** + * Returns an array of Messages for a Channel. + * {@link https://discord.com/developers/docs/resources/channel#get-channel-messages | Discord Documentation} + * @param channelId - The target Channel to get Messages in. + * @returns An array of Messages + */ +export async function getMessages( + channelId: Snowflake, + params?: RESTGetAPIChannelMessagesQuery +) { + const query = new URLSearchParams(); + + if (params) { + Object.entries(params).forEach(([key, value]) => { + query.set(key, value); + }); + } + + return client.get(Routes.channelMessages(channelId), { + query, + }) as Promise; +} + +/** + * Get a Message. + * {@link https://discord.com/developers/docs/resources/channel#get-channel-message | Discord Documentation} + * @param channelId - The target Channel to get the Message in. + * @param messageId - The target Message to get. + * @returns The Message. + */ +export async function getMessage(channelId: Snowflake, messageId: Snowflake) { + return client.get( + Routes.channelMessage(channelId, messageId) + ) as Promise; +} + +/** + * Posts a Message to a Channel. + * {@link https://discord.com/developers/docs/resources/channel#create-message | Discord Documentation} + * @param channelId - The target Channel to post the Message in. + * @param data - The Message content. + * @returns The created Message. + */ +export async function createMessage( + channelId: Snowflake, + data: RESTPostAPIChannelMessageJSONBody +) { + return client.post(Routes.channelMessages(channelId), { + body: data, + }) as Promise; +} From be1764fa812d8cf43032887d26edad05d131e13e Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Thu, 3 Nov 2022 12:53:04 -0700 Subject: [PATCH 13/13] language tweak --- packages/discord-api/src/routes/auto-moderation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord-api/src/routes/auto-moderation.ts b/packages/discord-api/src/routes/auto-moderation.ts index 66d32faa..d6a3206e 100644 --- a/packages/discord-api/src/routes/auto-moderation.ts +++ b/packages/discord-api/src/routes/auto-moderation.ts @@ -61,7 +61,7 @@ export async function createAutoModerationRule( /** * Updates an Auto Moderation Rule. * {@link https://discord.com/developers/docs/resources/auto-moderation#modify-auto-moderation-rule | Discord Documentation} - * @param guildId - The target Guild to delete the Rule from. + * @param guildId - The target Guild to update the Rule in. * @param ruleId - The target Rule to update. * @param data - The new data to use for the Rule. * @param auditLogReason - An optional Audit Log entry to record this action under.