/
Command.java
719 lines (657 loc) · 21 KB
/
Command.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
* 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.interactions.commands;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.ISnowflake;
import net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.CommandEditAction;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.data.DataType;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.requests.restaction.CommandEditActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Represents a Discord slash-command.
* <br>This can be used to edit or delete the command.
*
* @see Guild#retrieveCommandById(String)
* @see Guild#retrieveCommands()
*/
public class Command implements ISnowflake
{
private static final EnumSet<OptionType> OPTIONS = EnumSet.complementOf(EnumSet.of(OptionType.SUB_COMMAND, OptionType.SUB_COMMAND_GROUP));
private static final Predicate<DataObject> OPTION_TEST = it -> OPTIONS.contains(OptionType.fromKey(it.getInt("type")));
private static final Predicate<DataObject> SUBCOMMAND_TEST = it -> OptionType.fromKey(it.getInt("type")) == OptionType.SUB_COMMAND;
private static final Predicate<DataObject> GROUP_TEST = it -> OptionType.fromKey(it.getInt("type")) == OptionType.SUB_COMMAND_GROUP;
private final JDAImpl api;
private final Guild guild;
private final String name, description;
private final List<Option> options;
private final List<SubcommandGroup> groups;
private final List<Subcommand> subcommands;
private final long id, guildId, applicationId;
private final boolean defaultEnabled;
public Command(JDAImpl api, Guild guild, DataObject json)
{
this.api = api;
this.guild = guild;
this.name = json.getString("name");
this.description = json.getString("description");
this.id = json.getUnsignedLong("id");
this.defaultEnabled = json.getBoolean("default_permission");
this.guildId = guild != null ? guild.getIdLong() : 0L;
this.applicationId = json.getUnsignedLong("application_id", api.getSelfUser().getApplicationIdLong());
this.options = parseOptions(json, OPTION_TEST, Option::new);
this.groups = parseOptions(json, GROUP_TEST, SubcommandGroup::new);
this.subcommands = parseOptions(json, SUBCOMMAND_TEST, Subcommand::new);
}
protected static <T> List<T> parseOptions(DataObject json, Predicate<DataObject> test, Function<DataObject, T> transform)
{
return json.optArray("options").map(arr ->
arr.stream(DataArray::getObject)
.filter(test)
.map(transform)
.collect(Collectors.toList())
).orElse(Collections.emptyList());
}
/**
* Delete this command.
* <br>If this is a global command it may take up to 1 hour to vanish from all clients.
*
* @throws IllegalStateException
* If this command is not owned by this bot
*
* @return {@link RestAction}
*/
@Nonnull
@CheckReturnValue
public RestAction<Void> delete()
{
if (applicationId != api.getSelfUser().getApplicationIdLong())
throw new IllegalStateException("Cannot delete a command from another bot!");
Route.CompiledRoute route;
String appId = getJDA().getSelfUser().getApplicationId();
if (guildId != 0L)
route = Route.Interactions.DELETE_GUILD_COMMAND.compile(appId, Long.toUnsignedString(guildId), getId());
else
route = Route.Interactions.DELETE_COMMAND.compile(appId, getId());
return new RestActionImpl<>(api, route);
}
/**
* Edit this command.
* <br>This can be used to change the command attributes such as name or description.
*
* @throws IllegalStateException
* If this command is not owned by this bot
*
* @return {@link CommandEditAction}
*/
@Nonnull
@CheckReturnValue
public CommandEditAction editCommand()
{
if (applicationId != api.getSelfUser().getApplicationIdLong())
throw new IllegalStateException("Cannot edit a command from another bot!");
return guild == null ? new CommandEditActionImpl(api, getId()) : new CommandEditActionImpl(guild, getId());
}
/**
* Retrieves the {@link CommandPrivilege CommandPrivileges} for this command.
* <br>This is a shortcut for {@link Guild#retrieveCommandPrivilegesById(String)}.
*
* <p>These privileges are used to restrict who can use commands through Role/User whitelists/blacklists.
*
* <p>If there is no command with the provided ID,
* this RestAction fails with {@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_COMMAND ErrorResponse.UNKNOWN_COMMAND}
*
* @param guild
* The target guild from which to retrieve the privileges
*
* @throws IllegalArgumentException
* If the guild is null
*
* @return {@link RestAction} - Type: {@link List} of {@link CommandPrivilege}
*/
@Nonnull
@CheckReturnValue
public RestAction<List<CommandPrivilege>> retrievePrivileges(@Nonnull Guild guild)
{
Checks.notNull(guild, "Guild");
return guild.retrieveCommandPrivilegesById(id);
}
/**
* Updates the list of {@link CommandPrivilege CommandPrivileges} for this command.
*
* <p>These privileges are used to restrict who can use commands through Role/User whitelists/blacklists.
*
* <p>If there is no command with the provided ID,
* this RestAction fails with {@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_COMMAND ErrorResponse.UNKNOWN_COMMAND}
*
* @param guild
* The target guild from which to update the privileges
* @param privileges
* Complete list of {@link CommandPrivilege CommandPrivileges} for this command
*
* @throws IllegalArgumentException
* If null is provided
* @throws IllegalStateException
* If this command is not owned by this bot
*
* @return {@link RestAction} - Type: {@link List} or {@link CommandPrivilege}
* The updated list of privileges for this command.
*/
@Nonnull
@CheckReturnValue
public RestAction<List<CommandPrivilege>> updatePrivileges(@Nonnull Guild guild, @Nonnull Collection<? extends CommandPrivilege> privileges)
{
if (applicationId != api.getSelfUser().getApplicationIdLong())
throw new IllegalStateException("Cannot update privileges for a command from another bot!");
Checks.notNull(guild, "Guild");
return guild.updateCommandPrivilegesById(id, privileges);
}
/**
* Updates the list of {@link CommandPrivilege CommandPrivileges} for this command.
*
* <p>These privileges are used to restrict who can use commands through Role/User whitelists/blacklists.
*
* <p>If there is no command with the provided ID,
* this RestAction fails with {@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_COMMAND ErrorResponse.UNKNOWN_COMMAND}
*
* @param guild
* The target guild from which to update the privileges
* @param privileges
* Complete list of {@link CommandPrivilege CommandPrivileges} for this command
*
* @throws IllegalArgumentException
* If null is provided
* @throws IllegalStateException
* If this command is not owned by this bot
*
* @return {@link RestAction} - Type: {@link List} or {@link CommandPrivilege}
* The updated list of privileges for this command.
*/
@Nonnull
@CheckReturnValue
public RestAction<List<CommandPrivilege>> updatePrivileges(@Nonnull Guild guild, @Nonnull CommandPrivilege... privileges)
{
Checks.noneNull(privileges, "CommandPrivileges");
return updatePrivileges(guild, Arrays.asList(privileges));
}
/**
* Returns the {@link net.dv8tion.jda.api.JDA JDA} instance of this Command
*
* @return the corresponding JDA instance
*/
@Nonnull
public JDA getJDA()
{
return api;
}
/**
* The name of this command.
*
* @return The name
*/
@Nonnull
public String getName()
{
return name;
}
/**
* The description of this command.
*
* @return The description
*/
@Nonnull
public String getDescription()
{
return description;
}
/**
* Whether this command is enabled for everyone by default.
*
* @return True, if everyone can use this command by default.
*/
public boolean isDefaultEnabled()
{
return defaultEnabled;
}
/**
* The {@link Option Options} of this command.
*
* @return Immutable list of command options
*/
@Nonnull
public List<Option> getOptions()
{
return options;
}
/**
* The {@link Subcommand Subcommands} of this command.
*
* @return Immutable list of subcommands
*/
@Nonnull
public List<Subcommand> getSubcommands()
{
return subcommands;
}
/**
* The {@link SubcommandGroup SubcommandGroups} of this command.
*
* @return Immutable list of subcommand groups
*/
@Nonnull
public List<SubcommandGroup> getSubcommandGroups()
{
return groups;
}
/**
* The id of the application this command belongs to.
*
* @return The application id
*/
public long getApplicationIdLong()
{
return applicationId;
}
/**
* The id of the application this command belongs to.
*
* @return The application id
*/
@Nonnull
public String getApplicationId()
{
return Long.toUnsignedString(applicationId);
}
@Override
public long getIdLong()
{
return id;
}
@Override
public String toString()
{
return "C:" + getName() + "(" + getId() + ")";
}
@Override
public boolean equals(Object obj)
{
if (obj == this)
return true;
if (!(obj instanceof Command))
return false;
return id == ((Command) obj).id;
}
@Override
public int hashCode()
{
return Long.hashCode(id);
}
/**
* Predefined choice used for options.
*
* @see net.dv8tion.jda.api.interactions.commands.build.OptionData#addChoices(Command.Choice...)
* @see net.dv8tion.jda.api.interactions.commands.build.OptionData#addChoices(Collection)
*/
public static class Choice
{
private final String name;
private final long intValue;
private final String stringValue;
/**
* Create a Choice tuple
*
* @param name
* The display name of this choice
* @param value
* The integer value you receive in a command option
*/
public Choice(@Nonnull String name, long value)
{
this.name = name;
this.intValue = value;
this.stringValue = Long.toString(value);
}
/**
* Create a Choice tuple
*
* @param name
* The display name of this choice
* @param value
* The string value you receive in a command option
*/
public Choice(@Nonnull String name, @Nonnull String value)
{
this.name = name;
this.intValue = 0;
this.stringValue = value;
}
/**
* Create a Choice tuple
*
* @param json
* The serialized choice instance with name and value mapping
*
* @throws IllegalArgumentException
* If null is provided
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the data is not formatted correctly or missing required parameters
*/
public Choice(@Nonnull DataObject json)
{
Checks.notNull(json, "DataObject");
this.name = json.getString("name");
if (json.isType("value", DataType.INT))
{
this.intValue = json.getLong("value");
this.stringValue = Long.toString(intValue); // does this make sense?
}
else
{
this.intValue = 0;
this.stringValue = json.getString("value");
}
}
/**
* The readable name of this choice.
* <br>This is shown to the user in the official client.
*
* @return The choice name
*/
@Nonnull
public String getName()
{
return name;
}
/**
* The value of this choice.
*
* @return The long value
*/
public long getAsLong()
{
return intValue;
}
/**
* The value of this choice.
*
* @return The String value
*/
@Nonnull
public String getAsString()
{
return stringValue;
}
@Override
public int hashCode()
{
return Objects.hash(name, stringValue);
}
@Override
public boolean equals(Object obj)
{
if (obj == this) return true;
if (!(obj instanceof Choice)) return false;
Choice other = (Choice) obj;
return Objects.equals(other.name, name) && Objects.equals(other.stringValue, stringValue);
}
@Override
public String toString()
{
return "Choice(" + name + "," + stringValue + ")";
}
}
/**
* An Option for a command.
*/
public static class Option
{
private final String name, description;
private final int type;
private final boolean required;
private final List<Choice> choices;
public Option(@Nonnull DataObject json)
{
this.name = json.getString("name");
this.description = json.getString("description");
this.type = json.getInt("type");
this.required = json.getBoolean("required");
this.choices = json.optArray("choices")
.map(it -> it.stream(DataArray::getObject).map(Choice::new).collect(Collectors.toList()))
.orElse(Collections.emptyList());
}
/**
* The name of this option, subcommand, or subcommand group.
*
* @return The name
*/
@Nonnull
public String getName()
{
return name;
}
/**
* The description of this option, subcommand, or subcommand group.
*
* @return The description
*/
@Nonnull
public String getDescription()
{
return description;
}
/**
* The raw option type.
*
* @return The type
*/
public int getTypeRaw()
{
return type;
}
/**
* Whether this option is required
*
* @return True if this option is required
*/
public boolean isRequired()
{
return required;
}
/**
* The {@link OptionType}.
*
* @return The type
*/
@Nonnull
public OptionType getType()
{
return OptionType.fromKey(type);
}
/**
* The predefined choices available for this option.
* <br>If no choices are defined, this returns an empty list.
*
* @return Immutable {@link List} of {@link Choice}
*/
@Nonnull
public List<Choice> getChoices()
{
return choices;
}
@Override
public int hashCode()
{
return Objects.hash(name, description, type, choices);
}
@Override
public boolean equals(Object obj)
{
if (obj == this) return true;
if (!(obj instanceof Option)) return false;
Option other = (Option) obj;
return Objects.equals(other.name, name)
&& Objects.equals(other.description, description)
&& Objects.equals(other.choices, choices)
&& other.type == type;
}
@Override
public String toString()
{
return "Option[" + getType() + "](" + name + ")";
}
}
/**
* An Subcommand for a command.
*/
public static class Subcommand
{
private final String name, description;
private final List<Option> options;
public Subcommand(DataObject json)
{
this.name = json.getString("name");
this.description = json.getString("description");
this.options = parseOptions(json, OPTION_TEST, Option::new);
}
/**
* The name of this subcommand.
*
* @return The name
*/
@Nonnull
public String getName()
{
return name;
}
/**
* The description of this subcommand.
*
* @return The description
*/
@Nonnull
public String getDescription()
{
return description;
}
/**
* The options for this subcommand, or the subcommands within this group.
*
* @return Immutable list of Options
*/
@Nonnull
public List<Option> getOptions()
{
return options;
}
@Override
public int hashCode()
{
return Objects.hash(name, description, options);
}
@Override
public boolean equals(Object obj)
{
if (obj == this) return true;
if (!(obj instanceof Subcommand)) return false;
Subcommand other = (Subcommand) obj;
return Objects.equals(other.name, name)
&& Objects.equals(other.description, description)
&& Objects.equals(other.options, options);
}
@Override
public String toString()
{
return "Subcommand(" + name + ")";
}
}
/**
* An Subcommand Group for a command.
*/
public static class SubcommandGroup
{
private final String name, description;
private final List<Subcommand> subcommands;
public SubcommandGroup(DataObject json)
{
this.name = json.getString("name");
this.description = json.getString("description");
this.subcommands = parseOptions(json, SUBCOMMAND_TEST, Subcommand::new);
}
/**
* The name of this subcommand group.
*
* @return The name
*/
@Nonnull
public String getName()
{
return name;
}
/**
* The description of this subcommand group.
*
* @return The description
*/
@Nonnull
public String getDescription()
{
return description;
}
/**
* The {@link Subcommand Subcommands} in this group
*
* @return Immutable {@link List} of {@link Subcommand}
*/
@Nonnull
public List<Subcommand> getSubcommands()
{
return subcommands;
}
@Override
public int hashCode()
{
return Objects.hash(name, description, subcommands);
}
@Override
public boolean equals(Object obj)
{
if (obj == this) return true;
if (!(obj instanceof SubcommandGroup)) return false;
SubcommandGroup other = (SubcommandGroup) obj;
return Objects.equals(other.name, name)
&& Objects.equals(other.description, description)
&& Objects.equals(other.subcommands, subcommands);
}
@Override
public String toString()
{
return "SubcommandGroup(" + name + ")";
}
}
}