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 <ayyantee@gmail.com>

Co-authored-by: Ayyan <ayyantee@gmail.com>
This commit is contained in:
ITOH
2021-01-24 19:57:27 +00:00
committed by GitHub
parent 417315f89e
commit b7557050a6
2 changed files with 91 additions and 5 deletions
+82 -5
View File
@@ -13,6 +13,7 @@ import {
UpsertSlashCommandOptions, UpsertSlashCommandOptions,
UpsertSlashCommandsOptions, UpsertSlashCommandsOptions,
WebhookCreateOptions, WebhookCreateOptions,
WebhookEditOptions,
WebhookPayload, WebhookPayload,
} from "../../types/mod.ts"; } from "../../types/mod.ts";
import { cache } from "../../util/cache.ts"; import { cache } from "../../util/cache.ts";
@@ -58,6 +59,87 @@ export async function createWebhook(
) as Promise<WebhookPayload>; ) as Promise<WebhookPayload>;
} }
/** 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<WebhookEditOptions, "channelID">,
) {
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 */ /** Execute a webhook with webhook ID and webhook token */
export async function executeWebhook( export async function executeWebhook(
webhookID: string, webhookID: string,
@@ -117,11 +199,6 @@ export async function executeWebhook(
return structures.createMessage(result as MessageCreateOptions); 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( export function editWebhookMessage(
webhookID: string, webhookID: string,
webhookToken: string, webhookToken: string,
+9
View File
@@ -36,6 +36,15 @@ export interface WebhookCreateOptions {
avatar?: string; 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 { 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) */ /** 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; wait?: boolean;