Add support for more OAuth features (#1203)

This commit is contained in:
Gus Caplan
2017-02-22 14:09:40 -06:00
committed by Amish Shah
parent d09dfa4c37
commit 61e12c637b
6 changed files with 67 additions and 37 deletions

View File

@@ -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);
}
/**

View File

@@ -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');
@@ -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);
}

View File

@@ -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'),

View File

@@ -1,26 +0,0 @@
const User = require('./User');
const OAuth2Application = require('./OAuth2Application');
/**
* Represents the client's OAuth2 Application
* @extends {OAuth2Application}
*/
class ClientOAuth2Application extends OAuth2Application {
setup(data) {
super.setup(data);
/**
* The app's flags
* @type {number}
*/
this.flags = data.flags;
/**
* The app's owner
* @type {User}
*/
this.owner = new User(this.client, data.owner);
}
}
module.exports = ClientOAuth2Application;

View File

@@ -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;
}
/**
@@ -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}

View File

@@ -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