forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuildChannelManager.js
162 lines (149 loc) · 5.43 KB
/
GuildChannelManager.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
158
159
160
161
162
'use strict';
const BaseManager = require('./BaseManager');
const GuildChannel = require('../structures/GuildChannel');
const PermissionOverwrites = require('../structures/PermissionOverwrites');
const Collection = require('../util/Collection');
const { ChannelTypes } = require('../util/Constants');
/**
* Manages API methods for GuildChannels and stores their cache.
* @extends {BaseManager}
*/
class GuildChannelManager extends BaseManager {
constructor(guild, iterable) {
super(guild.client, iterable, GuildChannel);
/**
* The guild this Manager belongs to
* @type {Guild}
*/
this.guild = guild;
}
/**
* The cache of this Manager
* @type {Collection<Snowflake, GuildChannel>}
* @name GuildChannelManager#cache
*/
add(channel) {
const existing = this.cache.get(channel.id);
if (existing) return existing;
this.cache.set(channel.id, channel);
return channel;
}
/**
* Data that can be resolved to give a Guild Channel object. This can be:
* * A GuildChannel object
* * A Snowflake
* @typedef {GuildChannel|Snowflake} GuildChannelResolvable
*/
/**
* Resolves a GuildChannelResolvable to a Channel object.
* @method resolve
* @memberof GuildChannelManager
* @instance
* @param {GuildChannelResolvable} channel The GuildChannel resolvable to resolve
* @returns {?GuildChannel}
*/
/**
* Resolves a GuildChannelResolvable to a channel ID string.
* @method resolveID
* @memberof GuildChannelManager
* @instance
* @param {GuildChannelResolvable} channel The GuildChannel resolvable to resolve
* @returns {?Snowflake}
*/
/**
* Creates a new channel in the guild.
* @param {string} name The name of the new channel
* @param {Object} [options] Options
* @param {string} [options.type='text'] The type of the new channel, either `text`, `voice`, or `category`
* @param {string} [options.topic] The topic for the new channel
* @param {boolean} [options.nsfw] Whether the new channel is nsfw
* @param {number} [options.bitrate] Bitrate of the new channel in bits (only voice)
* @param {number} [options.userLimit] Maximum amount of users allowed in the new channel (only voice)
* @param {ChannelResolvable} [options.parent] Parent of the new channel
* @param {OverwriteResolvable[]|Collection<Snowflake, OverwriteResolvable>} [options.permissionOverwrites]
* Permission overwrites of the new channel
* @param {number} [options.position] Position of the new channel
* @param {number} [options.rateLimitPerUser] The ratelimit per user for the channel
* @param {string} [options.reason] Reason for creating the channel
* @returns {Promise<GuildChannel>}
* @example
* // Create a new text channel
* guild.channels.create('new-general', { reason: 'Needed a cool new channel' })
* .then(console.log)
* .catch(console.error);
* @example
* // Create a new channel with permission overwrites
* guild.channels.create('new-voice', {
* type: 'voice',
* permissionOverwrites: [
* {
* id: message.author.id,
* deny: ['VIEW_CHANNEL'],
* },
* ],
* })
*/
async create(name, options = {}) {
let {
type,
topic,
nsfw,
bitrate,
userLimit,
parent,
permissionOverwrites,
position,
rateLimitPerUser,
reason,
} = options;
if (parent) parent = this.client.channels.resolveID(parent);
if (permissionOverwrites) {
permissionOverwrites = permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
}
const data = await this.client.api.guilds(this.guild.id).channels.post({
data: {
name,
topic,
type: type ? ChannelTypes[type.toUpperCase()] : ChannelTypes.TEXT,
nsfw,
bitrate,
user_limit: userLimit,
parent_id: parent,
position,
permission_overwrites: permissionOverwrites,
rate_limit_per_user: rateLimitPerUser,
},
reason,
});
return this.client.actions.ChannelCreate.handle(data).channel;
}
/**
* Obtains one or more guild channels from Discord, or the channel cache if they're already available.
* @param {Snowflake} [id] ID of the channel
* @param {boolean} [cache=true] Whether to cache the new channel objects if it weren't already
* @param {boolean} [force=false] Whether to skip the cache check and request the API
* @returns {Promise<?GuildChannel|Collection<Snowflake, GuildChannel>>}
* @example
* // Fetch all channels from the guild
* message.guild.channels.fetch()
* .then(channels => console.log(`There are ${channels.size} channels.`))
* .catch(console.error);
* @example
* // Fetch a single channel
* message.guild.channels.fetch('222197033908436994')
* .then(channel => console.log(`The channel name is: ${channel.name}`))
* .catch(console.error);
*/
async fetch(id, cache = true, force = false) {
if (id && !force) {
const existing = this.cache.get(id);
if (existing) return existing;
}
// We cannot fetch a single guild channel, as of this commit's date, Discord API throws with 404
const data = await this.client.api.guilds(this.guild.id).channels.get();
const channels = new Collection();
for (const channel of data) channels.set(channel.id, this.client.channels.add(channel, this.guild, cache));
return id ? channels.get(id) ?? null : channels;
}
}
module.exports = GuildChannelManager;