mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +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 BaseClient = require('./BaseClient');
|
||||||
const { Error, ErrorCodes } = require('../errors');
|
const { Error, ErrorCodes } = require('../errors');
|
||||||
const Webhook = require('../structures/Webhook');
|
const Webhook = require('../structures/Webhook');
|
||||||
|
const { parseWebhookURL } = require('../util/Util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The webhook client.
|
* The webhook client.
|
||||||
@@ -28,14 +29,12 @@ class WebhookClient extends BaseClient {
|
|||||||
let { id, token } = data;
|
let { id, token } = data;
|
||||||
|
|
||||||
if ('url' in data) {
|
if ('url' in data) {
|
||||||
const url = data.url.match(
|
const parsed = parseWebhookURL(data.url);
|
||||||
// eslint-disable-next-line no-useless-escape
|
if (!parsed) {
|
||||||
/https?:\/\/(?:ptb\.|canary\.)?discord\.com\/api(?:\/v\d{1,2})?\/webhooks\/(\d{17,19})\/([\w-]{68})/i,
|
throw new Error(ErrorCodes.WebhookURLInvalid);
|
||||||
);
|
}
|
||||||
|
|
||||||
if (!url || url.length <= 1) throw new Error(ErrorCodes.WebhookURLInvalid);
|
({ id, token } = parsed);
|
||||||
|
|
||||||
[, id, token] = url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|||||||
@@ -528,6 +528,32 @@ function lazy(cb) {
|
|||||||
return () => (defaultValue ??= 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 = {
|
module.exports = {
|
||||||
flatten,
|
flatten,
|
||||||
escapeMarkdown,
|
escapeMarkdown,
|
||||||
@@ -554,6 +580,7 @@ module.exports = {
|
|||||||
cleanContent,
|
cleanContent,
|
||||||
cleanCodeBlockContent,
|
cleanCodeBlockContent,
|
||||||
lazy,
|
lazy,
|
||||||
|
parseWebhookURL,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fixes Circular
|
// 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,
|
route: string,
|
||||||
reason?: string,
|
reason?: string,
|
||||||
): Promise<{ id: Snowflake; position: number }[]>;
|
): Promise<{ id: Snowflake; position: number }[]>;
|
||||||
|
export function parseWebhookURL(url: string): WebhookClientDataIdWithToken | null;
|
||||||
|
|
||||||
export interface MappedComponentBuilderTypes {
|
export interface MappedComponentBuilderTypes {
|
||||||
[ComponentType.Button]: ButtonBuilder;
|
[ComponentType.Button]: ButtonBuilder;
|
||||||
|
|||||||
Reference in New Issue
Block a user