mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
Add support for more OAuth features (#1203)
This commit is contained in:
@@ -361,12 +361,11 @@ class Client extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtains the OAuth Application of the bot from Discord.
|
* 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>}
|
* @returns {Promise<ClientOAuth2Application>}
|
||||||
*/
|
*/
|
||||||
fetchApplication() {
|
fetchApplication(id = '@me') {
|
||||||
if (!this.user.bot) throw new Error(Constants.Errors.NO_BOT_ACCOUNT);
|
return this.rest.methods.getApplication(id);
|
||||||
return this.rest.methods.getMyApplication();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ const Role = require('../../structures/Role');
|
|||||||
const Invite = require('../../structures/Invite');
|
const Invite = require('../../structures/Invite');
|
||||||
const Webhook = require('../../structures/Webhook');
|
const Webhook = require('../../structures/Webhook');
|
||||||
const UserProfile = require('../../structures/UserProfile');
|
const UserProfile = require('../../structures/UserProfile');
|
||||||
const ClientOAuth2Application = require('../../structures/ClientOAuth2Application');
|
const OAuth2Application = require('../../structures/OAuth2Application');
|
||||||
const Channel = require('../../structures/Channel');
|
const Channel = require('../../structures/Channel');
|
||||||
const Guild = require('../../structures/Guild');
|
const Guild = require('../../structures/Guild');
|
||||||
const VoiceRegion = require('../../structures/VoiceRegion');
|
const VoiceRegion = require('../../structures/VoiceRegion');
|
||||||
@@ -799,12 +799,20 @@ class RESTMethods {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getMyApplication() {
|
getApplication(id) {
|
||||||
return this.rest.makeRequest('get', Constants.Endpoints.myApplication, true).then(app =>
|
return this.rest.makeRequest('get', Constants.Endpoints.oauth2Application(id), true).then(app =>
|
||||||
new ClientOAuth2Application(this.client, 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) {
|
setNote(user, note) {
|
||||||
return this.rest.makeRequest('put', Constants.Endpoints.note(user.id), true, { note }).then(() => user);
|
return this.rest.makeRequest('put', Constants.Endpoints.note(user.id), true, { note }).then(() => user);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ module.exports = {
|
|||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
Channel: require('./structures/Channel'),
|
Channel: require('./structures/Channel'),
|
||||||
ClientOAuth2Application: require('./structures/ClientOAuth2Application'),
|
|
||||||
ClientUser: require('./structures/ClientUser'),
|
ClientUser: require('./structures/ClientUser'),
|
||||||
DMChannel: require('./structures/DMChannel'),
|
DMChannel: require('./structures/DMChannel'),
|
||||||
Emoji: require('./structures/Emoji'),
|
Emoji: require('./structures/Emoji'),
|
||||||
|
|||||||
@@ -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;
|
|
||||||
@@ -47,9 +47,51 @@ class OAuth2Application {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The app's RPC origins
|
* The app's RPC origins
|
||||||
* @type {Array<string>}
|
* @type {?string[]}
|
||||||
*/
|
*/
|
||||||
this.rpcOrigins = data.rpc_origins;
|
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);
|
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.
|
* When concatenated with a string, this automatically concatenates the app name rather than the app object.
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ const Endpoints = exports.Endpoints = {
|
|||||||
webhook: (webhookID, token) => `${API}/webhooks/${webhookID}${token ? `/${token}` : ''}`,
|
webhook: (webhookID, token) => `${API}/webhooks/${webhookID}${token ? `/${token}` : ''}`,
|
||||||
|
|
||||||
// oauth
|
// oauth
|
||||||
myApplication: `${API}/oauth2/applications/@me`,
|
oauth2Application: (appID) => `${API}/oauth2/applications/${appID}`,
|
||||||
getApp: (id) => `${API}/oauth2/authorize?client_id=${id}`,
|
getApp: (id) => `${API}/oauth2/authorize?client_id=${id}`,
|
||||||
|
|
||||||
// emoji
|
// emoji
|
||||||
|
|||||||
Reference in New Issue
Block a user