misc: update ClientApplication for the current API (#2767)

* misc: Update ClientApplication for the current API

* cleanup: ClientApplication#fetchAssets, removed createAsset

* Major cleanup time

* Merge to kyra's branch

* docs: Updated typings

* fix: re-add ClientApplication#{cover,coverImage()}

* typings(ClientApplication): move coverImage declaration up
This commit is contained in:
Kyra
2018-08-22 08:10:55 +02:00
committed by SpaceEEC
parent 02f98cd7e6
commit 28d4f74b65
2 changed files with 21 additions and 65 deletions

View File

@@ -1,8 +1,9 @@
const Snowflake = require('../util/Snowflake');
const { ClientApplicationAssetTypes, Endpoints } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const Base = require('./Base');
const AssetTypes = Object.keys(ClientApplicationAssetTypes);
/**
* Represents a Client OAuth2 Application.
* @extends {Base}
@@ -39,22 +40,16 @@ class ClientApplication extends Base {
this.icon = data.icon;
/**
* The app's cover image hash
* The app's cover image
* @type {?string}
*/
this.cover = data.cover_image;
this.cover = data.cover_image || null;
/**
* The app's RPC origins
* The app's RPC origins, if enabled
* @type {?string[]}
*/
this.rpcOrigins = data.rpc_origins;
/**
* The app's redirect URIs
* @type {string[]}
*/
this.redirectURIs = data.redirect_uris;
this.rpcOrigins = data.rpc_origins || [];
/**
* If this app's bot requires a code grant when using the OAuth2 flow
@@ -69,36 +64,10 @@ class ClientApplication extends Base {
this.botPublic = data.bot_public;
/**
* If this app can use rpc
* @type {boolean}
* The owner of this OAuth application
* @type {User}
*/
this.rpcApplicationState = data.rpc_application_state;
/**
* Object containing basic info about this app's bot
* @type {Object}
*/
this.bot = data.bot;
/**
* The flags for the app
* @type {number}
*/
this.flags = data.flags;
/**
* OAuth2 secret for the application
* @type {string}
*/
this.secret = data.secret;
if (data.owner) {
/**
* The owner of this OAuth application
* @type {?User}
*/
this.owner = this.client.users.add(data.owner);
}
this.owner = this.client.users.add(data.owner);
}
/**
@@ -142,34 +111,26 @@ class ClientApplication extends Base {
}
/**
* Get rich presence assets.
* @returns {Promise<Object>}
* Asset data.
* @typedef {Object} ClientAsset
* @property {Snowflake} id The asset ID
* @property {string} name The asset name
* @property {string} type The asset type
*/
/**
* Gets the clients rich presence assets.
* @returns {Promise<Array<ClientAsset>>}
*/
fetchAssets() {
const types = Object.keys(ClientApplicationAssetTypes);
return this.client.api.oauth2.applications(this.id).assets.get()
.then(assets => assets.map(a => ({
id: a.id,
name: a.name,
type: types[a.type - 1],
type: AssetTypes[a.type - 1],
})));
}
/**
* Creates a rich presence asset.
* @param {string} name Name of the asset
* @param {Base64Resolvable} data Data of the asset
* @param {string} type Type of the asset. `big`, or `small`
* @returns {Promise}
*/
async createAsset(name, data, type) {
return this.client.api.oauth2.applications(this.id).assets.post({ data: {
name,
type: ClientApplicationAssetTypes[type.toUpperCase()],
image: await DataResolver.resolveImage(data),
} });
}
/**
* When concatenated with a string, this automatically returns the application's name instead of the
* ClientApplication object.

7
typings/index.d.ts vendored
View File

@@ -228,10 +228,9 @@ declare module 'discord.js' {
export class ClientApplication extends Base {
constructor(client: Client, data: object);
public bot: object;
public botPublic: boolean;
public botRequireCodeGrant: boolean;
public cover: string;
public cover?: string;
public readonly createdAt: Date;
public readonly createdTimestamp: number;
public description: string;
@@ -239,12 +238,8 @@ declare module 'discord.js' {
public id: Snowflake;
public name: string;
public owner: User;
public redirectURIs: string[];
public rpcApplicationState: boolean;
public rpcOrigins: string[];
public secret: string;
public coverImage(options?: AvatarOptions): string;
public createAsset(name: string, data: Base64Resolvable, type: 'big' | 'small' | 'Big' | 'Small'): Promise<object>;
public fetchAssets(): Promise<ClientApplicationAsset>;
public iconURL(options?: AvatarOptions): string;
public toJSON(): object;