Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application role connections #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
*/
```

### `getUserApplicationRoleConnection(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.getUserApplicationRoleConnection(access_token).then(console.log);
/*
{
platform_name: 'Example Linked Role Discord Bot',
platform_username: null,
metadata: {
cookieseaten: '1483',
allergictonuts: '0',
firstcookiebaked: '2003-12-20'
}
}
*/
```

### `updateUserApplicationRoleConnection(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.updateUserApplicationRoleConnection({
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 @@ -85,6 +85,14 @@ declare namespace OAuth {
verify_key: string;
}

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 @@ -280,6 +288,16 @@ declare class OAuth extends EventEmitter {
withCounts?: boolean;
}): Promise<OAuth.PartialGuild[]>;
getUserConnections(access_token: string): Promise<OAuth.Connection[]>;
getUserApplicationRoleConnection(access_token: string, client_id?: string): Promise<OAuth.RoleConnection>;
updateUserApplicationRoleConnection(opts: {
platformName?: string;
platformUsername?: string;
metadata?: {
[key: string]: string;
};
accessToken: string;
clientId?: string;
}): Promise<OAuth.RoleConnection>;
addMember(opts: {
deaf?: boolean;
mute?: boolean;
Expand Down
53 changes: 53 additions & 0 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,59 @@ 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>}
*/
getUserApplicationRoleConnection(access_token, client_id) {
const clientId = client_id || this.clientId;
return this.request(
"GET",
`/users/@me/applications/${clientId}/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>}
*/
updateUserApplicationRoleConnection(options) {
const clientId = options.clientId || this.clientId;
return this.request(
"PUT",
`/users/@me/applications/${clientId}/role-connection`,
{
platform_name: options.platformName,
platform_username: options.platformUsername,
metadata: options.metadata,
},
{
auth: {
type: "Bearer",
creds: options.accessToken,
},
contentType: "application/json",
},
);
}

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