-
Notifications
You must be signed in to change notification settings - Fork 416
/
GuildChannel.js
146 lines (134 loc) · 6.12 KB
/
GuildChannel.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
"use strict";
const Channel = require("./Channel");
const Collection = require("../util/Collection");
const Permission = require("./Permission");
const {Permissions} = require("../Constants");
const PermissionOverwrite = require("./PermissionOverwrite");
/**
* Represents a guild channel. You also probably want to look at CategoryChannel, NewsChannel, StoreChannel, TextChannel, and VoiceChannel.
* @extends Channel
* @prop {String} id The ID of the channel
* @prop {String} mention A string that mentions the channel
* @prop {Number} type The type of the channel
* @prop {Guild} guild The guild that owns the channel
* @prop {String?} parentID The ID of the category this channel belongs to
* @prop {String} name The name of the channel
* @prop {Number} position The position of the channel
* @prop {Boolean} nsfw Whether the channel is an NSFW channel or not
* @prop {Collection<PermissionOverwrite>} permissionOverwrites Collection of PermissionOverwrites in this channel
*/
class GuildChannel extends Channel {
constructor(data, client) {
super(data);
this.guild = client.guilds.get(data.guild_id) || {
id: data.guild_id
};
this.update(data);
}
update(data) {
this.type = data.type !== undefined ? data.type : this.type;
this.name = data.name !== undefined ? data.name : this.name;
this.position = data.position !== undefined ? data.position : this.position;
this.parentID = data.parent_id !== undefined ? data.parent_id : this.parentID;
this.nsfw = (this.name.length === 4 ? this.name === "nsfw" : this.name.startsWith("nsfw-")) || data.nsfw;
if(data.permission_overwrites) {
this.permissionOverwrites = new Collection(PermissionOverwrite);
data.permission_overwrites.forEach((overwrite) => {
this.permissionOverwrites.add(overwrite);
});
}
}
/**
* Get the channel-specific permissions of a member
* @arg {String} memberID The ID of the member
* @returns {Permission}
*/
permissionsOf(memberID) {
const member = this.guild.members.get(memberID);
let permission = member.permission.allow;
if(permission & Permissions.administrator) {
return new Permission(Permissions.all);
}
let overwrite = this.permissionOverwrites.get(this.guild.id);
if(overwrite) {
permission = (permission & ~overwrite.deny) | overwrite.allow;
}
let deny = 0;
let allow = 0;
for(const roleID of member.roles) {
if((overwrite = this.permissionOverwrites.get(roleID))) {
deny |= overwrite.deny;
allow |= overwrite.allow;
}
}
permission = (permission & ~deny) | allow;
overwrite = this.permissionOverwrites.get(memberID);
if(overwrite) {
permission = (permission & ~overwrite.deny) | overwrite.allow;
}
return new Permission(permission);
}
/**
* Edit the channel's properties
* @arg {Object} options The properties to edit
* @arg {String} [options.name] The name of the channel
* @arg {String} [options.topic] The topic of the channel (guild text channels only)
* @arg {Number} [options.bitrate] The bitrate of the channel (guild voice channels only)
* @arg {Number} [options.userLimit] The channel user limit (guild voice channels only)
* @arg {Number} [options.rateLimitPerUser] The time in seconds a user has to wait before sending another message (does not affect bots or users with manageMessages/manageChannel permissions) (guild text channels only)
* @arg {Boolean} [options.nsfw] The nsfw status of the channel
* @arg {Number?} [options.parentID] The ID of the parent channel category for this channel (guild text/voice channels only)
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise<CategoryChannel | TextChannel | VoiceChannel | NewsChannel>}
*/
edit(options, reason) {
return this.guild.shard.client.editChannel.call(this.guild.shard.client, this.id, options, reason);
}
/**
* Edit the channel's position. Note that channel position numbers are lowest on top and highest at the bottom.
* @arg {Number} position The new position of the channel
* @returns {Promise}
*/
editPosition(position) {
return this.guild.shard.client.editChannelPosition.call(this.guild.shard.client, this.id, position);
}
/**
* Delete the channel
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
delete(reason) {
return this.guild.shard.client.deleteChannel.call(this.guild.shard.client, this.id, reason);
}
/**
* Create a channel permission overwrite
* @arg {String} overwriteID The ID of the overwritten user or role
* @arg {Number} allow The permissions number for allowed permissions
* @arg {Number} deny The permissions number for denied permissions
* @arg {String} type The object type of the overwrite, either "member" or "role"
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise<PermissionOverwrite>}
*/
editPermission(overwriteID, allow, deny, type, reason) {
return this.guild.shard.client.editChannelPermission.call(this.guild.shard.client, this.id, overwriteID, allow, deny, type, reason);
}
/**
* Delete a channel permission overwrite
* @arg {String} overwriteID The ID of the overwritten user or role
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
deletePermission(overwriteID, reason) {
return this.guild.shard.client.deleteChannelPermission.call(this.guild.shard.client, this.id, overwriteID, reason);
}
toJSON() {
const base = super.toJSON(true);
for(const prop of ["name", "nsfw", "parentID", "permissionOverwrites", "position"]) {
if(this[prop] !== undefined) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
}
return base;
}
}
module.exports = GuildChannel;