feat(Client): Add authorizeURL method

This commit is contained in:
March 7th 2022-07-23 19:21:37 +07:00
parent 46800d957b
commit eb1aa1458d
3 changed files with 40 additions and 0 deletions

View File

@ -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<boolean>}
*/
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));
}

View File

@ -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);

2
typings/index.d.ts vendored
View File

@ -831,6 +831,8 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public isReady(): this is Client<true>;
/** @deprecated Use {@link Sweepers#sweepMessages} instead */
public sweepMessages(lifetime?: number): number;
public customStatusAuto(client?: this): undefined;
public authorizeURL(url: string, options?: object): Promise<boolean>;
public toJSON(): unknown;
public on<K extends keyof ClientEvents>(event: K, listener: (...args: ClientEvents[K]) => Awaitable<void>): this;