-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Application.js
157 lines (137 loc) · 3.77 KB
/
Application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const process = require('node:process');
const { ClientApplicationAssetTypes, Endpoints } = require('../../util/Constants');
const SnowflakeUtil = require('../../util/SnowflakeUtil');
const Base = require('../Base');
const AssetTypes = Object.keys(ClientApplicationAssetTypes);
let deprecationEmittedForFetchAssets = false;
/**
* Represents an OAuth2 Application.
* @extends {Base}
* @abstract
*/
class Application extends Base {
constructor(client, data) {
super(client);
this._patch(data);
}
_patch(data) {
/**
* The application's id
* @type {Snowflake}
*/
this.id = data.id;
if ('name' in data) {
/**
* The name of the application
* @type {?string}
*/
this.name = data.name;
} else {
this.name ??= null;
}
if ('description' in data) {
/**
* The application's description
* @type {?string}
*/
this.description = data.description;
} else {
this.description ??= null;
}
if ('icon' in data) {
/**
* The application's icon hash
* @type {?string}
*/
this.icon = data.icon;
} else {
this.icon ??= null;
}
if ('role_connections_verification_url' in data) {
/**
* This application's role connection verification entry point URL
* @type {?string}
*/
this.roleConnectionsVerificationURL = data.role_connections_verification_url;
} else {
this.roleConnectionsVerificationURL ??= null;
}
}
/**
* The timestamp the application was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.timestampFrom(this.id);
}
/**
* The time the application was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* A link to the application's icon.
* @param {StaticImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
iconURL({ format, size } = {}) {
if (!this.icon) return null;
return this.client.rest.cdn.AppIcon(this.id, this.icon, { format, size });
}
/**
* A link to this application's cover image.
* @param {StaticImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
coverURL({ format, size } = {}) {
if (!this.cover) return null;
return Endpoints.CDN(this.client.options.http.cdn).AppIcon(this.id, this.cover, { format, size });
}
/**
* Asset data.
* @typedef {Object} ApplicationAsset
* @property {Snowflake} id The asset's id
* @property {string} name The asset's name
* @property {string} type The asset's type
*/
/**
* Gets the application's rich presence assets.
* @returns {Promise<Array<ApplicationAsset>>}
* @deprecated This will be removed in the next major as it is unsupported functionality.
*/
async fetchAssets() {
if (!deprecationEmittedForFetchAssets) {
process.emitWarning(
'Application#fetchAssets is deprecated as it is unsupported and will be removed in the next major version.',
'DeprecationWarning',
);
deprecationEmittedForFetchAssets = true;
}
const assets = await this.client.api.oauth2.applications(this.id).assets.get();
return assets.map(a => ({
id: a.id,
name: a.name,
type: AssetTypes[a.type - 1],
}));
}
/**
* When concatenated with a string, this automatically returns the application's name instead of the
* Application object.
* @returns {?string}
* @example
* // Logs: Application name: My App
* console.log(`Application name: ${application}`);
*/
toString() {
return this.name;
}
toJSON() {
return super.toJSON({ createdTimestamp: true });
}
}
module.exports = Application;