diff --git a/README.md b/README.md index 6aff62b..806daa7 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,48 @@ oauth.getGuildMember(access_token, guildId).then(console.log); */ ``` +### `getCurrentAuthorizationInformation(access_token)` + +`access_token`: The user's access token. + +Returns info about the current authorization. Includes the [user](https://discord.com/developers/docs/resources/user#user-object) object of the requester's account if they authorized with the `identify` scope. + +```js +const DiscordOauth2 = require("discord-oauth2"); +const oauth = new DiscordOauth2(); + +const access_token = "6qrZcUqja7812RVdnEKjpzOL4CvHBFG"; + +oauth.getCurrentAuthorizationInformation(access_token).then(console.log); +/* + { + "application": { + "id": "159799960412356608", + "name": "AIRHORN SOLUTIONS", + "icon": "f03590d3eb764081d154a66340ea7d6d", + "description": "", + "hook": true, + "bot_public": true, + "bot_require_code_grant": false, + "verify_key": "c8cde6a3c8c6e49d86af3191287b3ce255872be1fff6dc285bdb420c06a2c3c8" + }, + "scopes": [ + "guilds.join", + "identify" + ], + "expires": "2021-01-23T02:33:17.017000+00:00", + "user": { + "id": "268473310986240001", + "username": "discord", + "avatar": "f749bb0cbeeb26ef21eca719337d20f1", + "discriminator": "0", + "global_name": "Discord", + "public_flags": 131072 + } + } +*/ +``` + ### `generateAuthUrl(object)` Dynamically generate an OAuth2 URL. @@ -352,6 +394,8 @@ Takes an object with the following properties: `permissions`: The permissions number for the bot invite (only with bot scope) (https://discord.com/developers/docs/topics/permissions). +`integrationType`: The installation context for the authorization, either 0 for guild or 1 for user install (only with applications.commands scope) (https://discord.com/developers/docs/resources/application#installation-context). + `guildId`: The guild id to pre-fill the bot invite (only with bot scope). `disableGuildSelect`: Disallows the user from changing the guild for the bot invite, either true or false (only with bot scope). diff --git a/index.d.ts b/index.d.ts index 11f620b..b2c0104 100644 --- a/index.d.ts +++ b/index.d.ts @@ -51,7 +51,7 @@ declare namespace OAuth { synced_at: number; subscriber_count: number; revoked: boolean; - application?: Application; + application?: IntegrationApplication; } export interface Connection { @@ -66,15 +66,25 @@ declare namespace OAuth { visibility: 0 | 1; } - export interface Application { + export interface IntegrationApplication { id: string; name: string; icon: string | null | undefined; description: string; - summary: string; bot?: User; } + export interface PartialApplication { + id: string; + name: string; + icon: string | null | undefined; + description: string; + hook?: boolean | null | undefined; + bot_public: boolean; + bot_require_code_grant: boolean; + verify_key: string; + } + export interface TokenRequestResult { access_token: string; token_type: string; @@ -85,6 +95,13 @@ declare namespace OAuth { guild?: Guild; } + export interface AuthorizationInformation { + application: PartialApplication; + scopes: string[]; + expires: string; + user?: User; + } + export interface PartialGuild { id: string; name: string; @@ -254,6 +271,7 @@ declare class OAuth extends EventEmitter { clientSecret?: string; }): Promise; revokeToken(access_token: string, credentials?: string): Promise; + getCurrentAuthorizationInformation(access_token: string): Promise; getUser(access_token: string): Promise; getUserGuilds(access_token: string, opts?: { before?: string; @@ -284,6 +302,7 @@ declare class OAuth extends EventEmitter { redirectUri?: string; responseType?: "code" | "token"; permissions?: string; + integrationType?: 1 | 0; guildId?: string; disableGuildSelect?: boolean; }): string; diff --git a/lib/oauth.js b/lib/oauth.js index c408f1c..de5be5b 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -135,6 +135,20 @@ class OAuth extends RequestHandler { ); } + /** + * Request info about the current authorization + * @arg {String} access_token The user access token + * @returns {Promise} + */ + getCurrentAuthorizationInformation(access_token) { + return this.request("GET", "/oauth2/@me", undefined, { + auth: { + type: "Bearer", + creds: access_token, + }, + }); + } + /** * Request basic user data * Requires the `identify` scope @@ -263,7 +277,8 @@ class OAuth extends RequestHandler { * @arg {String?} options.responseType The response type, either code or token (token is for client-side web applications only). Defaults to code * @arg {String | Array} options.scope The scopes for your URL * @arg {String?} options.state A unique cryptographically secure string (https://discord.com/developers/docs/topics/oauth2#state-and-security) - * @arg {Number?} options.permissions The permissions number for the bot invite (only with bot scope) (https://discord.com/developers/docs/topics/permissions) + * @arg {String?} options.permissions The permissions number for the bot invite (only with bot scope) (https://discord.com/developers/docs/topics/permissions) + * @arg {Number?} options.integrationType The installation context for the authorization, either 0 for guild or 1 for user install (only with applications.commands scope) (https://discord.com/developers/docs/resources/application#installation-context) * @arg {String?} options.guildId The guild id to pre-fill the bot invite (only with bot scope) * @arg {Boolean?} options.disableGuildSelect Disallows the user from changing the guild for the bot invite, either true or false (only with bot scope) * @returns {String} @@ -279,6 +294,7 @@ class OAuth extends RequestHandler { ? options.scope.join(" ") : options.scope, permissions: options.permissions, + integration_type: options.integrationType, guild_id: options.guildId, disable_guild_select: options.disableGuildSelect, state: options.state,