110 lines
2.6 KiB
JavaScript
110 lines
2.6 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const Team = require('./Team');
|
||
|
const { Error } = require('../errors/DJSError');
|
||
|
const Application = require('./interfaces/Application');
|
||
|
const ApplicationFlagsBitField = require('../util/ApplicationFlagsBitField');
|
||
|
const ApplicationCommandManager = require('../managers/ApplicationCommandManager');
|
||
|
|
||
|
/**
|
||
|
* Represents a Client OAuth2 Application.
|
||
|
* @extends {Application}
|
||
|
*/
|
||
|
class ClientApplication extends Application {
|
||
|
constructor(client, data) {
|
||
|
super(client, data);
|
||
|
|
||
|
/**
|
||
|
* The application command manager for this application
|
||
|
* @type {ApplicationCommandManager}
|
||
|
*/
|
||
|
this.commands = new ApplicationCommandManager(this.client);
|
||
|
}
|
||
|
|
||
|
_patch(data) {
|
||
|
super._patch(data);
|
||
|
|
||
|
if(!data) return;
|
||
|
|
||
|
if ('flags' in data) {
|
||
|
/**
|
||
|
* The flags this application has
|
||
|
* @type {ApplicationFlagsBitField}
|
||
|
*/
|
||
|
this.flags = new ApplicationFlagsBitField(data.flags).freeze();
|
||
|
}
|
||
|
|
||
|
if ('cover_image' in data) {
|
||
|
/**
|
||
|
* The hash of the application's cover image
|
||
|
* @type {?string}
|
||
|
*/
|
||
|
this.cover = data.cover_image;
|
||
|
} else {
|
||
|
this.cover ??= null;
|
||
|
}
|
||
|
|
||
|
if ('rpc_origins' in data) {
|
||
|
/**
|
||
|
* The application's RPC origins, if enabled
|
||
|
* @type {string[]}
|
||
|
*/
|
||
|
this.rpcOrigins = data.rpc_origins;
|
||
|
} else {
|
||
|
this.rpcOrigins ??= [];
|
||
|
}
|
||
|
|
||
|
if ('bot_require_code_grant' in data) {
|
||
|
/**
|
||
|
* If this application's bot requires a code grant when using the OAuth2 flow
|
||
|
* @type {?boolean}
|
||
|
*/
|
||
|
this.botRequireCodeGrant = data.bot_require_code_grant;
|
||
|
} else {
|
||
|
this.botRequireCodeGrant ??= null;
|
||
|
}
|
||
|
|
||
|
if ('bot_public' in data) {
|
||
|
/**
|
||
|
* If this application's bot is public
|
||
|
* @type {?boolean}
|
||
|
*/
|
||
|
this.botPublic = data.bot_public;
|
||
|
} else {
|
||
|
this.botPublic ??= null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* The owner of this OAuth application
|
||
|
* @type {?(User|Team)}
|
||
|
*/
|
||
|
this.owner = data.team
|
||
|
? new Team(this.client, data.team)
|
||
|
: data.owner
|
||
|
? this.client.users._add(data.owner)
|
||
|
: this.owner ?? null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Whether this application is partial
|
||
|
* @type {boolean}
|
||
|
* @readonly
|
||
|
*/
|
||
|
get partial() {
|
||
|
return !this.name;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Obtains this application from Discord.
|
||
|
* @returns {Promise<ClientApplication>}
|
||
|
*/
|
||
|
async fetch() {
|
||
|
if(!this.client.bot) throw new Error("INVALID_USER_METHOD");
|
||
|
const app = await this.client.api.oauth2.applications('@me').get();
|
||
|
this._patch(app);
|
||
|
return this;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = ClientApplication;
|