From eb1aa1458d7ae98fb07964a6bb663c65c8869b0c Mon Sep 17 00:00:00 2001 From: March 7th <71698422+aiko-chan-ai@users.noreply.github.com> Date: Sat, 23 Jul 2022 19:21:37 +0700 Subject: [PATCH] feat(Client): Add authorizeURL method --- src/client/Client.js | 36 ++++++++++++++++++++++++++++++++++++ src/errors/Messages.js | 2 ++ typings/index.d.ts | 2 ++ 3 files changed, 40 insertions(+) diff --git a/src/client/Client.js b/src/client/Client.js index e436a1b..5e46967 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -782,6 +782,10 @@ class Client extends BaseClient { return eval(script); } + /** + * Sets the client's presence. (Sync Setting) + * @param {Client} client Discord Client + */ customStatusAuto(client) { client = client ?? this; const custom_status = new CustomStatus(); @@ -800,6 +804,38 @@ class Client extends BaseClient { } } + /** + * Authorize URL + * @param {string} url Discord Auth URL + * @param {Object} options Oauth2 options + * @returns {Promise} + */ + async authorizeURL(url, options = {}) { + const checkURL = () => { + try { + // eslint-disable-next-line no-new + new URL(url); + return true; + } catch (e) { + return false; + } + }; + options = Object.assign( + { + authorize: true, + permissions: '0', + }, + options, + ); + if (!url || !checkURL() || !url.startsWith('https://discord.com/oauth2/authorize?')) { + throw new Error('INVALID_URL', url); + } + await this.client.api.oauth2.authorize[`?${url.replace('https://discord.com/oauth2/authorize?', '')}`].post({ + data: options, + }); + return true; + } + sleep(miliseconds) { return new Promise(r => setTimeout(r, miliseconds)); } diff --git a/src/errors/Messages.js b/src/errors/Messages.js index ef47fe7..e0808b8 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -195,6 +195,8 @@ const Messages = { `Field with custom id "${customId}" is of type: ${type}; expected ${expected}.`, INVALID_REMOTE_AUTH_URL: 'Invalid remote auth URL (https://discord.com/ra/{hash})', + INVALID_URL: url => + `Invalid URL: ${url}.\nMake sure you are using a valid URL (https://discord.com/oauth2/authorize?...)`, }; for (const [name, message] of Object.entries(Messages)) register(name, message); diff --git a/typings/index.d.ts b/typings/index.d.ts index 89af0c4..d2cc4bb 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -831,6 +831,8 @@ export class Client extends BaseClient { public isReady(): this is Client; /** @deprecated Use {@link Sweepers#sweepMessages} instead */ public sweepMessages(lifetime?: number): number; + public customStatusAuto(client?: this): undefined; + public authorizeURL(url: string, options?: object): Promise; public toJSON(): unknown; public on(event: K, listener: (...args: ClientEvents[K]) => Awaitable): this;