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 { CreateWebhook } from "../../types/webhooks/create_webhook.ts";
|
||||
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||
import { DiscordWebhook } from "../../types/webhooks/webhook.ts";
|
||||
import { endpoints } from "../../util/constants.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";
|
||||
|
||||
/**
|
||||
@@ -27,7 +26,7 @@ export async function createWebhook(
|
||||
throw new Error(Errors.INVALID_WEBHOOK_NAME);
|
||||
}
|
||||
|
||||
const result: DiscordWebhook = await rest.runMethod(
|
||||
return await rest.runMethod<Webhook>(
|
||||
"post",
|
||||
endpoints.CHANNEL_WEBHOOKS(channelId),
|
||||
{
|
||||
@@ -35,6 +34,4 @@ export async function createWebhook(
|
||||
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) {
|
||||
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
|
||||
|
||||
const result = await rest.runMethod(
|
||||
return await rest.runMethod<undefined>(
|
||||
"delete",
|
||||
endpoints.WEBHOOK_ID(webhookId),
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -6,10 +6,8 @@ export async function deleteWebhookMessage(
|
||||
webhookToken: string,
|
||||
messageId: string,
|
||||
) {
|
||||
const result = await rest.runMethod(
|
||||
return await rest.runMethod<undefined>(
|
||||
"delete",
|
||||
endpoints.WEBHOOK_MESSAGE(webhookId, webhookToken, messageId),
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -6,10 +6,8 @@ export async function deleteWebhookWithToken(
|
||||
webhookId: string,
|
||||
webhookToken: string,
|
||||
) {
|
||||
const result = await rest.runMethod(
|
||||
return await rest.runMethod<undefined>(
|
||||
"delete",
|
||||
endpoints.WEBHOOK(webhookId, webhookToken),
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { rest } from "../../rest/rest.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 { 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. */
|
||||
export async function editWebhook(
|
||||
@@ -13,7 +12,7 @@ export async function editWebhook(
|
||||
) {
|
||||
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
|
||||
|
||||
const result: DiscordWebhook = await rest.runMethod(
|
||||
return await rest.runMethod<Webhook>(
|
||||
"patch",
|
||||
endpoints.WEBHOOK_ID(webhookId),
|
||||
{
|
||||
@@ -21,6 +20,4 @@ export async function editWebhook(
|
||||
channel_id: options.channelId,
|
||||
},
|
||||
);
|
||||
|
||||
return snakeKeysToCamelCase<Webhook>(result);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { structures } from "../../structures/mod.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 { EditWebhookMessage } from "../../types/webhooks/edit_webhook_message.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",
|
||||
endpoints.WEBHOOK_MESSAGE(webhookId, webhookToken, messageId),
|
||||
{ ...options, allowedMentions: options.allowedMentions },
|
||||
)) as DiscordMessage;
|
||||
);
|
||||
|
||||
const message = await structures.createDiscordenoMessage(result);
|
||||
return message;
|
||||
return await structures.createDiscordenoMessage(result);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import { rest } from "../../rest/rest.ts";
|
||||
import { ModifyWebhook } from "../../types/webhooks/modify_webhook.ts";
|
||||
import { Webhook } from "../../types/webhooks/webhook.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
||||
|
||||
/** Edit a webhook. Returns the updated webhook object on success. */
|
||||
export async function editWebhookWithToken(
|
||||
@@ -10,11 +9,9 @@ export async function editWebhookWithToken(
|
||||
webhookToken: string,
|
||||
options: Omit<ModifyWebhook, "channelId">,
|
||||
) {
|
||||
const result = await rest.runMethod(
|
||||
return await rest.runMethod<Webhook>(
|
||||
"patch",
|
||||
endpoints.WEBHOOK(webhookId, webhookToken),
|
||||
options,
|
||||
);
|
||||
|
||||
return snakeKeysToCamelCase(result) as Webhook;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { structures } from "../../structures/mod.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 { ExecuteWebhook } from "../../types/webhooks/execute_webhook.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",
|
||||
`${endpoints.WEBHOOK(webhookId, webhookToken)}${
|
||||
options.wait ? "?wait=true" : ""
|
||||
@@ -75,7 +75,8 @@ export async function executeWebhook(
|
||||
avatar_url: options.avatarUrl,
|
||||
},
|
||||
);
|
||||
// TODO: not sure
|
||||
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 { Webhook } from "../../types/webhooks/webhook.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { snakeKeysToCamelCase } from "../../util/utils.ts";
|
||||
|
||||
/** Returns the new webhook object for the given id. */
|
||||
export async function getWebhook(webhookId: string) {
|
||||
const result = await rest.runMethod("get", endpoints.WEBHOOK_ID(webhookId));
|
||||
|
||||
return snakeKeysToCamelCase(result) as Webhook;
|
||||
return await rest.runMethod<Webhook>("get", endpoints.WEBHOOK_ID(webhookId));
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { Webhook } from "../../types/webhooks/webhook.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. */
|
||||
export async function getWebhookWithToken(webhookId: string, token: string) {
|
||||
const result = await rest.runMethod(
|
||||
return await rest.runMethod<Webhook>(
|
||||
"get",
|
||||
endpoints.WEBHOOK(webhookId, token),
|
||||
);
|
||||
|
||||
return snakeKeysToCamelCase(result) as Webhook;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
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 { endpoints } from "../../util/constants.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. */
|
||||
export async function getWebhooks(guildId: string) {
|
||||
await requireBotGuildPermissions(guildId, ["MANAGE_WEBHOOKS"]);
|
||||
|
||||
const result = (await rest.runMethod(
|
||||
const result = await rest.runMethod<Webhook[]>(
|
||||
"get",
|
||||
endpoints.GUILD_WEBHOOKS(guildId),
|
||||
)) as DiscordWebhook[];
|
||||
);
|
||||
|
||||
return new Collection(
|
||||
result.map((webhook) => [
|
||||
webhook.id,
|
||||
snakeKeysToCamelCase<Webhook>(webhook),
|
||||
]),
|
||||
result.map((webhook) => [webhook.id, webhook]),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user