-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordCommandTag.java
295 lines (262 loc) · 9.91 KB
/
DiscordCommandTag.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
package com.denizenscript.ddiscordbot.objects;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.DiscordConnection;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.RedirectionFlagTracker;
import com.denizenscript.denizencore.objects.ArgumentHelper;
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.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import java.util.List;
public class DiscordCommandTag implements ObjectTag, FlaggableObject {
// <--[ObjectType]
// @name DiscordCommandTag
// @prefix discordcommand
// @base ElementTag
// @implements FlaggableObject
// @format
// The identity format for Discord commands is the bot ID (optional), followed by the guild ID (optional), followed by the command ID (required).
// For example: 1234
// Or: 12,1234
// Or: mybot,12,1234
//
// @plugin dDiscordBot
// @description
// A DiscordCommandTag is an object that represents a created slash command on Discord, as a bot-specific reference.
//
// This object type is flaggable.
// Flags on this object type will be stored in: plugins/dDiscordBot/flags/bot_(botname).dat, under special sub-key "__commands"
//
// -->
@Fetchable("discordcommand")
public static DiscordCommandTag valueOf(String string, TagContext context) {
if (string.startsWith("discordcommand@")) {
string = string.substring("discordcommand@".length());
}
if (string.contains("@")) {
return null;
}
List<String> commaSplit = CoreUtilities.split(string, ',');
if (commaSplit.size() == 0 || commaSplit.size() > 3) {
if (context == null || context.showErrors()) {
Debug.echoError("DiscordCommandTag input is not valid.");
}
return null;
}
String cmdIdText = commaSplit.get(commaSplit.size() - 1);
if (!ArgumentHelper.matchesInteger(cmdIdText)) {
if (context == null || context.showErrors()) {
Debug.echoError("DiscordCommandTag input is not a number.");
}
return null;
}
long cmdId = Long.parseLong(cmdIdText);
if (cmdId == 0) {
return null;
}
if (commaSplit.size() == 1) {
return new DiscordCommandTag(null, 0, cmdId);
}
String grpIdText = commaSplit.get(commaSplit.size() - 2);
if (!ArgumentHelper.matchesInteger(grpIdText)) {
if (context == null || context.showErrors()) {
Debug.echoError("DiscordCommandTag group ID input is not a number.");
}
return null;
}
long grpId = Long.parseLong(grpIdText);
if (grpId == 0) {
return null;
}
return new DiscordCommandTag(commaSplit.size() == 3 ? commaSplit.get(0) : null, grpId, cmdId);
}
public static boolean matches(String arg) {
if (arg.startsWith("discordcommand@")) {
return true;
}
if (arg.contains("@")) {
return false;
}
int comma = arg.lastIndexOf(',');
if (comma == -1) {
return ArgumentHelper.matchesInteger(arg);
}
if (comma == arg.length() - 1) {
return false;
}
return ArgumentHelper.matchesInteger(arg.substring(comma + 1));
}
public DiscordCommandTag(String bot, long guild_id, long command_id) {
this.bot = bot;
this.guild_id = guild_id;
this.command_id = command_id;
}
public DiscordCommandTag(String bot, Guild guild, Command command) {
this.bot = bot;
this.guild = guild;
this.guild_id = guild != null ? this.guild.getIdLong() : 0;
this.command = command;
this.command_id = this.command.getIdLong();
}
public DiscordConnection getBot() {
return DenizenDiscordBot.instance.connections.get(bot);
}
public Guild getGuild() {
if (guild != null) {
return guild;
}
if (bot == null) {
return null;
}
if (guild_id == 0) {
return null;
}
guild = getBot().client.getGuildById(guild_id);
return guild;
}
public Command getCommand() {
if (command != null) {
return command;
}
if (bot == null) {
return null;
}
if (getGuild() != null) {
command = getGuild().retrieveCommandById(command_id).complete();
}
else {
command = getBot().client.retrieveCommandById(command_id).complete();
}
return command;
}
public String bot;
public Guild guild;
public Command command;
public long guild_id;
public long command_id;
@Override
public AbstractFlagTracker getFlagTracker() {
return new RedirectionFlagTracker(getBot().flags, "__commands." + command_id);
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}
public static void register() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
// <--[tag]
// @attribute <DiscordCommandTag.id>
// @returns ElementTag(Number)
// @plugin dDiscordBot
// @description
// Returns the ID of the command.
// -->
tagProcessor.registerTag(ElementTag.class, "id", (attribute, object) -> {
return new ElementTag(object.command_id);
});
// <--[tag]
// @attribute <DiscordCommandTag.name>
// @returns ElementTag
// @plugin dDiscordBot
// @description
// Returns the name of the command.
// -->
tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> {
return new ElementTag(object.getCommand().getName());
});
// <--[tag]
// @attribute <DiscordCommandTag.description>
// @returns ElementTag
// @plugin dDiscordBot
// @description
// Returns the description of the command.
// -->
tagProcessor.registerTag(ElementTag.class, "description", (attribute, object) -> {
return new ElementTag(object.getCommand().getDescription());
});
// <--[tag]
// @attribute <DiscordCommandTag.options>
// @returns ListTag(MapTag)
// @plugin dDiscordBot
// @description
// Returns the option MapTags of the command. This is the same value as the one provided when creating a command, as documented in <@link command DiscordCommand>.
// -->
tagProcessor.registerTag(ListTag.class, "options", (attribute, object) -> {
ListTag options = new ListTag();
for (Command.Option option : object.getCommand().getOptions()) {
MapTag map = new MapTag();
map.putObject("type", new ElementTag(option.getType().toString().toLowerCase().replaceAll("_", "")));
map.putObject("name", new ElementTag(option.getName()));
map.putObject("description", new ElementTag(option.getDescription()));
map.putObject("required", new ElementTag(option.isRequired()));
if (option.getType() == OptionType.STRING || option.getType() == OptionType.INTEGER) {
ListTag choices = new ListTag();
for (Command.Choice choice : option.getChoices()) {
MapTag choiceData = new MapTag();
choiceData.putObject("name", new ElementTag(choice.getName()));
if (option.getType() == OptionType.STRING) {
choiceData.putObject("value", new ElementTag(choice.getAsString()));
}
else {
choiceData.putObject("value", new ElementTag(choice.getAsLong()));
}
choices.addObject(choiceData);
}
map.putObject("choices", choices);
}
options.addObject(map);
}
return options;
});
}
public static ObjectTagProcessor<DiscordCommandTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
String prefix = "discordcommand";
@Override
public String getPrefix() {
return prefix;
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String identify() {
if (bot != null) {
return "discordcommand@" + bot + "," + guild_id + "," + command_id;
}
if (guild_id != 0) {
return "discordcommand@" + guild_id + "," + command_id;
}
return "discordcommand@" + command_id;
}
@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;
}
}