-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordSelectionTag.java
286 lines (256 loc) · 10.1 KB
/
DiscordSelectionTag.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
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.ListTag;
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.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import net.dv8tion.jda.api.entities.Emoji;
import net.dv8tion.jda.api.interactions.components.selections.SelectOption;
import net.dv8tion.jda.api.interactions.components.selections.SelectionMenu;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public class DiscordSelectionTag implements ObjectTag {
// <--[ObjectType]
// @name DiscordSelectionTag
// @prefix discordselection
// @base ElementTag
// @format
// The identity format for Discord selection menu is a map of menu data. Do not alter raw menu data, use the with.as tag instead.
//
// @plugin dDiscordBot
// @description
// A DiscordSelectionTag is an object that represents a Discord selection menu for use with dDiscordBot.
//
// -->
@Fetchable("discordselection")
public static DiscordSelectionTag valueOf(String string, TagContext context) {
if (string.startsWith("discordselection@")) {
string = string.substring("discordselection@".length());
}
MapTag map = MapTag.valueOf(string, context);
if (map == null) {
return null;
}
return new DiscordSelectionTag(map);
}
@Override
public DiscordSelectionTag duplicate() {
return new DiscordSelectionTag(menuData.duplicate());
}
public static boolean matches(String arg) {
if (arg.startsWith("discordselection@")) {
return true;
}
return MapTag.matches(arg);
}
public DiscordSelectionTag() {
menuData = new MapTag();
}
public DiscordSelectionTag(MapTag map) {
menuData = map;
}
public static MapTag getSelectionOption(SelectOption selectOption) {
MapTag options = new MapTag();
options.putObject("label", new ElementTag(selectOption.getLabel()));
options.putObject("value", new ElementTag(selectOption.getValue()));
if (selectOption.getDescription() != null) {
options.putObject("description", new ElementTag(selectOption.getDescription()));
}
if (selectOption.getEmoji() != null) {
options.putObject("emoji", new ElementTag(selectOption.getEmoji().getName()));
}
return options;
}
public DiscordSelectionTag(SelectionMenu menu) {
menuData = new MapTag();
if (menu.getId() != null) {
menuData.putObject("id", new ElementTag(menu.getId()));
}
ListTag options = new ListTag();
if (menu.getOptions() != null) {
for (SelectOption option : menu.getOptions()) {
options.addObject(DiscordSelectionTag.getSelectionOption(option));
}
}
menuData.putObject("options", options);
}
public SelectionMenu.Builder build(TagContext context) {
ObjectTag id = menuData.getObject("id");
MapTag options = (MapTag) menuData.getObject("options");
SelectionMenu.Builder menu = null;
if (id != null) {
menu = SelectionMenu.create(id.toString());
}
else {
return null;
}
if (options != null) {
for (ObjectTag optionObj : options.map.values()) {
MapTag option = optionObj.asType(MapTag.class, context);
ElementTag label = (ElementTag) option.getObject("label");
ElementTag value = (ElementTag) option.getObject("value");
ElementTag description = (ElementTag) option.getObject("description");
ElementTag emoji = (ElementTag) option.getObject("emoji");
Emoji emojiData = null;
if (emoji != null) {
emojiData = Emoji.fromUnicode(emoji.toString());
}
if (label == null || value == null) {
return null;
}
if (description == null && emoji == null) {
menu.addOption(label.toString(), value.toString());
}
else if (emoji == null) {
menu.addOption(label.toString(), value.toString(), description.toString());
}
else if (description == null) {
if (emojiData != null) {
menu.addOption(label.toString(), value.toString(), emojiData);
}
}
else {
if (emojiData != null) {
menu.addOption(label.toString(), value.toString(), description.toString(), emojiData);
}
}
}
}
return menu;
}
public MapTag menuData;
public static HashSet<String> acceptedWithKeys = new HashSet<>(Arrays.asList(
"id", "options"
));
public static void registerTags() {
// <--[tag]
// @attribute <DiscordSelectionTag.with_map[<map>]>
// @returns DiscordSelectionTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Selection tag, with the map of keys to values applied.
// Refer to <@link tag DiscordSelectionTag.with.as>.
// -->
registerTag("with_map", (attribute, object) -> {
DiscordSelectionTag menu = object.duplicate();
if (!attribute.hasContext(1)) {
attribute.echoError("Invalid selection.with_map[...] tag: must have an input value.");
return null;
}
MapTag map = MapTag.getMapFor(attribute.getContextObject(1), attribute.context);
for (Map.Entry<StringHolder, ObjectTag> entry : map.map.entrySet()) {
String key = entry.getKey().low;
if (!acceptedWithKeys.contains(key)) {
attribute.echoError("Invalid selection.with_map[...] tag: unknown key '" + key + "' given.");
return null;
}
ObjectTag val = entry.getValue();
if (val == null) {
attribute.echoError("selection.with_map[...] value is invalid.");
return null;
}
menu.menuData.putObject(key, val);
}
return menu;
});
// <--[tag]
// @attribute <DiscordSelectionTag.with[<key>].as[<value>]>
// @returns DiscordSelectionTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Selection tag, with the specified data key set to the specified value.
// The following keys are accepted, with values of the listed type:
// id: ElementTag
// options: MapTag where the values are also a MapTag consisting of:
// - label: ElementTag
// - value: ElementTag
// - description: ElementTag
// - emoji: ElementTag
// -->
registerTag("with", (attribute, object) -> {
DiscordSelectionTag menu = object.duplicate();
if (!attribute.hasContext(1)) {
attribute.echoError("Invalid selection.with[...] tag: must have an input value.");
return null;
}
String key = CoreUtilities.toLowerCase(attribute.getContext(1));
if (!acceptedWithKeys.contains(key)) {
attribute.echoError("Invalid selection.with[...] tag: unknown key '" + key + "' given.");
return null;
}
attribute.fulfill(1);
if (!attribute.startsWith("as") || !attribute.hasContext(1)) {
attribute.echoError("selection.with[...] must be followed by as[...].");
}
ObjectTag val = attribute.getContextObject(1);
if (val == null) {
attribute.echoError("selection.with[...].as[...] value is invalid.");
return null;
}
menu.menuData.putObject(key, val);
return menu;
});
// <--[tag]
// @attribute <DiscordSelectionTag.map>
// @returns MapTag
// @plugin dDiscordBot
// @description
// Returns the MapTag internally backing this selection tag.
// -->
registerTag("map", (attribute, object) -> {
return object.menuData.duplicate();
});
}
public static ObjectTagProcessor<DiscordSelectionTag> tagProcessor = new ObjectTagProcessor<>();
public static void registerTag(String name, TagRunnable.ObjectInterface<DiscordSelectionTag> runnable, String... variants) {
tagProcessor.registerTag(name, runnable, variants);
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
String prefix = "discordselection";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debuggable() {
return "discordselection@" + menuData.debuggable();
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "DiscordSelection";
}
@Override
public String identify() {
return "discordselection@" + menuData.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;
}
}