Skip to content

Commit

Permalink
Add support for more OAuth features (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek authored and amishshah committed Feb 22, 2017
1 parent d09dfa4 commit 61e12c6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 37 deletions.
7 changes: 3 additions & 4 deletions src/client/Client.js
Expand Up @@ -361,12 +361,11 @@ class Client extends EventEmitter {

/**
* Obtains the OAuth Application of the bot from Discord.
* <warn>This is only available when using a bot account.</warn>
* @param {Snowflake} [id='@me'] ID of application to fetch
* @returns {Promise<ClientOAuth2Application>}
*/
fetchApplication() {
if (!this.user.bot) throw new Error(Constants.Errors.NO_BOT_ACCOUNT);
return this.rest.methods.getMyApplication();
fetchApplication(id = '@me') {
return this.rest.methods.getApplication(id);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/client/rest/RESTMethods.js
Expand Up @@ -12,7 +12,7 @@ const Role = require('../../structures/Role');
const Invite = require('../../structures/Invite');
const Webhook = require('../../structures/Webhook');
const UserProfile = require('../../structures/UserProfile');
const ClientOAuth2Application = require('../../structures/ClientOAuth2Application');
const OAuth2Application = require('../../structures/OAuth2Application');
const Channel = require('../../structures/Channel');
const Guild = require('../../structures/Guild');
const VoiceRegion = require('../../structures/VoiceRegion');
Expand Down Expand Up @@ -799,12 +799,20 @@ class RESTMethods {
);
}

getMyApplication() {
return this.rest.makeRequest('get', Constants.Endpoints.myApplication, true).then(app =>
new ClientOAuth2Application(this.client, app)
getApplication(id) {
return this.rest.makeRequest('get', Constants.Endpoints.oauth2Application(id), true).then(app =>
new OAuth2Application(this.client, app)
);
}

resetApplication(id) {
return this.rest.makeRequest(
'post',
`${Constants.Endpoints.oauth2Application(id)}/reset`,
true
).then(app => new OAuth2Application(this.client, app));
}

setNote(user, note) {
return this.rest.makeRequest('put', Constants.Endpoints.note(user.id), true, { note }).then(() => user);
}
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Expand Up @@ -24,7 +24,6 @@ module.exports = {

// Structures
Channel: require('./structures/Channel'),
ClientOAuth2Application: require('./structures/ClientOAuth2Application'),
ClientUser: require('./structures/ClientUser'),
DMChannel: require('./structures/DMChannel'),
Emoji: require('./structures/Emoji'),
Expand Down
26 changes: 0 additions & 26 deletions src/structures/ClientOAuth2Application.js

This file was deleted.

52 changes: 51 additions & 1 deletion src/structures/OAuth2Application.js
Expand Up @@ -47,9 +47,51 @@ class OAuth2Application {

/**
* The app's RPC origins
* @type {Array<string>}
* @type {?string[]}
*/
this.rpcOrigins = data.rpc_origins;

/**
* The app's redirect URIs
* @type {string[]}
*/
this.redirectURIs = data.redirect_uris;

/**
* If this app's bot requires a code grant when using the oauth2 flow
* @type {boolean}
*/
this.botRequireCodeGrant = data.bot_require_code_grant;

/**
* If this app's bot is public
* @type {boolean}
*/
this.botPublic = data.bot_public;

/**
* If this app can use rpc
* @type {boolean}
*/
this.rpcApplicationState = data.rpc_application_state;

/**
* Object containing basic info about this app's bot
* @type {Object}
*/
this.bot = data.bot;

/**
* Flags for the app
* @type {number}
*/
this.flags = data.flags;

/**
* oauth2 secret for the app
* @type {boolean}
*/
this.secret = data.secret;
}

/**
Expand All @@ -70,6 +112,14 @@ class OAuth2Application {
return new Date(this.createdTimestamp);
}

/**
* Reset the app's secret and bot token
* @returns {OAuth2Application}
*/
reset() {
return this.client.rest.methods.resetApplication(this.id);
}

/**
* When concatenated with a string, this automatically concatenates the app name rather than the app object.
* @returns {string}
Expand Down
2 changes: 1 addition & 1 deletion src/util/Constants.js
Expand Up @@ -158,7 +158,7 @@ const Endpoints = exports.Endpoints = {
webhook: (webhookID, token) => `${API}/webhooks/${webhookID}${token ? `/${token}` : ''}`,

// oauth
myApplication: `${API}/oauth2/applications/@me`,
oauth2Application: (appID) => `${API}/oauth2/applications/${appID}`,
getApp: (id) => `${API}/oauth2/authorize?client_id=${id}`,

// emoji
Expand Down

0 comments on commit 61e12c6

Please sign in to comment.