Skip to content

Commit

Permalink
Merge branch 'master' into patch-3
Browse files Browse the repository at this point in the history
  • Loading branch information
reboxer authored Dec 15, 2024
2 parents 5c7fe8b + 0374f74 commit 3fb7f43
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
Expand Down
25 changes: 22 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare namespace OAuth {
synced_at: number;
subscriber_count: number;
revoked: boolean;
application?: Application;
application?: IntegrationApplication;
}

export interface Connection {
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -254,6 +271,7 @@ declare class OAuth extends EventEmitter {
clientSecret?: string;
}): Promise<OAuth.TokenRequestResult>;
revokeToken(access_token: string, credentials?: string): Promise<string>;
getCurrentAuthorizationInformation(access_token: string): Promise<OAuth.AuthorizationInformation>;
getUser(access_token: string): Promise<OAuth.User>;
getUserGuilds(access_token: string, opts?: {
before?: string;
Expand Down Expand Up @@ -284,6 +302,7 @@ declare class OAuth extends EventEmitter {
redirectUri?: string;
responseType?: "code" | "token";
permissions?: string;
integrationType?: 1 | 0;
guildId?: string;
disableGuildSelect?: boolean;
}): string;
Expand Down
18 changes: 17 additions & 1 deletion lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ class OAuth extends RequestHandler {
);
}

/**
* Request info about the current authorization
* @arg {String} access_token The user access token
* @returns {Promise<Object>}
*/
getCurrentAuthorizationInformation(access_token) {
return this.request("GET", "/oauth2/@me", undefined, {
auth: {
type: "Bearer",
creds: access_token,
},
});
}

/**
* Request basic user data
* Requires the `identify` scope
Expand Down Expand Up @@ -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}
Expand All @@ -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,
Expand Down

0 comments on commit 3fb7f43

Please sign in to comment.