-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordEmbedTag.java
439 lines (408 loc) · 17.5 KB
/
DiscordEmbedTag.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package com.denizenscript.ddiscordbot.objects;
import com.denizenscript.denizen.objects.ColorTag;
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.objects.core.TimeTag;
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.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.bukkit.Color;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
public class DiscordEmbedTag implements ObjectTag {
// <--[ObjectType]
// @name DiscordEmbedTag
// @prefix discordembed
// @base ElementTag
// @format
// The identity format for Discord embeds is a map of embed data. Do not alter raw embed data, use the with.as tag instead.
//
// @plugin dDiscordBot
// @description
// A DiscordEmbedTag is an object that represents a Discord embed for use with dDiscordBot.
//
// -->
@Fetchable("discordembed")
public static DiscordEmbedTag valueOf(String string, TagContext context) {
if (string.startsWith("discordembed@")) {
string = string.substring("discordembed@".length());
}
MapTag map = MapTag.valueOf(string, context);
if (map == null) {
return null;
}
return new DiscordEmbedTag(map);
}
@Override
public DiscordEmbedTag duplicate() {
return new DiscordEmbedTag(embedData.duplicate());
}
public static boolean matches(String arg) {
if (arg.startsWith("discordembed@")) {
return true;
}
return MapTag.matches(arg);
}
public DiscordEmbedTag() {
embedData = new MapTag();
}
public DiscordEmbedTag(MapTag map) {
embedData = map;
}
public DiscordEmbedTag(MessageEmbed embed) {
embedData = new MapTag();
if (embed.getAuthor() != null && embed.getAuthor().getName() != null) {
embedData.putObject("author_name", new ElementTag(embed.getAuthor().getName()));
}
if (embed.getAuthor() != null && embed.getAuthor().getUrl() != null) {
embedData.putObject("author_url", new ElementTag(embed.getAuthor().getUrl()));
}
if (embed.getAuthor() != null && embed.getAuthor().getIconUrl() != null) {
embedData.putObject("author_icon_url", new ElementTag(embed.getAuthor().getIconUrl()));
}
if (embed.getColor() != null) {
embedData.putObject("author_icon_url", new ColorTag(Color.fromRGB(embed.getColorRaw())));
}
if (embed.getDescription() != null) {
embedData.putObject("getDescription", new ElementTag(embed.getDescription()));
}
if (embed.getFooter() != null && embed.getFooter().getText() != null) {
embedData.putObject("footer", new ElementTag(embed.getFooter().getText()));
}
if (embed.getFooter() != null && embed.getFooter().getIconUrl() != null) {
embedData.putObject("footer_icon", new ElementTag(embed.getFooter().getIconUrl()));
}
if (embed.getImage() != null && embed.getImage().getUrl() != null) {
embedData.putObject("image", new ElementTag(embed.getImage().getUrl()));
}
if (embed.getThumbnail() != null && embed.getThumbnail().getUrl() != null) {
embedData.putObject("thumbnail", new ElementTag(embed.getThumbnail().getUrl()));
}
if (embed.getTimestamp() != null) {
embedData.putObject("timestamp", new TimeTag(embed.getTimestamp().toZonedDateTime()));
}
if (embed.getTitle() != null) {
embedData.putObject("title", new ElementTag(embed.getTitle()));
}
if (embed.getUrl() != null) {
embedData.putObject("title_url", new ElementTag(embed.getUrl()));
}
if (!embed.getFields().isEmpty()) {
ListTag fields = new ListTag();
for (MessageEmbed.Field field : embed.getFields()) {
MapTag fieldMap = new MapTag();
if (field.getName() != null) {
fieldMap.putObject("title", new ElementTag(field.getName()));
}
if (field.getValue() != null) {
fieldMap.putObject("title", new ElementTag(field.getValue()));
}
fieldMap.putObject("inline", new ElementTag(field.isInline()));
fields.addObject(fieldMap);
}
embedData.putObject("fields", fields);
}
}
public EmbedBuilder build(TagContext context) {
EmbedBuilder builder = new EmbedBuilder();
ObjectTag author_name = embedData.getObject("author_name");
ObjectTag author_url = embedData.getObject("author_url");
ObjectTag author_icon_url = embedData.getObject("author_icon_url");
ObjectTag color = embedData.getObject("color");
ObjectTag description = embedData.getObject("description");
ObjectTag footer = embedData.getObject("footer");
ObjectTag footer_icon = embedData.getObject("footer_icon");
ObjectTag image = embedData.getObject("image");
ObjectTag thumbnail = embedData.getObject("thumbnail");
ObjectTag timestamp = embedData.getObject("timestamp");
ObjectTag title = embedData.getObject("title");
ObjectTag title_url = embedData.getObject("title_url");
ObjectTag fields = embedData.getObject("fields");
if (author_name != null) {
if (author_url != null) {
if (author_icon_url != null) {
builder.setAuthor(author_name.toString(), author_url.toString(), author_icon_url.toString());
}
else {
builder.setAuthor(author_name.toString(), author_url.toString());
}
}
else {
if (author_icon_url != null) {
builder.setAuthor(author_name.toString(), null, author_icon_url.toString());
}
else {
builder.setAuthor(author_name.toString());
}
}
}
if (color != null) {
builder.setColor(ColorTag.valueOf(color.toString(), context).getColor().asRGB());
}
if (description != null) {
builder.setDescription(description.toString());
}
if (footer != null) {
if (footer_icon != null) {
builder.setFooter(footer.toString(), footer_icon.toString());
}
else {
builder.setFooter(footer.toString());
}
}
if (image != null) {
builder.setImage(image.toString());
}
if (thumbnail != null) {
builder.setThumbnail(thumbnail.toString());
}
if (timestamp != null) {
builder.setTimestamp(TimeTag.valueOf(timestamp.toString(), context).instant);
}
if (title != null) {
if (title_url != null) {
builder.setTitle(title.toString(), title_url.toString());
}
else {
builder.setTitle(title.toString());
}
}
if (fields != null) {
ListTag fieldList = ListTag.getListFor(fields, context);
for (ObjectTag field : fieldList.objectForms) {
MapTag fieldMap = MapTag.getMapFor(field, context);
if (fieldMap != null) {
ObjectTag fieldTitle = fieldMap.getObject("title");
ObjectTag fieldValue = fieldMap.getObject("value");
ObjectTag fieldInline = fieldMap.getObject("inline");
builder.addField(fieldTitle.toString(), fieldValue.toString(), fieldInline != null && fieldInline.toString().equals("true"));
}
}
}
return builder;
}
public MapTag embedData;
public static HashSet<String> acceptedWithKeys = new HashSet<>(Arrays.asList(
"author_name", "author_url", "author_icon_url", "color", "description",
"footer", "footer_icon", "image", "thumbnail", "timestamp", "title", "title_url", "fields"
));
public static void registerTags() {
// <--[tag]
// @attribute <DiscordEmbedTag.with_map[<map>]>
// @returns DiscordEmbedTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Embed tag, with the map of keys to values applied.
// Refer to <@link tag DiscordEmbedTag.with.as>.
// -->
registerTag("with_map", (attribute, object) -> {
DiscordEmbedTag embed = object.duplicate();
if (!attribute.hasContext(1)) {
attribute.echoError("Invalid embed.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 embed.with_map[...] tag: unknown key '" + key + "' given.");
return null;
}
ObjectTag val = entry.getValue();
if (key.equals("timestamp")) {
val = val.asType(TimeTag.class, attribute.context);
}
else if (key.equals("color")) {
val = val.asType(ColorTag.class, attribute.context);
}
if (val == null) {
attribute.echoError("embed.with_map[...] value is invalid.");
return null;
}
embed.embedData.putObject(key, val);
}
return embed;
});
// <--[tag]
// @attribute <DiscordEmbedTag.with[<key>].as[<value>]>
// @returns DiscordEmbedTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Embed tag, with the specified data key set to the specified value.
// The following keys are accepted, with values of the listed type:
// author_name: ElementTag
// author_url: ElementTag of a URL (requires author_name set)
// author_icon_url: ElementTag of a URL (requires author_name set)
// color: ColorTag
// description: ElementTag
// footer: ElementTag
// footer_icon: ElementTag of a URL (requires footer set)
// image: ElementTag of a URL
// thumbnail: ElementTag of a URL
// timestamp: TimeTag
// title: ElementTag
// title_url: ElementTag of a URL (requires title set)
// -->
registerTag("with", (attribute, object) -> {
DiscordEmbedTag embed = object.duplicate();
if (!attribute.hasContext(1)) {
attribute.echoError("Invalid embed.with[...] tag: must have an input value.");
return null;
}
String key = CoreUtilities.toLowerCase(attribute.getContext(1));
if (!acceptedWithKeys.contains(key)) {
attribute.echoError("Invalid embed.with[...] tag: unknown key '" + key + "' given.");
return null;
}
attribute.fulfill(1);
if (!attribute.startsWith("as") || !attribute.hasContext(1)) {
attribute.echoError("embed.with[...] must be followed by as[...].");
}
ObjectTag val = attribute.getContextObject(1);
if (key.equals("timestamp")) {
val = val.asType(TimeTag.class, attribute.context);
}
else if (key.equals("color")) {
val = val.asType(ColorTag.class, attribute.context);
}
if (val == null) {
attribute.echoError("embed.with[...].as[...] value is invalid.");
return null;
}
embed.embedData.putObject(key, val);
return embed;
});
// <--[tag]
// @attribute <DiscordEmbedTag.map>
// @returns MapTag
// @plugin dDiscordBot
// @description
// Returns the MapTag internally backing this embed tag.
// -->
registerTag("map", (attribute, object) -> {
return object.embedData.duplicate();
});
// <--[tag]
// @attribute <DiscordEmbedTag.add_field[<title>].value[<value>]>
// @returns DiscordEmbedTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Embed tag, with a field added with the given title and value.
// -->
registerTag("add_field", (attribute, object) -> {
DiscordEmbedTag embed = object.duplicate();
if (!attribute.hasContext(1)) {
attribute.echoError("Invalid embed.add_field[...] tag: must have an input title.");
return null;
}
String title = attribute.getContext(1);
attribute.fulfill(1);
if (!attribute.startsWith("value") || !attribute.hasContext(1)) {
attribute.echoError("embed.add_field[...] must be followed by value[...].");
}
String value = attribute.getContext(1);
MapTag fieldMap = new MapTag();
fieldMap.putObject("title", new ElementTag(title));
fieldMap.putObject("value", new ElementTag(value));
ObjectTag fieldsData = embed.embedData.getObject("fields");
ListTag fieldList;
if (fieldsData == null) {
fieldList = new ListTag();
embed.embedData.putObject("fields", fieldList);
}
else {
fieldList = ListTag.getListFor(fieldsData, attribute.context);
}
fieldList.addObject(fieldMap);
return embed;
});
// <--[tag]
// @attribute <DiscordEmbedTag.add_inline_field[<title>].value[<value>]>
// @returns DiscordEmbedTag
// @plugin dDiscordBot
// @description
// Returns a copy of this Embed tag, with an inline field added with the given title and value.
// -->
registerTag("add_inline_field", (attribute, object) -> {
DiscordEmbedTag embed = object.duplicate();
if (!attribute.hasContext(1)) {
attribute.echoError("Invalid embed.add_inline_field[...] tag: must have an input title.");
return null;
}
String title = attribute.getContext(1);
attribute.fulfill(1);
if (!attribute.startsWith("value") || !attribute.hasContext(1)) {
attribute.echoError("embed.add_inline_field[...] must be followed by value[...].");
}
String value = attribute.getContext(1);
MapTag fieldMap = new MapTag();
fieldMap.putObject("title", new ElementTag(title));
fieldMap.putObject("value", new ElementTag(value));
fieldMap.putObject("inline", new ElementTag("true"));
ObjectTag fieldsData = embed.embedData.getObject("fields");
ListTag fieldList;
if (fieldsData == null) {
fieldList = new ListTag();
embed.embedData.putObject("fields", fieldList);
}
else {
fieldList = ListTag.getListFor(fieldsData, attribute.context);
}
fieldList.addObject(fieldMap);
return embed;
});
}
public static ObjectTagProcessor<DiscordEmbedTag> tagProcessor = new ObjectTagProcessor<>();
public static void registerTag(String name, TagRunnable.ObjectInterface<DiscordEmbedTag> runnable, String... variants) {
tagProcessor.registerTag(name, runnable, variants);
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
String prefix = "discordembed";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debuggable() {
return "discordembed@" + embedData.debuggable();
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "DiscordEmbed";
}
@Override
public String identify() {
return "discordembed@" + embedData.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;
}
}