/
Category.java
327 lines (309 loc) · 14.6 KB
/
Category.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.entities.channel.concrete;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.IPermissionHolder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.attribute.ICopyableChannel;
import net.dv8tion.jda.api.entities.channel.attribute.IMemberContainer;
import net.dv8tion.jda.api.entities.channel.attribute.IPermissionContainer;
import net.dv8tion.jda.api.entities.channel.attribute.IPositionableChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.managers.channel.concrete.CategoryManager;
import net.dv8tion.jda.api.requests.restaction.ChannelAction;
import net.dv8tion.jda.api.requests.restaction.order.CategoryOrderAction;
import net.dv8tion.jda.api.requests.restaction.order.ChannelOrderAction;
import net.dv8tion.jda.api.requests.restaction.order.OrderAction;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* Represents a channel category in the official Discord API.
* <br>Categories are used to keep order in a Guild by dividing the channels into groups.
*
* @since 3.4.0
*
* @see Guild#getCategoryCache()
* @see Guild#getCategories()
* @see Guild#getCategoriesByName(String, boolean)
* @see Guild#getCategoryById(long)
*
* @see JDA#getCategoryCache()
* @see JDA#getCategories()
* @see JDA#getCategoriesByName(String, boolean)
* @see JDA#getCategoryById(long)
*/
public interface Category extends GuildChannel, ICopyableChannel, IPositionableChannel, IPermissionContainer, IMemberContainer
{
/**
* All {@link GuildChannel Channels} listed
* for this Category
* <br>This may contain {@link VoiceChannel VoiceChannels},
* and {@link TextChannel TextChannels}!
*
* @return Immutable list of all child channels
*/
@Nonnull
default List<GuildChannel> getChannels()
{
List<GuildChannel> channels = new ArrayList<>();
channels.addAll(getTextChannels());
channels.addAll(getVoiceChannels());
channels.addAll(getStageChannels());
channels.addAll(getNewsChannels());
Collections.sort(channels);
return Collections.unmodifiableList(channels);
}
/**
* All {@link TextChannel TextChannels}
* listed for this Category
*
* @return Immutable list of all child TextChannels
*/
@Nonnull
default List<TextChannel> getTextChannels()
{
return Collections.unmodifiableList(getGuild().getTextChannelCache().stream()
.filter(channel -> equals(channel.getParentCategory()))
.sorted().collect(Collectors.toList()));
}
/**
* All {@link NewsChannel NewsChannels}
* listed for this Category
*
* @return Immutable list of all child NewsChannels
*/
@Nonnull
default List<NewsChannel> getNewsChannels()
{
return Collections.unmodifiableList(getGuild().getNewsChannelCache().stream()
.filter(channel -> equals(channel.getParentCategory()))
.sorted().collect(Collectors.toList()));
}
/**
* All {@link VoiceChannel VoiceChannels}
* listed for this Category
*
* @return Immutable list of all child VoiceChannels
*/
@Nonnull
default List<VoiceChannel> getVoiceChannels()
{
return Collections.unmodifiableList(getGuild().getVoiceChannelCache().stream()
.filter(channel -> equals(channel.getParentCategory()))
.sorted().collect(Collectors.toList()));
}
/**
* All {@link StageChannel StageChannel}
* listed for this Category
*
* @return Immutable list of all child StageChannel
*/
@Nonnull
default List<StageChannel> getStageChannels()
{
return Collections.unmodifiableList(getGuild().getStageChannelCache().stream()
.filter(channel -> equals(channel.getParentCategory()))
.sorted().collect(Collectors.toList()));
}
/**
* Creates a new {@link TextChannel TextChannel} with this Category as parent.
* For this to be successful, the logged in account has to have the
* {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} Permission in this Category.
*
* <p>This will copy all {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverrides} of this Category!
* Unless the bot is unable to sync it with this category due to permission escalation.
* See {@link IPermissionHolder#canSync(IPermissionContainer, IPermissionContainer)} for details.
*
* <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} caused by
* the returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} include the following:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
* <br>The channel could not be created due to a permission discrepancy</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
* <br>The {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL VIEW_CHANNEL} permission was removed</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MAX_CHANNELS MAX_CHANNELS}
* <br>The maximum number of channels were exceeded</li>
* </ul>
*
* @param name
* The name of the TextChannel to create
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL} permission
* @throws IllegalArgumentException
* If the provided name is {@code null} or empty or greater than 100 characters in length
*
* @return A specific {@link ChannelAction ChannelAction}
* <br>This action allows to set fields for the new TextChannel before creating it
*/
@Nonnull
@CheckReturnValue
ChannelAction<TextChannel> createTextChannel(@Nonnull String name);
/**
* Creates a new {@link VoiceChannel VoiceChannel} with this Category as parent.
* For this to be successful, the logged in account has to have the
* {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} Permission in this Category.
*
* <p>This will copy all {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverrides} of this Category!
* Unless the bot is unable to sync it with this category due to permission escalation.
* See {@link IPermissionHolder#canSync(IPermissionContainer, IPermissionContainer)} for details.
*
* <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} caused by
* the returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} include the following:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
* <br>The channel could not be created due to a permission discrepancy</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
* <br>The {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL VIEW_CHANNEL} permission was removed</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MAX_CHANNELS MAX_CHANNELS}
* <br>The maximum number of channels were exceeded</li>
* </ul>
*
* @param name
* The name of the VoiceChannel to create
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL} permission
* @throws IllegalArgumentException
* If the provided name is {@code null} or empty or greater than 100 characters in length
*
* @return A specific {@link ChannelAction ChannelAction}
* <br>This action allows to set fields for the new VoiceChannel before creating it
*/
@Nonnull
@CheckReturnValue
ChannelAction<VoiceChannel> createVoiceChannel(@Nonnull String name);
/**
* Creates a new {@link StageChannel StageChannel} with this Category as parent.
* For this to be successful, the logged in account has to have the
* {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL MANAGE_CHANNEL} Permission in this Category.
*
* <p>This will copy all {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverrides} of this Category!
* Unless the bot is unable to sync it with this category due to permission escalation.
* See {@link IPermissionHolder#canSync(IPermissionContainer, IPermissionContainer)} for details.
*
* <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} caused by
* the returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} include the following:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
* <br>The channel could not be created due to a permission discrepancy</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
* <br>The {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL VIEW_CHANNEL} permission was removed</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MAX_CHANNELS MAX_CHANNELS}
* <br>The maximum number of channels were exceeded</li>
* </ul>
*
* @param name
* The name of the StageChannel to create
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL} permission
* @throws IllegalArgumentException
* If the provided name is {@code null} or empty or greater than 100 characters in length
*
* @return A specific {@link ChannelAction ChannelAction}
* <br>This action allows to set fields for the new StageChannel before creating it
*/
@Nonnull
@CheckReturnValue
ChannelAction<StageChannel> createStageChannel(@Nonnull String name);
/**
* Modifies the positional order of this Category's nested {@link #getTextChannels() TextChannels} and {@link #getNewsChannels() NewsChannels}.
* <br>This uses an extension of {@link ChannelOrderAction ChannelOrderAction}
* specialized for ordering the nested {@link TextChannel TextChannels}
* and {@link NewsChannel NewsChannels} of this {@link Category Category}.
* <br>Like {@link ChannelOrderAction}, the returned {@link CategoryOrderAction CategoryOrderAction}
* can be used to move TextChannels/NewsChannels {@link OrderAction#moveUp(int) up},
* {@link OrderAction#moveDown(int) down}, or
* {@link OrderAction#moveTo(int) to} a specific position.
* <br>This uses <b>ascending</b> order with a 0 based index.
*
* <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} include:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_CHANNEL UNNKOWN_CHANNEL}
* <br>One of the channels has been deleted before the completion of the task.</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
* <br>The currently logged in account was removed from the Guild.</li>
* </ul>
*
* @return A {@link CategoryOrderAction CategoryOrderAction} for
* ordering the Category's {@link TextChannel TextChannels}
* and {@link NewsChannel NewsChannels}.
*/
@Nonnull
@CheckReturnValue
CategoryOrderAction modifyTextChannelPositions();
/**
* Modifies the positional order of this Category's nested {@link #getVoiceChannels() VoiceChannels} and {@link #getStageChannels() StageChannels}.
* <br>This uses an extension of {@link ChannelOrderAction ChannelOrderAction}
* specialized for ordering the nested {@link VoiceChannel VoiceChannels}
* and {@link StageChannel StageChannels} of this {@link Category Category}.
* <br>Like {@link ChannelOrderAction}, the returned {@link CategoryOrderAction CategoryOrderAction}
* can be used to move VoiceChannels/StageChannels {@link OrderAction#moveUp(int) up},
* {@link OrderAction#moveDown(int) down}, or
* {@link OrderAction#moveTo(int) to} a specific position.
* <br>This uses <b>ascending</b> order with a 0 based index.
*
* <p>Possible {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} include:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_CHANNEL UNNKOWN_CHANNEL}
* <br>One of the channels has been deleted before the completion of the task.</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
* <br>The currently logged in account was removed from the Guild.</li>
* </ul>
*
* @return A {@link CategoryOrderAction CategoryOrderAction} for
* ordering the Category's {@link VoiceChannel VoiceChannels}
* and {@link StageChannel StageChannels}.
*/
@Nonnull
@CheckReturnValue
CategoryOrderAction modifyVoiceChannelPositions();
@Nonnull
@Override
default List<Member> getMembers()
{
return Collections.unmodifiableList(getChannels().stream()
.filter(IMemberContainer.class::isInstance)
.map(IMemberContainer.class::cast)
.map(IMemberContainer::getMembers)
.flatMap(List::stream)
.distinct()
.collect(Collectors.toList()));
}
@Nonnull
@Override
ChannelAction<Category> createCopy(@Nonnull Guild guild);
@Nonnull
@Override
ChannelAction<Category> createCopy();
@Nonnull
@Override
CategoryManager getManager();
}