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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/discord-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions packages/discord-api/src/headers/audit-log.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 3 additions & 1 deletion packages/discord-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from "./client.js";
export * from "./routes/commands.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";
16 changes: 16 additions & 0 deletions packages/discord-api/src/routes/audit-log.ts
Original file line number Diff line number Diff line change
@@ -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 - TThe target Guild to get the Audit Log in.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double T

* @returns The Audit Log for the Guild.
*/
export async function getGuildAuditLog(guildId: Snowflake) {
return client.get(
Routes.guildAuditLog(guildId)
) as Promise<RESTGetAPIAuditLogResult>;
}
97 changes: 97 additions & 0 deletions packages/discord-api/src/routes/auto-moderation.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
* 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.
*/
export async function getAutoModerationRules(guildId: Snowflake) {
return client.get(
Routes.guildAutoModerationRules(guildId)
) as Promise<RESTGetAPIAutoModerationRulesResult>;
}

/**
* 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.
* @returns The Auto Moderation Rule.
*/
export async function getAutoModerationRule(
guildId: Snowflake,
ruleId: Snowflake
) {
return client.get(
Routes.guildAutoModerationRule(guildId, ruleId)
) as Promise<RESTGetAPIAutoModerationRuleResult>;
}

/**
* 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<RESTPostAPIAutoModerationRuleResult>;
}

/**
* 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 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.
* @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<RESTPatchAPIAutoModerationRuleResult>;
}

/**
* 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<RESTDeleteAPIAutoModerationRuleResult>;
}
56 changes: 56 additions & 0 deletions packages/discord-api/src/routes/channel.tsx
Original file line number Diff line number Diff line change
@@ -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<RESTGetAPIChannelResult>;
}

/**
* 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<RESTPatchAPIChannelResult>;
}

/**
* 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<RESTDeleteAPIChannelResult>;
}
62 changes: 62 additions & 0 deletions packages/discord-api/src/routes/messages.tsx
Original file line number Diff line number Diff line change
@@ -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<RESTGetAPIChannelMessagesResult>;
}

/**
* 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<RESTGetAPIChannelMessageResult>;
}

/**
* 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<RESTPostAPIChannelMessageResult>;
}
32 changes: 32 additions & 0 deletions packages/discord-api/src/routes/webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.js";

/**
* 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(
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>;
}
5 changes: 0 additions & 5 deletions packages/discord-api/test/client.test.ts

This file was deleted.

27 changes: 27 additions & 0 deletions packages/discord-api/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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",
]
`);
});
2 changes: 1 addition & 1 deletion packages/interaction-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down