-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordInteractionCommand.java
238 lines (230 loc) · 12.3 KB
/
DiscordInteractionCommand.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
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.objects.DiscordEmbedTag;
import com.denizenscript.ddiscordbot.objects.DiscordInteractionTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ObjectTag;
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.debugging.Debug;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.callbacks.IDeferrableCallback;
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.requests.restaction.WebhookMessageAction;
import net.dv8tion.jda.api.requests.restaction.WebhookMessageUpdateAction;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import org.bukkit.Bukkit;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class DiscordInteractionCommand extends AbstractDiscordCommand implements Holdable {
public DiscordInteractionCommand() {
setName("discordinteraction");
setSyntax("discordinteraction [defer/reply/edit/delete] [interaction:<interaction>] (ephemeral) (attach_file_name:<name>) (attach_file_text:<text>) (rows:<rows>) (<message>)");
setRequiredArguments(2, 7);
setPrefixesHandled("interaction", "rows", "attach_file_name", "attach_file_text");
setBooleansHandled("ephemeral");
isProcedural = false;
}
// <--[command]
// @Name discordinteraction
// @Syntax discordinteraction [defer/reply/delete] [interaction:<interaction>] (ephemeral) (attach_file_name:<name>) (attach_file_text:<text>) (rows:<rows>) (<message>)
// @Required 2
// @Maximum 7
// @Short Manages Discord interactions.
// @Plugin dDiscordBot
// @Guide https://guide.denizenscript.com/guides/expanding/ddiscordbot.html
// @Group external
//
// @Description
// Manages Discord interactions.
//
// You can defer, reply to, edit, or delete an interaction. These instructions all require the "interaction" argument.
//
// The "ephemeral" argument can be used to have the reply message be visible to that one user.
//
// You can defer an interaction before replying, which is useful if your reply may take more than a few seconds to be selected.
// If you defer, the 'ephemeral' option can only be set by the defer - you cannot change it with the later reply.
// Replying to an interaction uses similar logic to normal messaging. See <@link command discordmessage>.
// If you deferred without using 'ephemeral', the 'delete' option will delete the "Thinking..." message.
//
// Slash commands, and replies to interactions, have limitations. See <@link url https://gist.github.com/MinnDevelopment/b883b078fdb69d0e568249cc8bf37fe9>.
//
// Generally used alongside <@link command discordcommand>
//
// The command should usually be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <entry[saveName].command> returns the DiscordCommandTag of a slash command upon creation, when the command is ~waited for.
//
// @Usage
// Use to quickly reply to a slash command interaction.
// - ~discordinteraction reply interaction:<context.interaction> "hello!"
//
// @Usage
// Use to defer and reply to a slash command interaction.
// - ~discordinteraction defer interaction:<context.interaction>
// - ~discordinteraction reply interaction:<context.interaction> <context.options.get[hello].if_null[world]>
//
// @Usage
// Use to defer and reply to an interaction ephemerally.
// - ~discordinteraction defer interaction:<context.interaction> ephemeral:true
// - ~discordinteraction reply interaction:<context.interaction> "Shh, don't tell anyone!"
//
// -->
public enum DiscordInteractionInstruction { DEFER, REPLY, EDIT, DELETE }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("instruction")
&& arg.matchesEnum(DiscordInteractionInstruction.class)) {
scriptEntry.addObject("instruction", arg.asElement());
}
else if (!scriptEntry.hasObject("message")) {
scriptEntry.addObject("message", arg.getRawObject());
}
else {
arg.reportUnhandled();
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag instruction = scriptEntry.getElement("instruction");
DiscordInteractionTag interaction = scriptEntry.requiredArgForPrefix("interaction", DiscordInteractionTag.class);
boolean ephemeral = scriptEntry.argAsBoolean("ephemeral");
ElementTag attachFileName = scriptEntry.argForPrefixAsElement("attach_file_name", null);
ElementTag attachFileText = scriptEntry.argForPrefixAsElement("attach_file_text", null);
ObjectTag rows = scriptEntry.argForPrefix("rows", ObjectTag.class, true);
ObjectTag message = scriptEntry.getObjectTag("message");
if (scriptEntry.dbCallShouldDebug()) {
// Note: attachFileText intentionally at end
Debug.report(scriptEntry, getName(), instruction, interaction, ephemeral, rows, message, attachFileName, attachFileText);
}
DiscordInteractionInstruction instructionEnum = DiscordInteractionInstruction.valueOf(instruction.asString().toUpperCase());
Runnable runner = () -> {
try {
switch (instructionEnum) {
case DEFER: {
if (interaction.interaction == null) {
handleError(scriptEntry, "Invalid interaction! Has it expired?");
return;
}
if (interaction.interaction instanceof IReplyCallback) {
((IReplyCallback) interaction.interaction).deferReply(ephemeral).complete();
} else {
handleError(scriptEntry, "Interaction is not a reply callback!");
}
break;
}
case EDIT:
case REPLY: {
if (interaction.interaction == null) {
handleError(scriptEntry, "Invalid interaction! Has it expired?");
return;
}
/*
* Messages aren't allowed to have attachments in ephemeral messages
* Since you can't see if the acknowledged message is ephemeral or not, this is a requirement so we don't have to try/catch
*/
else if (message == null) {
handleError(scriptEntry, "Must have a message!");
return;
}
MessageEmbed embed = null;
List<ActionRow> actionRows = DiscordMessageCommand.createRows(scriptEntry, rows);
if (message.shouldBeType(DiscordEmbedTag.class)) {
embed = message.asType(DiscordEmbedTag.class, scriptEntry.context).build(scriptEntry.getContext()).build();
}
if (instructionEnum == DiscordInteractionInstruction.EDIT) {
WebhookMessageUpdateAction<Message> action;
InteractionHook hook = ((IDeferrableCallback) interaction.interaction).getHook();
if (embed != null) {
action = hook.editOriginalEmbeds(embed);
}
else {
action = hook.editOriginal(message.toString());
}
if (attachFileName != null) {
if (attachFileText != null) {
action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
}
else {
handleError(scriptEntry, "Failed to send attachment - missing content?");
}
}
if (actionRows != null) {
action = action.setActionRows(actionRows);
}
action.complete();
}
else if (interaction.interaction.isAcknowledged()) {
WebhookMessageAction<Message> action;
InteractionHook hook = ((IDeferrableCallback) interaction.interaction).getHook();
if (embed != null) {
action = hook.sendMessageEmbeds(embed);
}
else {
action = hook.sendMessage(message.toString());
}
if (attachFileName != null) {
if (attachFileText != null) {
action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
}
else {
handleError(scriptEntry, "Failed to send attachment - missing content?");
}
}
if (actionRows != null) {
action = action.addActionRows(actionRows);
}
action.complete();
} else {
ReplyCallbackAction action;
IReplyCallback replyTo = (IReplyCallback) interaction.interaction;
if (embed != null) {
action = replyTo.replyEmbeds(embed);
}
else {
action = replyTo.reply(message.toString());
}
if (attachFileName != null) {
if (attachFileText != null) {
action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
}
else {
handleError(scriptEntry, "Failed to send attachment - missing content?");
}
}
if (actionRows != null) {
action = action.addActionRows(actionRows);
}
action = action.setEphemeral(ephemeral);
action.complete();
}
break;
}
case DELETE: {
if (interaction.interaction == null) {
handleError(scriptEntry, "Invalid interaction! Has it expired?");
return;
}
((IDeferrableCallback) interaction.interaction).getHook().deleteOriginal().complete();
break;
}
}
}
catch (Exception ex) {
handleError(scriptEntry, ex);
}
};
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
runner.run();
scriptEntry.setFinished(true);
});
}
}