mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 19:28:17 +00:00
webhooks
This commit is contained in:
@@ -2,10 +2,9 @@ import { rest } from "../../rest/rest.ts";
|
|||||||
import { Errors } from "../../types/misc/errors.ts";
|
import { Errors } from "../../types/misc/errors.ts";
|
||||||
import { CreateWebhook } from "../../types/webhooks/create_webhook.ts";
|
import { CreateWebhook } from "../../types/webhooks/create_webhook.ts";
|
||||||
import { Webhook } from "../../types/webhooks/webhook.ts";
|
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||||
import { DiscordWebhook } from "../../types/webhooks/webhook.ts";
|
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
||||||
import { snakeKeysToCamelCase, urlToBase64 } from "../../util/utils.ts";
|
import { urlToBase64 } from "../../util/utils.ts";
|
||||||
import { validateLength } from "../../util/validate_length.ts";
|
import { validateLength } from "../../util/validate_length.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,7 +26,7 @@ export async function createWebhook(
|
|||||||
throw new Error(Errors.INVALID_WEBHOOK_NAME);
|
throw new Error(Errors.INVALID_WEBHOOK_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result: DiscordWebhook = await rest.runMethod(
|
return await rest.runMethod<Webhook>(
|
||||||
"post",
|
"post",
|
||||||
endpoints.CHANNEL_WEBHOOKS(channelId),
|
endpoints.CHANNEL_WEBHOOKS(channelId),
|
||||||
{
|
{
|
||||||
@@ -35,6 +34,4 @@ export async function createWebhook(
|
|||||||
avatar: options.avatar ? await urlToBase64(options.avatar) : undefined,
|
avatar: options.avatar ? await urlToBase64(options.avatar) : undefined,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase<Webhook>(result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,8 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
|||||||
export async function deleteWebhook(channelId: string, webhookId: string) {
|
export async function deleteWebhook(channelId: string, webhookId: string) {
|
||||||
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
|
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
|
||||||
|
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<undefined>(
|
||||||
"delete",
|
"delete",
|
||||||
endpoints.WEBHOOK_ID(webhookId),
|
endpoints.WEBHOOK_ID(webhookId),
|
||||||
);
|
);
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,8 @@ export async function deleteWebhookMessage(
|
|||||||
webhookToken: string,
|
webhookToken: string,
|
||||||
messageId: string,
|
messageId: string,
|
||||||
) {
|
) {
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<undefined>(
|
||||||
"delete",
|
"delete",
|
||||||
endpoints.WEBHOOK_MESSAGE(webhookId, webhookToken, messageId),
|
endpoints.WEBHOOK_MESSAGE(webhookId, webhookToken, messageId),
|
||||||
);
|
);
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,8 @@ export async function deleteWebhookWithToken(
|
|||||||
webhookId: string,
|
webhookId: string,
|
||||||
webhookToken: string,
|
webhookToken: string,
|
||||||
) {
|
) {
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<undefined>(
|
||||||
"delete",
|
"delete",
|
||||||
endpoints.WEBHOOK(webhookId, webhookToken),
|
endpoints.WEBHOOK(webhookId, webhookToken),
|
||||||
);
|
);
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { ModifyWebhook } from "../../types/webhooks/modify_webhook.ts";
|
import { ModifyWebhook } from "../../types/webhooks/modify_webhook.ts";
|
||||||
import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.ts";
|
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
import { requireBotChannelPermissions } from "../../util/permissions.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Edit a webhook. Requires the `MANAGE_WEBHOOKS` permission. Returns the updated webhook object on success. */
|
/** Edit a webhook. Requires the `MANAGE_WEBHOOKS` permission. Returns the updated webhook object on success. */
|
||||||
export async function editWebhook(
|
export async function editWebhook(
|
||||||
@@ -13,7 +12,7 @@ export async function editWebhook(
|
|||||||
) {
|
) {
|
||||||
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
|
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
|
||||||
|
|
||||||
const result: DiscordWebhook = await rest.runMethod(
|
return await rest.runMethod<Webhook>(
|
||||||
"patch",
|
"patch",
|
||||||
endpoints.WEBHOOK_ID(webhookId),
|
endpoints.WEBHOOK_ID(webhookId),
|
||||||
{
|
{
|
||||||
@@ -21,6 +20,4 @@ export async function editWebhook(
|
|||||||
channel_id: options.channelId,
|
channel_id: options.channelId,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase<Webhook>(result);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { structures } from "../../structures/mod.ts";
|
import { structures } from "../../structures/mod.ts";
|
||||||
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
|
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
|
||||||
import { DiscordMessage } from "../../types/messages/message.ts";
|
import { Message } from "../../types/messages/message.ts";
|
||||||
import { Errors } from "../../types/misc/errors.ts";
|
import { Errors } from "../../types/misc/errors.ts";
|
||||||
import { EditWebhookMessage } from "../../types/webhooks/edit_webhook_message.ts";
|
import { EditWebhookMessage } from "../../types/webhooks/edit_webhook_message.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
@@ -60,12 +60,11 @@ export async function editWebhookMessage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = (await rest.runMethod(
|
const result = await rest.runMethod<Message>(
|
||||||
"patch",
|
"patch",
|
||||||
endpoints.WEBHOOK_MESSAGE(webhookId, webhookToken, messageId),
|
endpoints.WEBHOOK_MESSAGE(webhookId, webhookToken, messageId),
|
||||||
{ ...options, allowedMentions: options.allowedMentions },
|
{ ...options, allowedMentions: options.allowedMentions },
|
||||||
)) as DiscordMessage;
|
);
|
||||||
|
|
||||||
const message = await structures.createDiscordenoMessage(result);
|
return await structures.createDiscordenoMessage(result);
|
||||||
return message;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { rest } from "../../rest/rest.ts";
|
|||||||
import { ModifyWebhook } from "../../types/webhooks/modify_webhook.ts";
|
import { ModifyWebhook } from "../../types/webhooks/modify_webhook.ts";
|
||||||
import { Webhook } from "../../types/webhooks/webhook.ts";
|
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Edit a webhook. Returns the updated webhook object on success. */
|
/** Edit a webhook. Returns the updated webhook object on success. */
|
||||||
export async function editWebhookWithToken(
|
export async function editWebhookWithToken(
|
||||||
@@ -10,11 +9,9 @@ export async function editWebhookWithToken(
|
|||||||
webhookToken: string,
|
webhookToken: string,
|
||||||
options: Omit<ModifyWebhook, "channelId">,
|
options: Omit<ModifyWebhook, "channelId">,
|
||||||
) {
|
) {
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<Webhook>(
|
||||||
"patch",
|
"patch",
|
||||||
endpoints.WEBHOOK(webhookId, webhookToken),
|
endpoints.WEBHOOK(webhookId, webhookToken),
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase(result) as Webhook;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { structures } from "../../structures/mod.ts";
|
import { structures } from "../../structures/mod.ts";
|
||||||
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
|
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
|
||||||
import { DiscordMessage } from "../../types/messages/message.ts";
|
import { Message } from "../../types/messages/message.ts";
|
||||||
import { Errors } from "../../types/misc/errors.ts";
|
import { Errors } from "../../types/misc/errors.ts";
|
||||||
import { ExecuteWebhook } from "../../types/webhooks/execute_webhook.ts";
|
import { ExecuteWebhook } from "../../types/webhooks/execute_webhook.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
@@ -64,7 +64,7 @@ export async function executeWebhook(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await rest.runMethod(
|
const result = await rest.runMethod<Message>(
|
||||||
"post",
|
"post",
|
||||||
`${endpoints.WEBHOOK(webhookId, webhookToken)}${
|
`${endpoints.WEBHOOK(webhookId, webhookToken)}${
|
||||||
options.wait ? "?wait=true" : ""
|
options.wait ? "?wait=true" : ""
|
||||||
@@ -75,7 +75,8 @@ export async function executeWebhook(
|
|||||||
avatar_url: options.avatarUrl,
|
avatar_url: options.avatarUrl,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
// TODO: not sure
|
||||||
if (!options.wait) return;
|
if (!options.wait) return;
|
||||||
|
|
||||||
return structures.createDiscordenoMessage(result as DiscordMessage);
|
return structures.createDiscordenoMessage(result);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { Webhook } from "../../types/webhooks/webhook.ts";
|
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Returns the new webhook object for the given id. */
|
/** Returns the new webhook object for the given id. */
|
||||||
export async function getWebhook(webhookId: string) {
|
export async function getWebhook(webhookId: string) {
|
||||||
const result = await rest.runMethod("get", endpoints.WEBHOOK_ID(webhookId));
|
return await rest.runMethod<Webhook>("get", endpoints.WEBHOOK_ID(webhookId));
|
||||||
|
|
||||||
return snakeKeysToCamelCase(result) as Webhook;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { Webhook } from "../../types/webhooks/webhook.ts";
|
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Returns the new webhook object for the given id, this call does not require authentication and returns no user in the webhook object. */
|
/** Returns the new webhook object for the given id, this call does not require authentication and returns no user in the webhook object. */
|
||||||
export async function getWebhookWithToken(webhookId: string, token: string) {
|
export async function getWebhookWithToken(webhookId: string, token: string) {
|
||||||
const result = await rest.runMethod(
|
return await rest.runMethod<Webhook>(
|
||||||
"get",
|
"get",
|
||||||
endpoints.WEBHOOK(webhookId, token),
|
endpoints.WEBHOOK(webhookId, token),
|
||||||
);
|
);
|
||||||
|
|
||||||
return snakeKeysToCamelCase(result) as Webhook;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,19 @@
|
|||||||
import { rest } from "../../rest/rest.ts";
|
import { rest } from "../../rest/rest.ts";
|
||||||
import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.ts";
|
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||||
import { Collection } from "../../util/collection.ts";
|
import { Collection } from "../../util/collection.ts";
|
||||||
import { endpoints } from "../../util/constants.ts";
|
import { endpoints } from "../../util/constants.ts";
|
||||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
|
||||||
|
|
||||||
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
|
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
|
||||||
export async function getWebhooks(guildId: string) {
|
export async function getWebhooks(guildId: string) {
|
||||||
await requireBotGuildPermissions(guildId, ["MANAGE_WEBHOOKS"]);
|
await requireBotGuildPermissions(guildId, ["MANAGE_WEBHOOKS"]);
|
||||||
|
|
||||||
const result = (await rest.runMethod(
|
const result = await rest.runMethod<Webhook[]>(
|
||||||
"get",
|
"get",
|
||||||
endpoints.GUILD_WEBHOOKS(guildId),
|
endpoints.GUILD_WEBHOOKS(guildId),
|
||||||
)) as DiscordWebhook[];
|
);
|
||||||
|
|
||||||
return new Collection(
|
return new Collection(
|
||||||
result.map((webhook) => [
|
result.map((webhook) => [webhook.id, webhook]),
|
||||||
webhook.id,
|
|
||||||
snakeKeysToCamelCase<Webhook>(webhook),
|
|
||||||
]),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user