-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordButtonTag.java
256 lines (228 loc) · 8.57 KB
/
DiscordButtonTag.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
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.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.Button;
import net.dv8tion.jda.api.interactions.components.ButtonStyle;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
public class DiscordButtonTag implements ObjectTag {
// <--[ObjectType]
// @name DiscordButtonTag
// @prefix discordbutton
// @base ElementTag
// @format
// The identity format for Discord button is a map of button data. Do not alter raw button data, use the with.as tag instead.
//
// @plugin dDiscordBot
// @description
// A DiscordButtonTag is an object that represents a Discord button for use with dDiscordBot.
//
// -->
@Fetchable("discordbutton")
public static DiscordButtonTag valueOf(String string, TagContext context) {
if (string.startsWith("discordbutton@")) {
string = string.substring("discordbutton@".length());
}
MapTag map = MapTag.valueOf(string, context);
if (map == null) {
return null;
}
return new DiscordButtonTag(map);
}
@Override
public DiscordButtonTag duplicate() {
return new DiscordButtonTag(buttonData.duplicate());
}
public static boolean matches(String arg) {
if (arg.startsWith("discordbutton@")) {
return true;
}
return MapTag.matches(arg);
}
public DiscordButtonTag() {
buttonData = new MapTag();
}
public DiscordButtonTag(MapTag map) {
buttonData = map;
}
public DiscordButtonTag(Button button) {
buttonData = new MapTag();
buttonData.putObject("style", new ElementTag(button.getStyle().name()));
buttonData.putObject("label", new ElementTag(button.getLabel()));
if (button.getId() != null) {
buttonData.putObject("id", new ElementTag(button.getId()));
}
if (button.getEmoji() != null) {
buttonData.putObject("emoji", new ElementTag(button.getEmoji().getName()));
}
}
public Button build() {
ObjectTag style = buttonData.getObject("style");
ObjectTag id = buttonData.getObject("id");
ObjectTag label = buttonData.getObject("label");
ObjectTag emoji = buttonData.getObject("emoji");
ButtonStyle styleData;
Button button = null;
boolean isValidType = false;
for (ButtonStyle val : ButtonStyle.values()) {
if (val.name().toUpperCase().equals(
style.toString().toUpperCase()
)) {
isValidType = true;
break;
}
}
if (!isValidType) {
return null;
}
styleData = ButtonStyle.valueOf(style.toString().toUpperCase());
if (style != null) {
styleData = ButtonStyle.valueOf(style.toString().toUpperCase());
}
if (id != null) {
if (emoji != null) {
Emoji emojiData = Emoji.fromUnicode(emoji.toString());
if (emojiData != null) {
button = Button.of(styleData, id.toString(), emojiData);
}
} else {
if (label != null) {
button = Button.of(styleData, id.toString(), label.toString());
}
}
}
return button;
}
public MapTag buttonData;
public static HashSet<String> acceptedWithKeys = new HashSet<>(Arrays.asList(
"style", "id", "label", "emoji"
));
public static void registerTags() {
// <--[tag]
// @attribute <DiscordButtonTag.with_map[<map>]>
// @returns DiscordButtonTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Button tag, with the map of keys to values applied.
// Refer to <@link tag DiscordButtonTag.with.as>.
// -->
tagProcessor.registerTag(DiscordButtonTag.class, "with_map", (attribute, object) -> {
DiscordButtonTag button = object.duplicate();
if (!attribute.hasParam()) {
attribute.echoError("Invalid button.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 button.with_map[...] tag: unknown key '" + key + "' given.");
return null;
}
ObjectTag val = entry.getValue();
if (val == null) {
attribute.echoError("button.with_map[...] value is invalid.");
return null;
}
button.buttonData.putObject(key, val);
}
return button;
});
// <--[tag]
// @attribute <DiscordButtonTag.with[<key>].as[<value>]>
// @returns DiscordButtonTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Button tag, with the specified data key set to the specified value.
// The following keys are accepted, with values of the listed type:
// style: ElementTag of either primary, secondary, success, danger, or link
// id: ElementTag, can be a URL
// label: ElementTag
// emoji: ElementTag
// -->
tagProcessor.registerTag(DiscordButtonTag.class, "with", (attribute, object) -> {
DiscordButtonTag button = object.duplicate();
if (!attribute.hasParam()) {
attribute.echoError("Invalid button.with[...] tag: must have an input value.");
return null;
}
String key = CoreUtilities.toLowerCase(attribute.getParam());
if (!acceptedWithKeys.contains(key)) {
attribute.echoError("Invalid button.with[...] tag: unknown key '" + key + "' given.");
return null;
}
attribute.fulfill(1);
if (!attribute.startsWith("as") || !attribute.hasParam()) {
attribute.echoError("button.with[...] must be followed by as[...].");
}
ObjectTag val = attribute.getParamObject();
if (val == null) {
attribute.echoError("button.with[...].as[...] value is invalid.");
return null;
}
button.buttonData.putObject(key, val);
return button;
});
// <--[tag]
// @attribute <DiscordButtonTag.map>
// @returns MapTag
// @plugin dDiscordBot
// @description
// Returns the MapTag internally backing this button tag.
// -->
tagProcessor.registerTag(MapTag.class, "map", (attribute, object) -> {
return object.buttonData.duplicate();
});
}
public static ObjectTagProcessor<DiscordButtonTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
String prefix = "discordbutton";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debuggable() {
return "discordbutton@" + buttonData.debuggable();
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "DiscordButton";
}
@Override
public String identify() {
return "discordbutton@" + buttonData.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;
}
}