/
GenericPermissionOverrideEvent.java
210 lines (193 loc) · 5.98 KB
/
GenericPermissionOverrideEvent.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
/*
* 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.events.guild.override;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Indicates that a {@link PermissionOverride} for a {@link net.dv8tion.jda.api.entities.GuildChannel GuildChannel} was created, deleted, or updated.
* <br>Every guild channel override event is a subclass of this event and can be casted
*
* <p>Can be used to detect that any guild channel override event was fired
*/
public class GenericPermissionOverrideEvent extends GenericGuildEvent
{
protected final GuildChannel channel;
protected final PermissionOverride override;
public GenericPermissionOverrideEvent(@Nonnull JDA api, long responseNumber, @Nonnull GuildChannel channel, @Nonnull PermissionOverride override)
{
super(api, responseNumber, channel.getGuild());
this.channel = channel;
this.override = override;
}
/**
* The {@link ChannelType} of the {@link #getChannel() GuildChannel} this override belongs to.
*
* @return The {@link ChannelType}
*/
@Nonnull
public ChannelType getChannelType()
{
return channel.getType();
}
/**
* The {@link GuildChannel} this override belongs to.
*
* @return The {@link GuildChannel}
*/
@Nonnull
public GuildChannel getChannel()
{
return channel;
}
/**
* The {@link TextChannel} this override belongs to.
*
* @throws IllegalStateException
* If the override does not belong to a TextChannel
*
* @return {@link TextChannel}
*
* @see #getChannel()
* @see #getChannelType()
*/
@Nonnull
public TextChannel getTextChannel()
{
if (channel instanceof TextChannel)
return (TextChannel) channel;
throw new IllegalStateException("This override is for a channel of type " + getChannelType());
}
/**
* The {@link VoiceChannel} this override belongs to.
*
* @throws IllegalStateException
* If the override does not belong to a VoiceChannel
*
* @return {@link VoiceChannel}
*
* @see #getChannel()
* @see #getChannelType()
*/
@Nonnull
public VoiceChannel getVoiceChannel()
{
if (channel instanceof VoiceChannel)
return (VoiceChannel) channel;
throw new IllegalStateException("This override is for a channel of type " + getChannelType());
}
/**
* The {@link StoreChannel} this override belongs to.
*
* @throws IllegalStateException
* If the override does not belong to a StoreChannel
*
* @return {@link StoreChannel}
*
* @see #getChannel()
* @see #getChannelType()
*/
@Nonnull
public StoreChannel getStoreChannel()
{
if (channel instanceof StoreChannel)
return (StoreChannel) channel;
throw new IllegalStateException("This override is for a channel of type " + getChannelType());
}
/**
* The {@link Category} this override belongs to.
* <br>Note: This is not the same as {@code getChannel().getParent()}!
*
* @throws IllegalStateException
* If the override does not belong to a Category
*
* @return {@link Category}
*
* @see #getChannel()
* @see #getChannelType()
*/
@Nonnull
public Category getCategory()
{
if (channel instanceof Category)
return (Category) channel;
throw new IllegalStateException("This override is for a channel of type " + getChannelType());
}
/**
* The affected {@link PermissionOverride} that was updated.
*
* @return The override
*/
@Nonnull
public PermissionOverride getPermissionOverride()
{
return override;
}
/**
* Whether this override was for a role.
* <br>This means {@link #getRole()} is likely not null.
*
* @return True, if this override is for a role
*/
public boolean isRoleOverride()
{
return override.isRoleOverride();
}
/**
* Whether this override was for a member.
* <br>Note that {@link #getMember()} might still be null if the member isn't cached or there is a discord inconsistency.
*
* @return True, if this override is for a member
*/
public boolean isMemberOverride()
{
return override.isMemberOverride();
}
/**
* The {@link IPermissionHolder} for the override.
* <br>This can be a {@link Member} or {@link Role}. If the role or member are not cached then this will be null.
*
* @return Possibly-null permission holder
*/
@Nullable
public IPermissionHolder getPermissionHolder()
{
return isMemberOverride() ? override.getMember() : override.getRole();
}
/**
* The {@link Member} for the override.
* <br>This can be null if the member is not cached or there is a discord inconsistency.
*
* @return Possibly-null member
*/
@Nullable
public Member getMember()
{
return override.getMember();
}
/**
* The {@link Role} for the override.
*
* @return Possibly-null role
*/
@Nullable
public Role getRole()
{
return override.getRole();
}
}