discord-webhook-library
Version:
A powerful and easy-to-use library for creating and sending richly formatted messages to Discord webhooks, with built-in validation and rate-limiting.
116 lines (115 loc) • 5.39 kB
TypeScript
import { Message } from '../builders/Message';
import { AxiosInstance } from 'axios';
export interface WebhookInstance {
id: string;
token: string;
url: string;
axiosInstance: AxiosInstance;
}
export declare class Webhook {
private webhooks;
private messages;
/**
* Creates a new Webhook instance.
* @param url The full Discord webhook URL or an array of URLs.
* @throws {WebhookError} If any webhook URL is invalid or empty.
*/
constructor(url?: string | string[]);
/**
* Adds a webhook URL to the instance.
* @param url The full Discord webhook URL.
* @throws {WebhookError} If the webhook URL is invalid or empty.
*/
addWebhookUrl(url: string): void;
/**
* Returns the number of configured webhook URLs.
* @returns The number of webhook URLs.
*/
getWebhookCount(): number;
/**
* Returns an array of all configured webhook URLs.
* @returns An array of webhook URLs.
**/
getWebhookUrls(): string[];
/**
* Adds a message to the webhook's queue for batch sending.
* @param message The Message object to add to the queue.
* @returns The current Webhook instance.
*/
addMessage(message: Message): this;
/**
* Clears all messages from the webhook's queue.
* @returns The current Webhook instance.
*/
clearMessages(): this;
/**
* Returns an array of JSON payloads for all messages currently in the queue.
* This is useful for inspecting the payload before sending.
* @returns An array of plain objects representing the message payloads.
*/
getPayloads(): Record<string, unknown>[];
/**
* Sends all messages currently in the queue to all configured webhooks.
* Messages are sent sequentially to each webhook. If a message fails to send to any webhook,
* it remains in the queue, and the method will throw an error after attempting to send all messages
* to all webhooks.
* @throws {ValidationError} If any message fails Zod validation.
* @throws {RequestError} If any message fails to send due to a network or Discord API error.
* @throws {WebhookError} For other unexpected errors during the sending process.
*/
send(): Promise<void>;
private _sendOne;
/**
* Sends a file to all configured webhooks.
* @param filePath The path to the file to send.
* @param message An optional Message object to send along with the file.
* @throws {FileSystemError} If the file cannot be read.
* @throws {ValidationError} If the message payload fails Zod validation.
* @throws {RequestError} If the file sending fails due to a network or Discord API error.
* @throws {WebhookError} For other unexpected errors during the file sending process.
*/
sendFile(filePath: string, message?: Message): Promise<void>;
/**
* Sends an informational message with a blue embed to all configured webhooks.
* @param title The title of the embed.
* @param description The description of the embed (optional).
* @throws {ValidationError} If the generated message payload fails Zod validation.
* @throws {RequestError} If the message fails to send due to a network or Discord API error.
* @throws {WebhookError} For other unexpected errors.
*/
info(title: string, description?: string): Promise<void>;
/**
* Sends a success message with a green embed to all configured webhooks.
* @param title The title of the embed.
* @param description The description of the embed (optional).
* @throws {ValidationError} If the generated message payload fails Zod validation.
* @throws {RequestError} If the message fails to send due to a network or Discord API error.
* @throws {WebhookError} For other unexpected errors.
*/
success(title: string, description?: string): Promise<void>;
/**
* Sends a warning message with a yellow embed to all configured webhooks.
* @param title The title of the embed.
* @param description The description of the embed (optional).
* @throws {ValidationError} If the generated message payload fails Zod validation.
* @throws {RequestError} If the message fails to send due to a network or Discord API error.
* @throws {WebhookError} For other unexpected errors.
*/
warning(title: string, description?: string): Promise<void>;
/**
* Sends an error message with a red embed to all configured webhooks.
* @param title The title of the embed.
* @param description The description of the embed (optional).
* @throws {ValidationError} If the generated message payload fails Zod validation.
* @throws {RequestError} If the message fails to send due to a network or Discord API error.
* @throws {WebhookError} For other unexpected errors.
*/
error(title: string, description?: string): Promise<void>;
/**
* Deletes a previously sent webhook message from all configured webhooks.
* @param messageLinkOrId The full message link (e.g., from Discord UI) or just the message ID of the message to delete.
* @throws {RequestError} If the deletion fails due to a network or Discord API error.
* @throws {WebhookError} For other unexpected errors during deletion.
*/
delete(messageLinkOrId: string): Promise<void>;
}