-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordTextInputTag.java
251 lines (224 loc) · 9.22 KB
/
DiscordTextInputTag.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
package com.denizenscript.ddiscordbot.objects;
import com.denizenscript.denizencore.objects.Fetchable;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
public class DiscordTextInputTag implements ObjectTag {
// <--[ObjectType]
// @name DiscordTextInputTag
// @prefix discordtextinput
// @base ElementTag
// @format
// The identity format for Discord text input is a map of relevant data. Do not alter raw text input data, use the with.as tag instead.
//
// @plugin dDiscordBot
// @description
// A DiscordTextInputTag is an object that represents a Discord text input for use with dDiscordBot.
//
// -->
@Fetchable("discordtextinput")
public static DiscordTextInputTag valueOf(String string, TagContext context) {
if (string.startsWith("discordtextinput@")) {
string = string.substring("discordtextinput@".length());
}
MapTag map = MapTag.valueOf(string, context);
if (map == null) {
return null;
}
return new DiscordTextInputTag(map);
}
@Override
public DiscordTextInputTag duplicate() {
return new DiscordTextInputTag(textInputData.duplicate());
}
public static boolean matches(String arg) {
if (arg.startsWith("discordtextinput@")) {
return true;
}
return MapTag.matches(arg);
}
public DiscordTextInputTag() {
textInputData = new MapTag();
}
public DiscordTextInputTag(MapTag map) {
textInputData = map;
}
public DiscordTextInputTag(TextInput textInput) {
textInputData = new MapTag();
textInputData.putObject("style", new ElementTag(textInput.getStyle()));
textInputData.putObject("label", new ElementTag(textInput.getLabel()));
textInputData.putObject("id", new ElementTag(textInput.getId()));
if (textInput.getMinLength() != -1) {
textInputData.putObject("min_length", new ElementTag(textInput.getMinLength()));
}
if (textInput.getMaxLength() != -1) {
textInputData.putObject("max_length", new ElementTag(textInput.getMaxLength()));
}
textInputData.putObject("is_required", new ElementTag(textInput.isRequired()));
if (textInput.getValue() != null) {
textInputData.putObject("value", new ElementTag(textInput.getValue()));
}
if (textInput.getPlaceHolder() != null) {
textInputData.putObject("placeholder", new ElementTag(textInput.getPlaceHolder()));
}
}
public TextInput build() {
ElementTag id = textInputData.getElement("id");
if (id == null) {
return null;
}
ElementTag label = textInputData.getElement("label");
ElementTag style = textInputData.getElement("style", "SHORT");
TextInputStyle textInputStyle = style.asEnum(TextInputStyle.class);
TextInput.Builder textInput = TextInput.create(id.toString(), label.toString(), textInputStyle);
ElementTag minLength = textInputData.getElement("min_length");
if (minLength != null) {
textInput.setMinLength(minLength.asInt());
}
ElementTag maxLength = textInputData.getElement("max_length");
if (maxLength != null) {
textInput.setMaxLength(maxLength.asInt());
}
ElementTag isRequired = textInputData.getElement("is_required");
if (isRequired != null) {
textInput.setRequired(isRequired.asBoolean());
}
ElementTag value = textInputData.getElement("value");
textInput.setValue(value == null ? null : value.toString());
ElementTag placeholder = textInputData.getElement("placeholder");
textInput.setPlaceholder(placeholder == null ? null : placeholder.toString());
return textInput.build();
}
public MapTag textInputData;
public static HashSet<String> acceptedWithKeys = new HashSet<>(Arrays.asList(
"style", "id", "label", "min_length", "max_length", "is_required", "value", "placeholder"
));
public static void registerTags() {
// <--[tag]
// @attribute <DiscordTextInputTag.with_map[<map>]>
// @returns DiscordTextInputTag
// @plugin dDiscordBot
// @description
// Returns a copy of this TextInput tag, with the map of keys to values applied.
// Refer to <@link tag DiscordTextInputTag.with.as>.
// -->
tagProcessor.registerTag(DiscordTextInputTag.class, "with_map", (attribute, object) -> {
DiscordTextInputTag textInput = object.duplicate();
if (!attribute.hasParam()) {
attribute.echoError("Invalid text_input.with_map[...] tag: must have an input value.");
return null;
}
MapTag map = MapTag.getMapFor(attribute.getParamObject(), attribute.context);
for (Map.Entry<StringHolder, ObjectTag> entry : map.map.entrySet()) {
String key = entry.getKey().low;
if (!acceptedWithKeys.contains(key)) {
attribute.echoError("Invalid text_input.with_map[...] tag: unknown key '" + key + "' given.");
return null;
}
ObjectTag val = entry.getValue();
if (val == null) {
attribute.echoError("text_input.with_map[...] value is invalid.");
return null;
}
textInput.textInputData.putObject(key, val);
}
return textInput;
});
// <--[tag]
// @attribute <DiscordTextInputTag.with[<key>].as[<value>]>
// @returns DiscordTextInputTag
// @plugin dDiscordBot
// @description
// Returns a copy of this TextInput tag, with the specified data key set to the specified value.
// The following keys are accepted, with values of the listed type:
// style: short or paragraph
// id: ElementTag
// label: ElementTag
// min_length: ElementTag(Number)
// max_length: ElementTag(Number)
// is_required: ElementTag(Boolean)
// value: ElementTag
// placeholder: ElementTag
// -->
tagProcessor.registerTag(DiscordTextInputTag.class, "with", (attribute, object) -> {
DiscordTextInputTag textInput = object.duplicate();
if (!attribute.hasParam()) {
attribute.echoError("Invalid text_input.with[...] tag: must have an input value.");
return null;
}
String key = CoreUtilities.toLowerCase(attribute.getParam());
if (!acceptedWithKeys.contains(key)) {
attribute.echoError("Invalid text_input.with[...] tag: unknown key '" + key + "' given.");
return null;
}
attribute.fulfill(1);
if (!attribute.startsWith("as") || !attribute.hasParam()) {
attribute.echoError("text_input.with[...] must be followed by as[...].");
}
ObjectTag val = attribute.getParamObject();
if (val == null) {
attribute.echoError("text_input.with[...].as[...] value is invalid.");
return null;
}
textInput.textInputData.putObject(key, val);
return textInput;
});
// <--[tag]
// @attribute <DiscordTextInputTag.map>
// @returns MapTag
// @plugin dDiscordBot
// @description
// Returns the MapTag internally backing this text input tag.
// -->
tagProcessor.registerTag(MapTag.class, "map", (attribute, object) -> {
return object.textInputData.duplicate();
});
}
public static ObjectTagProcessor<DiscordTextInputTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
String prefix = "discordtextinput";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debuggable() {
return "<LG>discordtextinput@<Y>" + textInputData.debuggable();
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String identify() {
return "discordtextinput@" + textInputData.identify();
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public ObjectTag setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}
}