-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordCommand.java
549 lines (535 loc) · 26.3 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.DiscordConnection;
import com.denizenscript.ddiscordbot.objects.*;
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.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.bukkit.Bukkit;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
public class DiscordCommand extends AbstractCommand implements Holdable {
public DiscordCommand() {
setName("discord");
setSyntax("discord [id:<id>] [connect code:<botcode>/disconnect/message/add_role/start_typing/stop_typing/remove_role/status (status:<status>) (activity:<activity>)/rename/edit_message/delete_message] (<message>) (message_id:<id>) (channel:<channel>) (user:<user>) (group:<group>) (role:<role>) (url:<url>)");
setRequiredArguments(2, 12);
}
// <--[command]
// @Name discord
// @Syntax discord [id:<id>] [connect code:<botcode>/disconnect/message/add_role/start_typing/stop_typing/remove_role/status (status:<status>) (activity:<activity>)/rename/edit_message/delete_message] (<message>) (message_id:<id>) (channel:<channel>) (user:<user>) (group:<group>) (role:<role>) (url:<url>)
// @Required 2
// @Maximum 12
// @Short Connects to and interacts with Discord.
// @Plugin dDiscordBot
// @Group external
//
// @Description
// Connects to and 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>.
//
// Do not type your bot token code directly into the script.
// The generally recommend way to track tokens is in a separate data YAML file.
// Load the file, issue the connect command, then unload the file. Do not keep it in server memory any longer than needed.
//
// @Tags
// <discord[<bot_id>]>
// <entry[saveName].message_id> returns the ID of the sent message, when the command is ~waited for, and the 'message' argument is used.
//
// @Usage
// Use to connect to Discord via a bot code.
// - ~discord id:mybot connect code:<[code]>
//
// @Usage
// Use to disconnect from Discord.
// - ~discord id:mybot disconnect
//
// @Usage
// Use to message a Discord channel.
// - ~discord id:mybot message channel:<discord[mybot].group[Denizen].channel[bot-spam]> "Hello world!"
//
// @Usage
// Use to message an embed to a Discord channel.
// - ~discord id:mybot message channel:<discord[mybot].group[Denizen].channel[bot-spam]> "<discord_embed.with[title].as[hi].with[description].as[This is an embed!]>"
//
// @Usage
// Use to message a Discord channel and record the ID.
// - ~discord id:mybot message channel:<discord[mybot].group[Denizen].channel[bot-spam]> "Hello world!" save:sent
// - announce "Sent as <entry[sent].message_id>"
//
// @Usage
// Use to send a message to a user through a private channel.
// - ~discord id:mybot message user:<[user]> "Hello world!"
//
// @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 stop typing in a specific channel.
// - ~discord id:mybot stop_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!"
//
// @Usage
// Use to delete a message the bot has already sent.
// - ~discord id:mybot delete_message channel:<[channel]> message_id:<[msg]>
//
// -->
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.getProcessedArgs()) {
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("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!");
}
}
public static class DiscordConnectThread extends Thread {
public String code;
public DiscordConnection conn;
public Runnable ender;
@Override
public void run() {
try {
try {
// Try with intents
JDA jda = JDABuilder.createDefault(code)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS,
GatewayIntent.GUILD_MESSAGE_REACTIONS, GatewayIntent.GUILD_MESSAGES,
GatewayIntent.DIRECT_MESSAGE_REACTIONS, GatewayIntent.DIRECT_MESSAGES)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.setAutoReconnect(true)
.setLargeThreshold(100000)
.setChunkingFilter(ChunkingFilter.ALL)
.build();
conn.client = jda;
jda.awaitReady();
}
catch (Exception ex) {
if (Debug.verbose) {
Debug.echoError(ex);
}
Debug.echoError("Discord full connection attempt failed.");
Debug.log("Discord using fallback connection path - connecting with intents disabled. Enable the members intent in your bot's settings (at https://discord.com/developers/applications ) to fix this.");
// If startup failure, try without intents
JDA jda = JDABuilder.createDefault(code).build();
conn.client = jda;
jda.awaitReady();
}
conn.registerHandlers();
}
catch (Exception ex) {
Bukkit.getScheduler().runTask(DenizenDiscordBot.instance, () -> {
DenizenDiscordBot.instance.connections.remove(conn.botID);
});
Debug.echoError(ex);
}
Bukkit.getScheduler().runTask(DenizenDiscordBot.instance, ender);
}
}
public static void errorMessage(ScriptQueue queue, String message) {
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenDiscordBot.instance, new Runnable() {
@Override
public void run() {
Debug.echoError(queue, message);
}
}, 0);
}
@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.
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.debug()
+ (channel != null ? channel.debug() : "")
+ instruction.debug()
+ (message != null ? message.debug() : "")
+ (user != null ? user.debug() : "")
+ (guild != null ? guild.debug() : "")
+ (role != null ? role.debug() : "")
+ (status != null ? status.debug() : "")
+ (activity != null ? activity.debug() : "")
+ (url != null ? url.debug() : "")
+ (messageId != null ? messageId.debug() : ""));
}
Supplier<Boolean> requireClientID = () -> {
if (!DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Failed to process Discord " + instruction.asString() + " command: unknown ID!");
scriptEntry.setFinished(true);
return true;
}
return false;
};
Function<JDA, Boolean> requireClientObject = (_client) -> {
if (_client == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "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) {
Debug.echoError(scriptEntry.getResidingQueue(), "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 (requireObject.apply(code, "code")) {
return;
}
if (scriptEntry.dbCallShouldDebug() && com.denizenscript.denizen.utilities.debugging.Debug.record) {
Debug.echoError("You almost recorded debug of your Discord token - record automatically disabled to protect you.");
com.denizenscript.denizen.utilities.debugging.Debug.record = false;
}
if (DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Failed to connect: duplicate ID!");
return;
}
DiscordConnection dc = new DiscordConnection();
dc.botID = id.asString();
DenizenDiscordBot.instance.connections.put(id.asString(), dc);
DiscordConnectThread dct = new DiscordConnectThread();
dct.code = code.asString();
dct.conn = dc;
dct.ender = () -> scriptEntry.setFinished(true);
dct.start();
return;
}
Runnable executeCore = () -> {
switch (instructionEnum) {
case DISCONNECT: {
if (requireClientID.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.remove(id.asString()).client;
client.shutdown();
try {
client.awaitStatus(JDA.Status.SHUTDOWN);
}
catch (InterruptedException ex) {
Debug.echoError(ex);
}
scriptEntry.setFinished(true);
break;
}
case MESSAGE: {
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) {
Debug.echoError("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) {
Debug.echoError("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.sendMessage(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;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
MessageChannel textChannel = client.getTextChannelById(channel.channel_id);
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
textChannel.editMessageById(messageId.asLong(), embed).complete();
}
else {
textChannel.editMessageById(messageId.asLong(), message.asString()).complete();
}
scriptEntry.setFinished(true);
break;
}
case DELETE_MESSAGE: {
if (requireClientID.get() || requireChannel.get() || requireMessageId.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
client.getTextChannelById(channel.channel_id).deleteMessageById(messageId.asLong()).complete();
scriptEntry.setFinished(true);
break;
}
case START_TYPING: {
if (requireClientID.get() || requireChannel.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
client.getTextChannelById(channel.channel_id).sendTyping().complete();
scriptEntry.setFinished(true);
break;
}
case STOP_TYPING: {
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());
if (activityType.equals("watching")) {
at = Activity.watching(message.asString());
}
else if (activityType.equals("streaming")) {
at = Activity.streaming(message.asString(), url.asString());
}
else if (activityType.equals("listening")) {
at = Activity.listening(message.asString());
}
else {
at = Activity.playing(message.asString());
}
String statusLower = status == null ? "online" : CoreUtilities.toLowerCase(status.asString());
OnlineStatus statusType;
if (statusLower.equals("idle")) {
statusType = OnlineStatus.IDLE;
}
else if (statusLower.equals("dnd")) {
statusType = OnlineStatus.DO_NOT_DISTURB;
}
else if (statusLower.equals("invisible")) {
statusType = OnlineStatus.INVISIBLE;
}
else {
statusType = OnlineStatus.ONLINE;
}
client.getPresence().setPresence(statusType, at);
scriptEntry.setFinished(true);
break;
}
}
};
if (scriptEntry.shouldWaitFor()) {
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, executeCore);
}
else {
executeCore.run();
}
}
}