Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Missing discord-api Methods #140

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
73 changes: 52 additions & 21 deletions packages/discord-api/src/routes/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {
RESTGetAPIApplicationCommandResult,
RESTGetAPIApplicationCommandsResult,
RESTGetAPIApplicationGuildCommandResult,
RESTGetAPIApplicationGuildCommandsResult,
RESTPatchAPIApplicationCommandJSONBody,
RESTPatchAPIApplicationCommandResult,
Expand All @@ -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(
Expand All @@ -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
) {
Expand All @@ -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<RESTPutAPIApplicationCommandsResult>;
return client.get(
Routes.applicationCommand(applicationId, commandId)
) as Promise<RESTGetAPIApplicationCommandResult>;
}

// TODO: Test, Document
export async function patchGlobalApplicationCommand(
export async function editGlobalApplicationCommand(
applicationId: Snowflake,
command: RESTPatchAPIApplicationCommandJSONBody & { id: Snowflake }
) {
Expand All @@ -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<RESTPutAPIApplicationCommandsResult>;
}

/**
* Guild Commands
*/

// TODO: Test, Document
export async function getGuildApplicationCommands(
applicationId: Snowflake,
Expand All @@ -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
Expand All @@ -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<RESTPutAPIApplicationGuildCommandsResult>;
return client.get(
Routes.applicationGuildCommand(applicationId, guildId, commandId)
) as Promise<RESTGetAPIApplicationGuildCommandResult>;
}

// 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<RESTPatchAPIApplicationGuildCommandResult>;
return client.put(Routes.applicationGuildCommands(applicationId, guildId), {
body: commands,
}) as Promise<RESTPutAPIApplicationGuildCommandsResult>;
}

// TODO: Test, Document
Expand All @@ -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<RESTPatchAPIApplicationGuildCommandResult>;
}
14 changes: 12 additions & 2 deletions packages/discord-api/src/routes/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,8 +20,18 @@ export async function postInteractionFollowup(
}) as Promise<RESTPostAPIInteractionFollowupResult>;
}

// export async function getInteractionFollowup(
// applicationId: Snowflake,
// interactionToken: string,
// messageId: Snowflake
// ) {
// return client.get(
// Routes.webhook(applicationId, interactionToken, messageId)
// ) as Promise<RESTGetAPIInteractionFollowupResult>;
// }

// TODO: Test, Document
export async function patchInteractionFollowup(
export async function editInteractionFollowup(
applicationId: Snowflake,
interactionToken: string,
id: string,
Expand Down
61 changes: 61 additions & 0 deletions packages/discord-api/src/routes/webhook.ts
Original file line number Diff line number Diff line change
@@ -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<RESTPostAPIChannelWebhookResult>;
}

/**
* 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
*/
27 changes: 27 additions & 0 deletions packages/discord-api/test/webhook.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});