mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(util): parseWebhookURL (#8166)
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
const BaseClient = require('./BaseClient');
|
||||
const { Error, ErrorCodes } = require('../errors');
|
||||
const Webhook = require('../structures/Webhook');
|
||||
const { parseWebhookURL } = require('../util/Util');
|
||||
|
||||
/**
|
||||
* The webhook client.
|
||||
@@ -28,14 +29,12 @@ class WebhookClient extends BaseClient {
|
||||
let { id, token } = data;
|
||||
|
||||
if ('url' in data) {
|
||||
const url = data.url.match(
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
/https?:\/\/(?:ptb\.|canary\.)?discord\.com\/api(?:\/v\d{1,2})?\/webhooks\/(\d{17,19})\/([\w-]{68})/i,
|
||||
);
|
||||
const parsed = parseWebhookURL(data.url);
|
||||
if (!parsed) {
|
||||
throw new Error(ErrorCodes.WebhookURLInvalid);
|
||||
}
|
||||
|
||||
if (!url || url.length <= 1) throw new Error(ErrorCodes.WebhookURLInvalid);
|
||||
|
||||
[, id, token] = url;
|
||||
({ id, token } = parsed);
|
||||
}
|
||||
|
||||
this.id = id;
|
||||
|
||||
@@ -528,6 +528,32 @@ function lazy(cb) {
|
||||
return () => (defaultValue ??= cb());
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the credentials used for a given webhook
|
||||
* @typedef {Object} WebhookCredentials
|
||||
* @property {string} id The webhook's id
|
||||
* @property {string} token The webhook's token
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parses a webhook URL for the id and token
|
||||
* @param {string} url The URL to parse
|
||||
* @returns {?WebhookCredentials} Null if the URL is invalid, otherwise the id and the token
|
||||
*/
|
||||
function parseWebhookURL(url) {
|
||||
const matches = url.match(
|
||||
/https?:\/\/(?:ptb\.|canary\.)?discord\.com\/api(?:\/v\d{1,2})?\/webhooks\/(\d{17,19})\/([\w-]{68})/i,
|
||||
);
|
||||
|
||||
if (!matches || matches.length <= 2) return null;
|
||||
|
||||
const [, id, token] = matches;
|
||||
return {
|
||||
id,
|
||||
token,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
flatten,
|
||||
escapeMarkdown,
|
||||
@@ -554,6 +580,7 @@ module.exports = {
|
||||
cleanContent,
|
||||
cleanCodeBlockContent,
|
||||
lazy,
|
||||
parseWebhookURL,
|
||||
};
|
||||
|
||||
// Fixes Circular
|
||||
|
||||
1
packages/discord.js/typings/index.d.ts
vendored
1
packages/discord.js/typings/index.d.ts
vendored
@@ -2659,6 +2659,7 @@ export function setPosition<T extends Channel | Role>(
|
||||
route: string,
|
||||
reason?: string,
|
||||
): Promise<{ id: Snowflake; position: number }[]>;
|
||||
export function parseWebhookURL(url: string): WebhookClientDataIdWithToken | null;
|
||||
|
||||
export interface MappedComponentBuilderTypes {
|
||||
[ComponentType.Button]: ButtonBuilder;
|
||||
|
||||
Reference in New Issue
Block a user