UNPKG

discord-guildpeek

Version:

Get a public preview of a Discord server from its invite code, without authentication.

25 lines (24 loc) 1.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractDiscordInviteId = extractDiscordInviteId; /** * Extracts the invite ID from a Discord invite URL. * * Accepts invite links in the following formats: * - `https://discord.gg/{id}` * - `https://discord.com/invite/{id}` * * @param url - The Discord invite URL to extract the invite ID from. * @returns The extracted invite ID as a string. * @throws {Error} If the URL does not match the expected Discord invite link formats. */ function extractDiscordInviteId(url) { // Regular expression to match Discord invite links // Accepts both formats: https://discord.gg/{id} and https://discord.com const match = url.match(/^https:\/\/(www\.)?(discord\.gg|discord\.com\/invite)\/([a-zA-Z0-9-_]+)\/?$/); // If the URL does not match the expected format, throw an error if (!match) { throw new Error("Invalid Discord invite link. Accepted formats: https://discord.gg/{id} or https://discord.com/invite/{id}"); } return match[3]; // The invite ID }