/
GroupChannel.js
108 lines (98 loc) · 3.76 KB
/
GroupChannel.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
"use strict";
const Collection = require("../util/Collection");
const {ImageFormats, ImageSizeBoundaries} = require("../Constants");
const Endpoints = require("../rest/Endpoints");
const PrivateChannel = require("./PrivateChannel");
const User = require("./User");
/**
* Represents a group channel. See PrivateChannel docs for additional properties.
* @extends PrivateChannel
* @prop {String} id The ID of the channel
* @prop {String} mention A string that mentions the channel
* @prop {Call?} call The current group call, if any
* @prop {Call?} lastCall The previous group call, if any
* @prop {Collection<User>} recipients The recipients in this private channel
* @prop {String} name The name of the group channel
* @prop {String?} icon The hash of the group channel icon
* @prop {String?} iconURL The URL of the group channel icon
* @prop {String} ownerID The ID of the user that is the group owner
*/
class GroupChannel extends PrivateChannel { // (╯°□°)╯︵ ┻━┻
constructor(data, client) {
super(data, client);
this.recipients = new Collection(User);
data.recipients.forEach((recipient) => {
this.recipients.add(client.options.restMode ? new User(recipient, client) : client.users.add(recipient, client));
});
this.update(data);
}
update(data) {
if(data.name !== undefined) {
this.name = data.name;
}
if(data.owner_id !== undefined) {
this.ownerID = data.owner_id;
}
if(data.icon !== undefined) {
this.icon = data.icon;
}
}
/**
* Edit the channel's properties
* @arg {Object} options The properties to edit
* @arg {String} [options.name] The name of the channel
* @arg {String} [options.icon] The icon of the channel as a base64 data URI (group channels only). Note: base64 strings alone are not base64 data URI strings
* @arg {String} [options.ownerID] The ID of the channel owner (group channels only)
* @returns {Promise<GroupChannel>}
*/
edit(options) {
return this._client.editChannel.call(this._client, this.id, options);
}
/**
* Add a user to the group
* @arg {String} userID The ID of the target user
* @returns {Promise}
*/
addRecipient(userID) {
return this._client.addGroupRecipient.call(this._client, this.id, userID);
}
/**
* Remove a user from the group
* @arg {String} userID The ID of the target user
* @returns {Promise}
*/
removeRecipient(userID) {
return this._client.removeGroupRecipient.call(this._client, this.id, userID);
}
get iconURL() {
return this.icon ? `${Endpoints.CDN_URL}/channel-icons/${this.id}/${this.icon}.${this._client.options.defaultImageFormat}?size=${this._client.options.defaultImageSize}` : null;
}
/**
* Get the group's icon with the given format and size
* @arg {String} [format] The filetype of the icon ("jpg", "png", "gif", or "webp")
* @arg {Number} [size] The size of the icon (any power of two between 16 and 2048)
*/
dynamicIconURL(format, size) {
if(!format || !ImageFormats.includes(format.toLowerCase())) {
format = this._client.options.defaultImageFormat;
}
if(
size < ImageSizeBoundaries.MINIMUM ||
size > ImageSizeBoundaries.MAXIMUM ||
(size & (size - 1))
) {
size = this._client.options.defaultImageSize;
}
return this.icon ? `${Endpoints.CDN_URL}/channel-icons/${this.id}/${this.icon}.${format}?size=${size}` : null;
}
toJSON(props = []) {
return super.toJSON([
"icon",
"name",
"ownerID",
"recipients",
...props
]);
}
}
module.exports = GroupChannel;