Skip to content

Commit

Permalink
Add application role connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Rost authored Jul 19, 2024
1 parent 62fc30e commit 33e290e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,89 @@ oauth.getUserConnections(access_token).then(console.log);
*/
```

### `getUserRoleConnection(access_token, client_id)`

`access_token`: The user's access token.

`client_id`: Your application's client id. Can be omitted if provided on the client constructor.

Requires the `role_connections.write` OAuth2 scope.

Returns the [application role connection](https://discord.com/developers/docs/resources/user#application-role-connection-object) object for the user.

```js
const DiscordOauth2 = require("discord-oauth2");
const oauth = new DiscordOauth2({
clientId: "332269999912132097",
});

const access_token = "6qrZcUqja7812RVdnEKjpzOL4CvHBFG";

oauth.getUserRoleConnection(access_token).then(console.log);
/*
{
platform_name: 'Example Linked Role Discord Bot',
platform_username: null,
metadata: {
cookieseaten: '1483',
allergictonuts: '0',
firstcookiebaked: '2003-12-20'
}
}
*/
```

### `updateUserRoleConnection(object)`

Update the application role connection for the user.

Takes an object with the following properties (required):

`accessToken`: The user access token.

Optional:

`platformName`: The vanity name of the platform the bot has connected.

`platformUsername`: The username on the platform the bot has connected.

`metadata`: Object mapping [application role connection metadata](https://discord.com/developers/docs/resources/application-role-connection-metadata#application-role-connection-metadata-object) keys to their string-ified value for the user on the platform the bot has connected.

`clientId`: Your application's client id. Can be omitted if provided on the client constructor.

Requires the `role_connections.write` OAuth2 scope.

Returns the [application role connection](https://discord.com/developers/docs/resources/user#application-role-connection-object) object for the user.

```js
const DiscordOauth2 = require("discord-oauth2");
const oauth = new DiscordOauth2({
clientId: "332269999912132097",
});

oauth.updateUserRoleConnection({
accessToken: "6qrZcUqja7812RVdnEKjpzOL4CvHBFG",
platformName: "Example Linked Role Discord Bot",
platformUsername: "Example Username",
metadata: {
cookieseaten: "1483",
allergictonuts: "0",
firstcookiebaked: "2003-12-20",
},
}).then(console.log);
/*
{
platform_name: 'Example Linked Role Discord Bot',
platform_username: 'Example Username',
metadata: {
cookieseaten: '1483',
allergictonuts: '0',
firstcookiebaked: '2003-12-20'
}
}
*/
```

### `addMember(object)`

Force join a user to a guild.
Expand Down
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ declare namespace OAuth {
bot?: User;
}

export interface RoleConnection {
platform_name: string | null;
platform_username: string | null;
metadata: {
[key: string]: string;
};
}

export interface TokenRequestResult {
access_token: string;
token_type: string;
Expand Down Expand Up @@ -175,6 +183,16 @@ declare class OAuth extends EventEmitter {
withCounts?: boolean;
}): Promise<OAuth.PartialGuild[]>;
getUserConnections(access_token: string): Promise<OAuth.Connection[]>;
getUserRoleConnection(access_token: string, client_id?: string): Promise<OAuth.RoleConnection>;
updateUserRoleConnection(opts: {
platformName?: string;
platformUsername?: string;
metadata?: {
[key: string]: string;
};
accessToken: string;
clientId?: string;
}): Promise<OAuth.RoleConnection>;
addMember(opts: {
deaf?: boolean;
mute?: boolean;
Expand Down
52 changes: 52 additions & 0 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,58 @@ class OAuth extends RequestHandler {
});
}

/**
* Request the application role connection for the user
* Requires the `role_connections.write` scope
* @arg {String} access_token The user access token
* @arg {String} [client_id] Your application's client id
* @returns {Promise<Object>}
*/
getUserRoleConnection(access_token, client_id) {
client_id = client_id || this.clientId;
return this.request(
"GET",
`/users/@me/applications/${client_id}/role-connection`,
undefined,
{
auth: {
type: "Bearer",
creds: access_token,
},
},
);
}

/**
* Update the application role connection for the user
* Requires the `role_connections.write` scope
* @arg {Object} options
* @arg {String?} options.platformName The name of the platform
* @arg {String?} options.platformUsername The username on the platform
* @arg {Object?} options.metadata The metadata keys mapped to their stringified values
* @arg {String} options.accessToken The user access token
* @arg {String?} options.clientId Your application's client id
* @returns {Promise<Object>}
*/
updateUserRoleConnection(options) {
const client_id = options.clientId || this.clientId;
return this.request(
"PUT",
`/users/@me/applications/${client_id}/role-connection`,
{
platform_name: options.platformName,
platform_username: options.platformUsername,
metadata: options.metadata,
},
{
auth: {
type: "Bearer",
creds: access_token,
},
},
);
}

/**
* Force a user to join a guild
* Requires the `guilds.join` scope
Expand Down

0 comments on commit 33e290e

Please sign in to comment.