/
AuditLogEntry.java
355 lines (326 loc) · 10.9 KB
/
AuditLogEntry.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.core.audit;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.ISnowflake;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.entities.Webhook;
import net.dv8tion.jda.core.entities.impl.GuildImpl;
import net.dv8tion.jda.core.entities.impl.UserImpl;
import net.dv8tion.jda.core.entities.impl.WebhookImpl;
import net.dv8tion.jda.core.utils.Checks;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Single entry for an {@link net.dv8tion.jda.core.requests.restaction.pagination.AuditLogPaginationAction
* AuditLogPaginationAction}.
* <br>This entry contains all options/changes and details for the action
* that was logged by the {@link net.dv8tion.jda.core.entities.Guild Guild} audit-logs.
*/
public class AuditLogEntry implements ISnowflake
{
protected final long id;
protected final long targetId;
protected final GuildImpl guild;
protected final UserImpl user;
protected final WebhookImpl webhook;
protected final String reason;
protected final Map<String, AuditLogChange> changes;
protected final Map<String, Object> options;
protected final ActionType type;
public AuditLogEntry(ActionType type, long id, long targetId, GuildImpl guild, UserImpl user, WebhookImpl webhook,
String reason, Map<String, AuditLogChange> changes, Map<String, Object> options)
{
this.type = type;
this.id = id;
this.targetId = targetId;
this.guild = guild;
this.user = user;
this.webhook = webhook;
this.reason = reason;
this.changes = changes != null && !changes.isEmpty()
? Collections.unmodifiableMap(changes)
: Collections.emptyMap();
this.options = options != null && !options.isEmpty()
? Collections.unmodifiableMap(options)
: Collections.emptyMap();
}
@Override
public long getIdLong()
{
return id;
}
/**
* The id for the target entity.
* <br>This references an entity based on the {@link net.dv8tion.jda.core.audit.TargetType TargetType}
* which is specified by {@link #getTargetType()}!
*
* @return The target id
*/
public long getTargetIdLong()
{
return targetId;
}
/**
* The id for the target entity.
* <br>This references an entity based on the {@link net.dv8tion.jda.core.audit.TargetType TargetType}
* which is specified by {@link #getTargetType()}!
*
* @return The target id
*/
public String getTargetId()
{
return Long.toUnsignedString(targetId);
}
/**
* The {@link net.dv8tion.jda.core.entities.Webhook Webhook} that the target id of this audit-log entry refers to
*
* @return Possibly-null Webhook instance
*/
@Nullable
public Webhook getWebhook()
{
return webhook;
}
/**
* The {@link net.dv8tion.jda.core.entities.Guild Guild} this audit-log entry refers to
*
* @return The Guild instance
*/
public Guild getGuild()
{
return guild;
}
/**
* The {@link net.dv8tion.jda.core.entities.User User} responsible
* for this action.
*
* @return Possibly-null User instance
*/
@Nullable
public User getUser()
{
return user;
}
/**
* The optional reason why this action was executed.
*
* @return Possibly-null reason String
*/
@Nullable
public String getReason()
{
return reason;
}
/**
* The corresponding JDA instance of the referring Guild
*
* @return The corresponding JDA instance
*/
public JDA getJDA()
{
return guild.getJDA();
}
/**
* Key-Value {@link java.util.Map Map} containing all {@link AuditLogChange
* AuditLogChanges} made in this entry.
* The keys for the returned map are case-insensitive keys defined in the regarding AuditLogChange value.
* <br>To iterate only the changes you can use {@link java.util.Map#values() Map.values()}!
*
* @return Key-Value Map of changes
*/
public Map<String, AuditLogChange> getChanges()
{
return changes;
}
/**
* Shortcut to <code>{@link #getChanges() getChanges()}.get(key)</code> lookup!
* <br>This lookup is case-insensitive!
*
* @param key
* The {@link net.dv8tion.jda.core.audit.AuditLogKey AuditLogKey} to look for
*
* @return Possibly-null value corresponding to the specified key
*/
@Nullable
public AuditLogChange getChangeByKey(final AuditLogKey key)
{
return key == null ? null : getChangeByKey(key.getKey());
}
/**
* Shortcut to <code>{@link #getChanges() getChanges()}.get(key)</code> lookup!
* <br>This lookup is case-insensitive!
*
* @param key
* The key to look for
*
* @return Possibly-null value corresponding to the specified key
*/
@Nullable
public AuditLogChange getChangeByKey(final String key)
{
return changes.get(key);
}
/**
* Filters all changes by the specified keys
*
* @param keys
* Varargs {@link net.dv8tion.jda.core.audit.AuditLogKey AuditLogKeys} to look for
*
* @throws java.lang.IllegalArgumentException
* If provided with null array
*
* @return Possibly-empty, never-null immutable list of {@link AuditLogChange AuditLogChanges}
*/
public List<AuditLogChange> getChangesForKeys(AuditLogKey... keys)
{
Checks.notNull(keys, "Keys");
List<AuditLogChange> changes = new ArrayList<>(keys.length);
for (AuditLogKey key : keys)
{
AuditLogChange change = getChangeByKey(key);
if (change != null)
changes.add(change);
}
return Collections.unmodifiableList(changes);
}
/**
* Key-Value {@link java.util.Map Map} containing all Options made in this entry. The keys for the returned map are
* case-insensitive keys defined in the regarding AuditLogChange value.
* <br>To iterate only the changes you can use {@link java.util.Map#values() Map.values()}!
*
* <p>Options may include secondary targets or details that do not qualify as "change".
* <br>An example of that would be the {@code member} option
* for {@link net.dv8tion.jda.core.audit.ActionType#CHANNEL_OVERRIDE_UPDATE CHANNEL_OVERRIDE_UPDATE}
* containing the user_id of a {@link net.dv8tion.jda.core.entities.Member Member}.
*
* @return Key-Value Map of changes
*/
public Map<String, Object> getOptions()
{
return options;
}
/**
* Shortcut to <code>{@link #getOptions() getOptions()}.get(name)</code> lookup!
* <br>This lookup is case-insensitive!
*
* @param <T>
* The expected type for this option <br>Will be used for casting
* @param name
* The field name to look for
*
* @throws java.lang.ClassCastException
* If the type-cast failed for the generic type.
*
* @return Possibly-null value corresponding to the specified key
*/
@Nullable
@SuppressWarnings("unchecked")
public <T> T getOptionByName(String name)
{
return (T) options.get(name);
}
/**
* Shortcut to <code>{@link #getOptions() getOptions()}.get(name)</code> lookup!
*
* @param <T>
* The expected type for this option <br>Will be used for casting
* @param option
* The {@link net.dv8tion.jda.core.audit.AuditLogOption AuditLogOption}
*
* @throws java.lang.ClassCastException
* If the type-cast failed for the generic type.
* @throws java.lang.IllegalArgumentException
* If provided with {@code null} option.
*
* @return Possibly-null value corresponding to the specified option constant
*/
@Nullable
public <T> T getOption(AuditLogOption option)
{
Checks.notNull(option, "Option");
return getOptionByName(option.getKey());
}
/**
* Constructs a filtered, immutable list of options corresponding to
* the provided {@link net.dv8tion.jda.core.audit.AuditLogOption AuditLogOptions}.
* <br>This will exclude options with {@code null} values!
*
* @param options
* The not-null {@link net.dv8tion.jda.core.audit.AuditLogOption AuditLogOptions}
* which will be used to gather option values via {@link #getOption(AuditLogOption) getOption(AuditLogOption)}!
*
* @throws java.lang.IllegalArgumentException
* If provided with null options
*
* @return Unmodifiable list of representative values
*/
public List<Object> getOptions(AuditLogOption... options)
{
Checks.notNull(options, "Options");
List<Object> items = new ArrayList<>(options.length);
for (AuditLogOption option : options)
{
Object obj = getOption(option);
if (obj != null)
items.add(obj);
}
return Collections.unmodifiableList(items);
}
/**
* The {@link net.dv8tion.jda.core.audit.ActionType ActionType} defining what auditable
* Action is referred to by this entry.
*
* @return The {@link net.dv8tion.jda.core.audit.ActionType ActionType}
*/
public ActionType getType()
{
return type;
}
/**
* The {@link net.dv8tion.jda.core.audit.TargetType TargetType} defining what kind of
* entity was targeted by this action.
* <br>Shortcut for {@code getType().getTargetType()}
*
* @return The {@link net.dv8tion.jda.core.audit.TargetType TargetType}
*/
public TargetType getTargetType()
{
return type.getTargetType();
}
@Override
public int hashCode()
{
return Long.hashCode(id);
}
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof AuditLogEntry))
return false;
AuditLogEntry other = (AuditLogEntry) obj;
return other.id == id && other.targetId == targetId;
}
@Override
public String toString()
{
return "ALE:" + type + "(ID:" + id + " / TID:" + targetId + " / " + guild + ')';
}
}