forked from discord-jda/JDA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandDataTest.java
More file actions
205 lines (172 loc) · 10.4 KB
/
CommandDataTest.java
File metadata and controls
205 lines (172 loc) · 10.4 KB
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
/*
* 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.
*/
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
public class CommandDataTest
{
@Test
public void testNormal()
{
CommandData command = new CommandDataImpl("ban", "Ban a user from this server")
.setGuildOnly(true)
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.BAN_MEMBERS))
.addOption(OptionType.USER, "user", "The user to ban", true) // required before non-required
.addOption(OptionType.STRING, "reason", "The ban reason") // test that default is false
.addOption(OptionType.INTEGER, "days", "The duration of the ban", false); // test with explicit false
DataObject data = command.toData();
Assertions.assertEquals("ban", data.getString("name"));
Assertions.assertEquals("Ban a user from this server", data.getString("description"));
Assertions.assertFalse(data.getBoolean("dm_permission"));
Assertions.assertEquals(Permission.BAN_MEMBERS.getRawValue(), data.getUnsignedLong("default_member_permissions"));
DataArray options = data.getArray("options");
DataObject option = options.getObject(0);
Assertions.assertTrue(option.getBoolean("required"));
Assertions.assertEquals("user", option.getString("name"));
Assertions.assertEquals("The user to ban", option.getString("description"));
option = options.getObject(1);
Assertions.assertFalse(option.getBoolean("required"));
Assertions.assertEquals("reason", option.getString("name"));
Assertions.assertEquals("The ban reason", option.getString("description"));
option = options.getObject(2);
Assertions.assertFalse(option.getBoolean("required"));
Assertions.assertEquals("days", option.getString("name"));
Assertions.assertEquals("The duration of the ban", option.getString("description"));
}
@Test
public void testDefaultMemberPermissions()
{
CommandData command = new CommandDataImpl("ban", "Ban a user from this server")
.setDefaultPermissions(DefaultMemberPermissions.DISABLED);
DataObject data = command.toData();
Assertions.assertEquals(0, data.getUnsignedLong("default_member_permissions"));
command.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
data = command.toData();
Assertions.assertTrue(data.isNull("default_member_permissions"));
}
@Test
public void testSubcommand()
{
CommandDataImpl command = new CommandDataImpl("mod", "Moderation commands")
.addSubcommands(new SubcommandData("ban", "Ban a user from this server")
.addOption(OptionType.USER, "user", "The user to ban", true) // required before non-required
.addOption(OptionType.STRING, "reason", "The ban reason") // test that default is false
.addOption(OptionType.INTEGER, "days", "The duration of the ban", false)); // test with explicit false
DataObject data = command.toData();
Assertions.assertEquals("mod", data.getString("name"));
Assertions.assertEquals("Moderation commands", data.getString("description"));
DataObject subdata = data.getArray("options").getObject(0);
Assertions.assertEquals("ban", subdata.getString("name"));
Assertions.assertEquals("Ban a user from this server", subdata.getString("description"));
DataArray options = subdata.getArray("options");
DataObject option = options.getObject(0);
Assertions.assertTrue(option.getBoolean("required"));
Assertions.assertEquals("user", option.getString("name"));
Assertions.assertEquals("The user to ban", option.getString("description"));
option = options.getObject(1);
Assertions.assertFalse(option.getBoolean("required"));
Assertions.assertEquals("reason", option.getString("name"));
Assertions.assertEquals("The ban reason", option.getString("description"));
option = options.getObject(2);
Assertions.assertFalse(option.getBoolean("required"));
Assertions.assertEquals("days", option.getString("name"));
Assertions.assertEquals("The duration of the ban", option.getString("description"));
}
@Test
public void testSubcommandGroup()
{
CommandDataImpl command = new CommandDataImpl("mod", "Moderation commands")
.addSubcommandGroups(new SubcommandGroupData("ban", "Ban or unban a user from this server")
.addSubcommands(new SubcommandData("add", "Ban a user from this server")
.addOption(OptionType.USER, "user", "The user to ban", true) // required before non-required
.addOption(OptionType.STRING, "reason", "The ban reason") // test that default is false
.addOption(OptionType.INTEGER, "days", "The duration of the ban", false))); // test with explicit false
DataObject data = command.toData();
Assertions.assertEquals("mod", data.getString("name"));
Assertions.assertEquals("Moderation commands", data.getString("description"));
DataObject group = data.getArray("options").getObject(0);
Assertions.assertEquals("ban", group.getString("name"));
Assertions.assertEquals("Ban or unban a user from this server", group.getString("description"));
DataObject subdata = group.getArray("options").getObject(0);
Assertions.assertEquals("add", subdata.getString("name"));
Assertions.assertEquals("Ban a user from this server", subdata.getString("description"));
DataArray options = subdata.getArray("options");
DataObject option = options.getObject(0);
Assertions.assertTrue(option.getBoolean("required"));
Assertions.assertEquals("user", option.getString("name"));
Assertions.assertEquals("The user to ban", option.getString("description"));
option = options.getObject(1);
Assertions.assertFalse(option.getBoolean("required"));
Assertions.assertEquals("reason", option.getString("name"));
Assertions.assertEquals("The ban reason", option.getString("description"));
option = options.getObject(2);
Assertions.assertFalse(option.getBoolean("required"));
Assertions.assertEquals("days", option.getString("name"));
Assertions.assertEquals("The duration of the ban", option.getString("description"));
}
@Test
public void testRequiredThrows()
{
CommandDataImpl command = new CommandDataImpl("ban", "Simple ban command");
command.addOption(OptionType.STRING, "opt", "desc");
Assertions.assertThrows(IllegalArgumentException.class, () -> command.addOption(OptionType.STRING, "other", "desc", true));
SubcommandData subcommand = new SubcommandData("sub", "Simple subcommand");
subcommand.addOption(OptionType.STRING, "opt", "desc");
Assertions.assertThrows(IllegalArgumentException.class, () -> subcommand.addOption(OptionType.STRING, "other", "desc", true));
}
@Test
public void testNameChecks()
{
Assertions.assertThrows(IllegalArgumentException.class, () -> new CommandDataImpl("invalid name", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> new CommandDataImpl("invalidName", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> new CommandDataImpl("valid_name", ""));
Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandData("invalid name", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandData("invalidName", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandData("valid_name", ""));
Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandGroupData("invalid name", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandGroupData("invalidName", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> new SubcommandGroupData("valid_name", ""));
}
@Test
public void testChoices()
{
OptionData option = new OptionData(OptionType.INTEGER, "choice", "Option with choices!");
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("invalid name", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("invalidName", "Valid description"));
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("valid_name", ""));
List<Command.Choice> choices = new ArrayList<>();
for (int i = 0; i < 25; i++)
{
option.addChoice("choice_" + i, i);
choices.add(new Command.Choice("choice_" + i, i));
}
Assertions.assertThrows(IllegalArgumentException.class, () -> option.addChoice("name", 100));
Assertions.assertEquals(25, option.getChoices().size());
Assertions.assertEquals(choices, option.getChoices());
}
}