-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordCommand.java
501 lines (492 loc) · 25.5 KB
/
DiscordCommand.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.DiscordConnection;
import com.denizenscript.ddiscordbot.objects.*;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.*;
import org.bukkit.Bukkit;
import java.io.File;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
public class DiscordCommand extends AbstractDiscordCommand implements Holdable {
public DiscordCommand() {
setName("discord");
setSyntax("discord [id:<id>] [disconnect/add_role/start_typing/remove_role/status (status:<status>) (activity:<activity>)/rename/edit_message] (<value>) (message_id:<id>) (channel:<channel>) (user:<user>) (group:<group>) (role:<role>) (url:<url>)");
setRequiredArguments(2, 12);
isProcedural = false;
}
// <--[command]
// @Name discord
// @Syntax discord [id:<id>] [disconnect/add_role/start_typing/remove_role/status (status:<status>) (activity:<activity>)/rename/edit_message] (<value>) (message_id:<id>) (channel:<channel>) (user:<user>) (group:<group>) (role:<role>) (url:<url>)
// @Required 2
// @Maximum 12
// @Short Interacts with Discord.
// @Plugin dDiscordBot
// @Guide https://guide.denizenscript.com/guides/expanding/ddiscordbot.html
// @Group external
//
// @Description
// Interacts with Discord.
//
// Commands may fail if the bot does not have permission within the Discord group to perform them.
//
// When setting the status of the Discord bot, the status argument can be: ONLINE, DND, IDLE, or INVISIBLE,
// and the activity argument can be: PLAYING, STREAMING, LISTENING, or WATCHING.
// Streaming activity requires a 'url:' input.
//
// The command should always be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <discord[<bot_id>]>
//
// @Usage
// Use to disconnect from Discord.
// - ~discord id:mybot disconnect
//
// @Usage
// Use to add a role on a user in a Discord guild.
// - ~discord id:mybot add_role user:<[user]> role:<[role]> group:<[group]>
//
// @Usage
// Use to remove a role on a user in a Discord guild.
// - ~discord id:mybot remove_role user:<[user]> role:<[role]> group:<[group]>
//
// @Usage
// Use to set the online status of the bot, and clear the game status.
// - ~discord id:mybot status "Minecraft" "status:ONLINE"
//
// @Usage
// Use to set the game status of the bot.
// - ~discord id:mybot status "Minecraft" "status:ONLINE" "activity:PLAYING"
//
// @Usage
// Use to change the bot's nickname.
// - ~discord id:mybot rename "<[nickname]>" group:<[group]>
//
// @Usage
// Use to give a user a new nickname.
// - ~discord id:mybot rename "<[nickname]>" user:<[user]> group:<[group]>
//
// @Usage
// Use to start typing in a specific channel.
// - ~discord id:mybot start_typing channel:<[channel]>
//
// @Usage
// Use to edit a message the bot has already sent.
// - ~discord id:mybot edit_message channel:<[channel]> message_id:<[msg]> "Wow! It got edited!"
//
// -->
public enum DiscordInstruction { CONNECT, DISCONNECT, MESSAGE, ADD_ROLE, REMOVE_ROLE, STATUS, RENAME, START_TYPING, STOP_TYPING, EDIT_MESSAGE, DELETE_MESSAGE }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("id")
&& arg.matchesPrefix("id")) {
scriptEntry.addObject("id", new ElementTag(CoreUtilities.toLowerCase(arg.getValue())));
}
else if (!scriptEntry.hasObject("instruction")
&& arg.matchesEnum(DiscordInstruction.values())) {
scriptEntry.addObject("instruction", arg.asElement());
}
else if (!scriptEntry.hasObject("code")
&& arg.matchesPrefix("code")) {
scriptEntry.addObject("code", arg.asElement());
}
else if (!scriptEntry.hasObject("tokenfile")
&& arg.matchesPrefix("tokenfile")) {
scriptEntry.addObject("tokenfile", arg.asElement());
}
else if (!scriptEntry.hasObject("channel")
&& arg.matchesPrefix("channel")
&& arg.matchesArgumentType(DiscordChannelTag.class)) {
scriptEntry.addObject("channel", arg.asType(DiscordChannelTag.class));
}
else if (!scriptEntry.hasObject("url")
&& arg.matchesPrefix("url")) {
scriptEntry.addObject("url", arg.asElement());
}
else if (!scriptEntry.hasObject("user")
&& arg.matchesPrefix("user")
&& arg.matchesArgumentType(DiscordUserTag.class)) {
scriptEntry.addObject("user", arg.asType(DiscordUserTag.class));
}
else if (!scriptEntry.hasObject("group")
&& arg.matchesPrefix("group")
&& arg.matchesArgumentType(DiscordGroupTag.class)) {
scriptEntry.addObject("group", arg.asType(DiscordGroupTag.class));
}
else if (!scriptEntry.hasObject("role")
&& arg.matchesPrefix("role")
&& arg.matchesArgumentType(DiscordRoleTag.class)) {
scriptEntry.addObject("role", arg.asType(DiscordRoleTag.class));
}
else if (!scriptEntry.hasObject("status")
&& arg.matchesPrefix("status")) {
scriptEntry.addObject("status", arg.asElement());
}
else if (!scriptEntry.hasObject("activity")
&& arg.matchesPrefix("activity")) {
scriptEntry.addObject("activity", arg.asElement());
}
else if (!scriptEntry.hasObject("message_id")
&& arg.matchesPrefix("message_id")) {
scriptEntry.addObject("message_id", arg.asElement());
}
else if (!scriptEntry.hasObject("message")) {
scriptEntry.addObject("message", new ElementTag(arg.getRawValue()));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("id")) {
throw new InvalidArgumentsException("Must have an ID!");
}
if (!scriptEntry.hasObject("instruction")) {
throw new InvalidArgumentsException("Must have an instruction!");
}
}
static {
DiscordConnectCommand.fixJDALogger();
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
ElementTag instruction = scriptEntry.getElement("instruction");
ElementTag code = scriptEntry.getElement("code"); // Intentionally do not debug this value.
ElementTag tokenFile = scriptEntry.getElement("tokenfile");
DiscordChannelTag channel = scriptEntry.getObjectTag("channel");
ElementTag message = scriptEntry.getElement("message");
ElementTag status = scriptEntry.getElement("status");
ElementTag activity = scriptEntry.getElement("activity");
DiscordUserTag user = scriptEntry.getObjectTag("user");
DiscordGroupTag guild = scriptEntry.getObjectTag("group");
DiscordRoleTag role = scriptEntry.getObjectTag("role");
ElementTag url = scriptEntry.getElement("url");
ElementTag messageId = scriptEntry.getElement("message_id");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, channel, instruction, message, user, guild, role, status, activity, url, tokenFile, messageId);
}
Supplier<Boolean> requireClientID = () -> {
if (!DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
handleError(scriptEntry, "Failed to process Discord " + instruction.asString() + " command: unknown ID!");
scriptEntry.setFinished(true);
return true;
}
return false;
};
Function<JDA, Boolean> requireClientObject = (_client) -> {
if (_client == null) {
handleError(scriptEntry, "The Discord bot '" + id.asString() + "'is not yet loaded.");
scriptEntry.setFinished(true);
return true;
}
return false;
};
BiFunction<Object, String, Boolean> requireObject = (obj, name) -> {
if (obj == null) {
handleError(scriptEntry, "Failed to process Discord " + instruction.asString() + " command: no " + name + " given!");
scriptEntry.setFinished(true);
return true;
}
return false;
};
Supplier<Boolean> requireUser = () -> requireObject.apply(user, "user");
Supplier<Boolean> requireChannel = () -> requireObject.apply(channel, "channel");
Supplier<Boolean> requireMessage = () -> requireObject.apply(message, "message");
Supplier<Boolean> requireGuild = () -> requireObject.apply(guild, "guild");
Supplier<Boolean> requireRole = () -> requireObject.apply(role, "role");
Supplier<Boolean> requireMessageId = () -> requireObject.apply(messageId, "message_id");
DiscordInstruction instructionEnum = DiscordInstruction.valueOf(instruction.asString().toUpperCase());
if (instructionEnum == DiscordInstruction.CONNECT) {
if (code != null && scriptEntry.dbCallShouldDebug() && com.denizenscript.denizen.utilities.debugging.Debug.record) {
handleError(scriptEntry, "You almost recorded debug of your Discord token - record automatically disabled to protect you.");
com.denizenscript.denizen.utilities.debugging.Debug.record = false;
}
}
Runnable executeCore = () -> {
try {
switch (instructionEnum) {
case CONNECT: {
DenizenDiscordBot.oldConnectCommand.warn(scriptEntry);
if (code == null && tokenFile == null) {
requireObject.apply(null, "tokenfile");
break;
}
if (DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
handleError(scriptEntry, "Failed to connect: duplicate ID!");
break;
}
String codeRaw;
if (code != null) {
codeRaw = code.asString();
}
else {
File f = new File(Denizen.getInstance().getDataFolder(), tokenFile.asString());
if (!Utilities.canReadFile(f)) {
handleError(scriptEntry, "Cannot read from that token file path due to security settings in Denizen/config.yml.");
scriptEntry.setFinished(true);
break;
}
if (!f.exists()) {
handleError(scriptEntry, "Invalid tokenfile specified. File does not exist.");
scriptEntry.setFinished(true);
break;
}
codeRaw = CoreUtilities.journallingLoadFile(f.getAbsolutePath());
if (codeRaw == null || codeRaw.length() < 5 || codeRaw.length() > 200) {
handleError(scriptEntry, "Invalid tokenfile specified. File content doesn't look like a bot token.");
scriptEntry.setFinished(true);
break;
}
codeRaw = codeRaw.trim();
}
DiscordConnection dc = new DiscordConnection();
dc.botID = id.asString();
DenizenDiscordBot.instance.connections.put(id.asString(), dc);
DiscordConnectCommand.DiscordConnectThread dct = new DiscordConnectCommand.DiscordConnectThread();
dct.code = codeRaw;
dct.conn = dc;
dct.ender = () -> scriptEntry.setFinished(true);
dct.start();
break;
}
case DISCONNECT: {
if (requireClientID.get()) {
return;
}
DiscordConnection dc = DenizenDiscordBot.instance.connections.remove(id.asString());
if (dc.flags.modified) {
dc.flags.saveToFile(DiscordConnectCommand.flagFilePathFor(id.asString()));
}
dc.client.shutdown();
try {
dc.client.awaitStatus(JDA.Status.SHUTDOWN);
}
catch (InterruptedException ex) {
handleError(scriptEntry, ex);
}
scriptEntry.setFinished(true);
break;
}
case MESSAGE: {
DenizenDiscordBot.oldMessageCommand.warn(scriptEntry);
if (channel == null && user == null) {
if (!requireChannel.get()) {
requireUser.get();
}
scriptEntry.setFinished(true);
return;
}
if (requireClientID.get() || requireMessage.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
MessageChannel textChan;
if (channel == null) {
User userObj = client.getUserById(user.user_id);
if (userObj == null) {
handleError(scriptEntry, "Invalid or unrecognized user (given user ID not valid? Have you enabled the 'members' intent?).");
scriptEntry.setFinished(true);
return;
}
textChan = userObj.openPrivateChannel().complete();
}
else {
textChan = client.getTextChannelById(channel.channel_id);
}
if (textChan == null) {
handleError(scriptEntry, "No channel to send message to (channel ID invalid, or not a text channel?).");
scriptEntry.setFinished(true);
return;
}
Message sentMessage;
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
sentMessage = textChan.sendMessageEmbeds(embed).complete();
}
else {
sentMessage = textChan.sendMessage(message.asString()).complete();
}
scriptEntry.addObject("message_id", new ElementTag(sentMessage.getId()));
scriptEntry.setFinished(true);
break;
}
case ADD_ROLE: {
if (requireClientID.get() || requireUser.get() || requireGuild.get() || requireRole.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
Guild guildObj = client.getGuildById(guild.guild_id);
Member memberObj = guildObj.getMemberById(user.user_id);
guildObj.addRoleToMember(memberObj, guildObj.getRoleById(role.role_id)).complete();
scriptEntry.setFinished(true);
break;
}
case REMOVE_ROLE: {
if (requireClientID.get() || requireUser.get() || requireRole.get() || requireGuild.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
Guild guildObj = client.getGuildById(guild.guild_id);
Member memberObj = guildObj.getMemberById(user.user_id);
guildObj.removeRoleFromMember(memberObj, guildObj.getRoleById(role.role_id)).complete();
scriptEntry.setFinished(true);
break;
}
case EDIT_MESSAGE: {
if (requireClientID.get() || requireChannel.get() || requireMessage.get() || requireMessageId.get()) {
return;
}
DiscordConnection connection = DenizenDiscordBot.instance.connections.get(id.asString());
if (requireClientObject.apply(connection == null ? null : connection.client)) {
return;
}
MessageChannel textChannel = (MessageChannel) connection.getChannel(channel.channel_id);
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
textChannel.editMessageEmbedsById(messageId.asLong(), embed).complete();
}
else {
textChannel.editMessageById(messageId.asLong(), message.asString()).complete();
}
scriptEntry.setFinished(true);
break;
}
case DELETE_MESSAGE: {
DenizenDiscordBot.oldDeleteMessage.warn(scriptEntry);
if (requireClientID.get() || requireChannel.get() || requireMessageId.get()) {
return;
}
DiscordConnection connection = DenizenDiscordBot.instance.connections.get(id.asString());
if (requireClientObject.apply(connection == null ? null : connection.client)) {
return;
}
((MessageChannel) connection.getChannel(channel.channel_id)).deleteMessageById(messageId.asLong()).complete();
scriptEntry.setFinished(true);
break;
}
case START_TYPING: {
if (requireClientID.get() || requireChannel.get()) {
return;
}
DiscordConnection connection = DenizenDiscordBot.instance.connections.get(id.asString());
if (requireClientObject.apply(connection == null ? null : connection.client)) {
return;
}
MessageChannel textChannel = (MessageChannel) connection.getChannel(channel.channel_id);
textChannel.sendTyping().complete();
scriptEntry.setFinished(true);
break;
}
case STOP_TYPING: {
DenizenDiscordBot.oldStopTyping.warn(scriptEntry);
if (requireClientID.get() || requireChannel.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
// TODO: ?
scriptEntry.setFinished(true);
break;
}
case RENAME: {
if (requireClientID.get() || requireGuild.get() || requireMessage.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
long userId;
if (user == null) {
userId = client.getSelfUser().getIdLong();
}
else {
userId = user.user_id;
}
client.getGuildById(guild.guild_id).getMemberById(userId).modifyNickname(message.asString()).complete();
scriptEntry.setFinished(true);
break;
}
case STATUS: {
if (requireClientID.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
Activity at;
String activityType = CoreUtilities.toLowerCase(activity.toString());
switch (activityType) {
case "watching":
at = Activity.watching(message.asString());
break;
case "streaming":
at = Activity.streaming(message.asString(), url.asString());
break;
case "listening":
at = Activity.listening(message.asString());
break;
default:
at = Activity.playing(message.asString());
break;
}
String statusLower = status == null ? "online" : CoreUtilities.toLowerCase(status.asString());
OnlineStatus statusType;
switch (statusLower) {
case "idle":
statusType = OnlineStatus.IDLE;
break;
case "dnd":
statusType = OnlineStatus.DO_NOT_DISTURB;
break;
case "invisible":
statusType = OnlineStatus.INVISIBLE;
break;
default:
statusType = OnlineStatus.ONLINE;
break;
}
client.getPresence().setPresence(statusType, at);
scriptEntry.setFinished(true);
break;
}
}
}
catch (Throwable ex) {
handleError(scriptEntry, ex);
scriptEntry.setFinished(true);
}
};
if (scriptEntry.shouldWaitFor()) {
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, executeCore);
}
else {
executeCore.run();
}
}
}