/
SubcommandData.java
203 lines (192 loc) · 7.44 KB
/
SubcommandData.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
/*
* 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.build;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.data.SerializableData;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.Nonnull;
import java.util.Collection;
/**
* Builder for a Slash-Command subcommand.
*/
public class SubcommandData extends BaseCommand<CommandData> implements SerializableData
{
private boolean allowRequired = true;
/**
* Create an subcommand builder.
*
* @param name
* The subcommand name, 1-32 lowercase alphanumeric characters
* @param description
* The subcommand description, 1-100 characters
*
* @throws IllegalArgumentException
* If any of the following requirements are not met
* <ul>
* <li>The name must be lowercase alphanumeric (with dash), 1-32 characters long</li>
* <li>The description must be 1-100 characters long</li>
* </ul>
*/
public SubcommandData(@Nonnull String name, @Nonnull String description)
{
super(name, description);
}
/**
* Adds up to 25 options to this subcommand.
*
* <p>Required options must be added before non-required options!
*
* @param options
* The {@link OptionData options} to add
*
* @throws IllegalArgumentException
* <ul>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If null is provided</li>
* </ul>
*
* @return The SubcommandData instance, for chaining
*/
@Nonnull
public SubcommandData addOptions(@Nonnull OptionData... options)
{
Checks.noneNull(options, "Option");
Checks.check(options.length + this.options.length() <= 25, "Cannot have more than 25 options for a subcommand!");
for (OptionData option : options)
{
Checks.check(option.getType() != OptionType.SUB_COMMAND, "Cannot add a subcommand to a subcommand!");
Checks.check(option.getType() != OptionType.SUB_COMMAND_GROUP, "Cannot add a subcommand group to a subcommand!");
Checks.check(allowRequired || !option.isRequired(), "Cannot add required options after non-required options!");
allowRequired = option.isRequired(); // prevent adding required options after non-required options
this.options.add(option);
}
return this;
}
/**
* Adds up to 25 options to this subcommand.
*
* <p>Required options must be added before non-required options!
*
* @param options
* The {@link OptionData options} to add
*
* @throws IllegalArgumentException
* <ul>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If null is provided</li>
* </ul>
*
* @return The SubcommandData instance, for chaining
*/
@Nonnull
public SubcommandData addOptions(@Nonnull Collection<? extends OptionData> options)
{
Checks.noneNull(options, "Options");
return addOptions(options.toArray(new OptionData[0]));
}
/**
* Adds an option to this subcommand.
*
* <p>Required options must be added before non-required options!
*
* @param type
* The {@link OptionData}
* @param name
* The lowercase option name, 1-32 characters
* @param description
* The option description, 1-100 characters
* @param required
* Whether this option is required (See {@link OptionData#setRequired(boolean)})
*
* @throws IllegalArgumentException
* <ul>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If null is provided</li>
* </ul>
*
* @return The SubcommandData instance, for chaining
*/
@Nonnull
public SubcommandData addOption(@Nonnull OptionType type, @Nonnull String name, @Nonnull String description, boolean required)
{
return addOptions(new OptionData(type, name, description).setRequired(required));
}
/**
* Adds an option to this subcommand.
* <br>The option is set to be non-required! You can use {@link #addOption(OptionType, String, String, boolean)} to add a required option instead.
*
* <p>Required options must be added before non-required options!
*
* @param type
* The {@link OptionData}
* @param name
* The lowercase option name, 1-32 characters
* @param description
* The option description, 1-100 characters
*
* @throws IllegalArgumentException
* <ul>
* <li>If this option is required and you already added a non-required option.</li>
* <li>If more than 25 options are provided.</li>
* <li>If null is provided</li>
* </ul>
*
* @return The SubcommandData instance, for chaining
*/
@Nonnull
public SubcommandData addOption(@Nonnull OptionType type, @Nonnull String name, @Nonnull String description)
{
return addOption(type, name, description, false);
}
@Nonnull
@Override
public DataObject toData()
{
return super.toData().put("type", OptionType.SUB_COMMAND.getKey());
}
/**
* Parses the provided serialization back into an SubcommandData instance.
* <br>This is the reverse function for {@link #toData()}.
*
* @param json
* The serialized {@link DataObject} representing the subcommand
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the serialized object is missing required fields
* @throws IllegalArgumentException
* If any of the values are failing the respective checks such as length
*
* @return The parsed SubcommandData instance, which can be further configured through setters
*/
@Nonnull
public static SubcommandData fromData(@Nonnull DataObject json)
{
String name = json.getString("name");
String description = json.getString("description");
SubcommandData sub = new SubcommandData(name, description);
json.optArray("options").ifPresent(arr ->
arr.stream(DataArray::getObject)
.map(OptionData::fromData)
.forEach(sub::addOptions)
);
return sub;
}
}