-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
CategoryImpl.java
163 lines (144 loc) · 5.03 KB
/
CategoryImpl.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
package net.dv8tion.jda.core.entities.impl;
import gnu.trove.map.TLongObjectMap;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.requests.RestAction;
import net.dv8tion.jda.core.requests.restaction.ChannelAction;
import net.dv8tion.jda.core.requests.restaction.InviteAction;
import net.dv8tion.jda.core.utils.Checks;
import net.dv8tion.jda.core.utils.MiscUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class CategoryImpl extends AbstractChannelImpl<CategoryImpl> implements Category
{
protected final TLongObjectMap<Channel> channels = MiscUtil.newLongMap();
public CategoryImpl(long id, GuildImpl guild)
{
super(id, guild);
}
@Override
public Category getParent()
{
return null;
}
@Override
public int compareTo(Category other)
{
Checks.notNull(other, "Other Category");
if (other.equals(this))
return 0;
Checks.check(getGuild().equals(other.getGuild()), "Cannot compare categories from different guilds!");
if (rawPosition == other.getPositionRaw())
return Long.compare(id, other.getIdLong());
return Integer.compare(rawPosition, other.getPositionRaw());
}
@Override
public ChannelType getType()
{
return ChannelType.CATEGORY;
}
@Override
public List<Member> getMembers()
{
return Collections.unmodifiableList(getChannels().stream()
.map(Channel::getMembers)
.flatMap(List::stream)
.distinct()
.collect(Collectors.toList()));
}
@Override
public int getPosition()
{
//We call getCategories instead of directly accessing the GuildImpl.getCategories because
// getCategories does the sorting logic.
List<Category> channels = guild.getCategories();
for (int i = 0; i < channels.size(); i++)
{
if (channels.get(i) == this)
return i;
}
throw new AssertionError("Somehow when determining position we never found the Category in the Guild's channels? wtf?");
}
@Override
public ChannelAction createCopy(Guild guild)
{
Checks.notNull(guild, "Guild");
ChannelAction action = guild.getController().createCategory(name);
if (guild.equals(getGuild()))
{
for (PermissionOverride o : overrides.valueCollection())
{
if (o.isMemberOverride())
action.addPermissionOverride(o.getMember(), o.getAllowedRaw(), o.getDeniedRaw());
else
action.addPermissionOverride(o.getRole(), o.getAllowedRaw(), o.getDeniedRaw());
}
}
return action;
}
@Override
public InviteAction createInvite()
{
throw new UnsupportedOperationException("Cannot create invites for category!");
}
@Override
public RestAction<List<Invite>> getInvites()
{
return new RestAction.EmptyRestAction<>(getJDA(), Collections.emptyList());
}
@Override
public List<Channel> getChannels()
{
List<Channel> channels = new ArrayList<>();
channels.addAll(getTextChannels());
channels.addAll(getVoiceChannels());
return Collections.unmodifiableList(channels);
}
@Override
public List<TextChannel> getTextChannels()
{
return Collections.unmodifiableList(getGuild().getTextChannels().stream()
.filter(channel -> channel.getParent() != null)
.filter(channel -> channel.getParent().equals(this))
.collect(Collectors.toList()));
}
@Override
public List<VoiceChannel> getVoiceChannels()
{
return Collections.unmodifiableList(getGuild().getVoiceChannels().stream()
.filter(channel -> channel.getParent() != null)
.filter(channel -> channel.getParent().equals(this))
.collect(Collectors.toList()));
}
@Override
public ChannelAction createTextChannel(String name)
{
ChannelAction action = guild.getController().createTextChannel(name).setParent(this);
applyPermission(action);
return action;
}
@Override
public ChannelAction createVoiceChannel(String name)
{
ChannelAction action = guild.getController().createVoiceChannel(name).setParent(this);
applyPermission(action);
return action;
}
@Override
public String toString()
{
return "GC:" + getName() + '(' + id + ')';
}
private void applyPermission(ChannelAction a)
{
overrides.forEachValue(override ->
{
if (override.isMemberOverride())
a.addPermissionOverride(override.getMember(), override.getAllowedRaw(), override.getDeniedRaw());
else
a.addPermissionOverride(override.getRole(), override.getAllowedRaw(), override.getDeniedRaw());
return true;
});
}
}