@humanwhocodes/crosspost
Version:
A utility to post across multiple social networks.
203 lines (202 loc) • 5.64 kB
TypeScript
/**
* @typedef {Object} DiscordWebhookOptions
* @property {string} webhookUrl The Discord webhook URL.
*/
/**
* @typedef {Object} DiscordWebhookResponse
* @property {string} id The ID of the created message.
* @property {string} channel_id The ID of the channel the message was posted to.
* @property {string} content The content of the message.
* @property {string} timestamp The timestamp of the message.
* @property {string} webhook_id The ID of the webhook that posted the message.
* @property {number} type The type of the message.
* @property {string} [guild_id] The ID of the guild the message was posted to (optional).
*/
/**
* @typedef {Object} DiscordWebhookErrorResponse
* @property {number} code The error code.
* @property {string} message The error message.
*/
/** @typedef {import("../types.js").PostOptions} PostOptions */
/**
* @typedef {Object} DiscordPayload
* @property {string} content The text content of the message
* @property {DiscordEmbed[]} [embeds] Array of embedded messages
* @property {DiscordMessageReference} [message_reference] Reference to another message
* @property {DiscordAttachment[]} [attachments] Array of file attachments
*/
/**
* @typedef {Object} DiscordEmbedImage
* @property {string} url URL of the image
*/
/**
* @typedef {Object} DiscordEmbed
* @property {string} [title] The title of the embed
* @property {string} [description] The description of the embed
* @property {DiscordEmbedImage} [thumbnail] The thumbnail image
* @property {DiscordEmbedImage} [image] The main image
*/
/**
* @typedef {Object} DiscordMessageReference
* @property {string} message_id The ID of the message being referenced
*/
/**
* @typedef {Object} DiscordAttachment
* @property {number} id The unique identifier for the attachment
* @property {string} description A description of the attachment
* @property {string} filename The filename of the attachment
*/
/**
* A strategy for posting messages to Discord via webhooks.
*/
export class DiscordWebhookStrategy {
/**
* Creates a new instance.
* @param {DiscordWebhookOptions} options Options for the instance.
* @throws {Error} When options are missing.
*/
constructor(options: DiscordWebhookOptions);
/**
* Maximum length of a Discord webhook message in characters.
* @type {number}
* @const
*/
MAX_MESSAGE_LENGTH: number;
/**
* The ID of the strategy.
* @type {string}
* @readonly
*/
readonly id: string;
/**
* The display name of the strategy.
* @type {string}
* @readonly
*/
readonly name: string;
/**
* Calculates the length of a message according to Discord's algorithm.
* All characters are counted as is.
* @param {string} message The message to calculate the length of.
* @returns {number} The calculated length of the message.
*/
calculateMessageLength(message: string): number;
/**
* Posts a message to Discord.
* @param {string} message The message to post.
* @param {PostOptions} [postOptions] Additional options for the post.
* @returns {Promise<DiscordWebhookResponse>} A promise that resolves with the message data.
* @throws {Error} When the message fails to post.
*/
post(message: string, postOptions?: PostOptions): Promise<DiscordWebhookResponse>;
#private;
}
export type DiscordWebhookOptions = {
/**
* The Discord webhook URL.
*/
webhookUrl: string;
};
export type DiscordWebhookResponse = {
/**
* The ID of the created message.
*/
id: string;
/**
* The ID of the channel the message was posted to.
*/
channel_id: string;
/**
* The content of the message.
*/
content: string;
/**
* The timestamp of the message.
*/
timestamp: string;
/**
* The ID of the webhook that posted the message.
*/
webhook_id: string;
/**
* The type of the message.
*/
type: number;
/**
* The ID of the guild the message was posted to (optional).
*/
guild_id?: string | undefined;
};
export type DiscordWebhookErrorResponse = {
/**
* The error code.
*/
code: number;
/**
* The error message.
*/
message: string;
};
export type PostOptions = import("../types.js").PostOptions;
export type DiscordPayload = {
/**
* The text content of the message
*/
content: string;
/**
* Array of embedded messages
*/
embeds?: DiscordEmbed[] | undefined;
/**
* Reference to another message
*/
message_reference?: DiscordMessageReference | undefined;
/**
* Array of file attachments
*/
attachments?: DiscordAttachment[] | undefined;
};
export type DiscordEmbedImage = {
/**
* URL of the image
*/
url: string;
};
export type DiscordEmbed = {
/**
* The title of the embed
*/
title?: string | undefined;
/**
* The description of the embed
*/
description?: string | undefined;
/**
* The thumbnail image
*/
thumbnail?: DiscordEmbedImage | undefined;
/**
* The main image
*/
image?: DiscordEmbedImage | undefined;
};
export type DiscordMessageReference = {
/**
* The ID of the message being referenced
*/
message_id: string;
};
export type DiscordAttachment = {
/**
* The unique identifier for the attachment
*/
id: number;
/**
* A description of the attachment
*/
description: string;
/**
* The filename of the attachment
*/
filename: string;
};