From b7557050a60ffff5342dae278827a853aede5b2f Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Sun, 24 Jan 2021 19:57:27 +0000 Subject: [PATCH] feat(handlers): add editWebhook*(), deleteWebhook*(), getWebhook*() (#438) * feat(handlers): add getWebhookWithToken * Update webhook.ts * remove(utils): dry constants * feat(handlers): add more webhook functions * move functions arround * well that function is not needed * better return types * Apply suggestions from code review Co-authored-by: Ayyan Co-authored-by: Ayyan --- src/api/handlers/webhook.ts | 87 ++++++++++++++++++++++++++++++++++--- src/types/webhook.ts | 9 ++++ 2 files changed, 91 insertions(+), 5 deletions(-) diff --git a/src/api/handlers/webhook.ts b/src/api/handlers/webhook.ts index 3472f70f2..9df852ea5 100644 --- a/src/api/handlers/webhook.ts +++ b/src/api/handlers/webhook.ts @@ -13,6 +13,7 @@ import { UpsertSlashCommandOptions, UpsertSlashCommandsOptions, WebhookCreateOptions, + WebhookEditOptions, WebhookPayload, } from "../../types/mod.ts"; import { cache } from "../../util/cache.ts"; @@ -58,6 +59,87 @@ export async function createWebhook( ) as Promise; } +/** Edit a webhook. Requires the `MANAGE_WEBHOOKS` permission. Returns the updated webhook object on success. */ +export async function editWebhook( + channelID: string, + webhookID: string, + options: WebhookEditOptions, +) { + const hasManageWebhooksPerm = await botHasChannelPermissions( + channelID, + ["MANAGE_WEBHOOKS"], + ); + if ( + !hasManageWebhooksPerm + ) { + throw new Error(Errors.MISSING_MANAGE_WEBHOOKS); + } + + const result = await RequestManager.patch(endpoints.WEBHOOK_ID(webhookID), { + ...options, + channel_id: options.channelID, + }); + + return result as WebhookPayload; +} + +/** Edit a webhook. Returns the updated webhook object on success. */ +export async function editWebhookWithToken( + webhookID: string, + webhookToken: string, + options: Omit, +) { + const result = await RequestManager.patch( + endpoints.WEBHOOK(webhookID, webhookToken), + options, + ); + + return result as WebhookPayload; +} + +/** Delete a webhook permanently. Requires the `MANAGE_WEBHOOKS` permission. Returns a undefined on success */ +export async function deleteWebhook(channelID: string, webhookID: string) { + const hasManageWebhooksPerm = await botHasChannelPermissions( + channelID, + ["MANAGE_WEBHOOKS"], + ); + if ( + !hasManageWebhooksPerm + ) { + throw new Error(Errors.MISSING_MANAGE_WEBHOOKS); + } + + const result = await RequestManager.delete(endpoints.WEBHOOK_ID(webhookID)); + + return result; +} + +/** Delete a webhook permanently. Returns a undefined on success */ +export async function deleteWebhookWithToken( + webhookID: string, + webhookToken: string, +) { + const result = await RequestManager.delete( + endpoints.WEBHOOK(webhookID, webhookToken), + ); + + return result; +} + +/** Returns the new webhook object for the given id. */ +export async function getWebhook(webhookID: string) { + const result = await RequestManager.get(endpoints.WEBHOOK_ID(webhookID)); + return result as WebhookPayload; +} + +/** 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 RequestManager.get( + endpoints.WEBHOOK(webhookID, token), + ); + return result as WebhookPayload; +} + /** Execute a webhook with webhook ID and webhook token */ export async function executeWebhook( webhookID: string, @@ -117,11 +199,6 @@ export async function executeWebhook( return structures.createMessage(result as MessageCreateOptions); } -/** Returns the new webhook object for the given id. */ -export function getWebhook(webhookID: string) { - return RequestManager.get(endpoints.WEBHOOK_ID(webhookID)); -} - export function editWebhookMessage( webhookID: string, webhookToken: string, diff --git a/src/types/webhook.ts b/src/types/webhook.ts index 27ccb2e87..8d7ae47c4 100644 --- a/src/types/webhook.ts +++ b/src/types/webhook.ts @@ -36,6 +36,15 @@ export interface WebhookCreateOptions { avatar?: string; } +export interface WebhookEditOptions { + /** Name of the webhook (1-80 characters) */ + name?: string; + /** Image url for avatar image for the default webhook avatar */ + avatar?: string | null; + /** The new channel id this webhook should be moved to */ + channelID?: string; +} + export interface ExecuteWebhookOptions { /** waits for server confirmation of message send before response, and returns the created message body (defaults to false; when false a message that is not saved does not return an error) */ wait?: boolean;