-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordMessageCommand.java
287 lines (278 loc) · 13.7 KB
/
DiscordMessageCommand.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
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.DiscordConnection;
import com.denizenscript.ddiscordbot.objects.*;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.Button;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import org.bukkit.Bukkit;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class DiscordMessageCommand extends AbstractCommand implements Holdable {
public DiscordMessageCommand() {
setName("discordmessage");
setSyntax("discordmessage [id:<id>] [reply:<message>/channel:<channel>/user:<user>] [<message>] (no_mention) (attach_file_name:<name> attach_file_text:<text>)");
setRequiredArguments(3, 7);
}
// <--[command]
// @Name discordmessage
// @Syntax discordmessage [id:<id>] (reply:<message>/channel:<channel>/user:<user>) [<message>] (no_mention) (attach_file_name:<name> attach_file_text:<text>)
// @Required 3
// @Maximum 7
// @Short Sends a message to a Discord channel.
// @Plugin dDiscordBot
// @Group external
//
// @Description
// Sends a message to a Discord channel.
//
// Command may fail if the bot does not have permission within the Discord group to send a message in that channel.
//
// You can send the message to: a channel or user, or optionally in reply to a previous message.
// If sending as a reply, optionally use "no_mention" to disable the default reply pinging the original user.
//
// You can use "attach_file_name:<name>" and "attach_file_text:<text>" to attach a text file with longer content than a normal message allows.
//
// You can use "rows" to attach action rows of components, such as buttons to the message.
//
// The command should usually be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <entry[saveName].message> returns the DiscordMessageTag of the sent message, when the command is ~waited for.
//
// @Usage
// Use to message a Discord channel.
// - ~discordmessage id:mybot channel:<discord[mybot].group[Denizen].channel[bot-spam]> "Hello world!"
//
// @Usage
// Use to reply to a message from a message received event.
// - ~discordmessage id:mybot reply:<context.message> "Hello world!"
//
// @Usage
// Use to message an embed to a Discord channel.
// - ~discordmessage id:mybot channel:<discord[mybot].group[Denizen].channel[bot-spam]> "<discord_embed[title=hi;description=this is an embed!]>"
//
// @Usage
// Use to message a Discord channel and record the ID.
// - ~discordmessage id:mybot 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.
// - ~discordmessage id:mybot message user:<[user]> "Hello world!"
//
// @Usage
// Use to send a text-file message to a channel.
// - ~discordmessage id:mybot channel:<[channel]> attach_file_name:quote.xml "attach_file_text:<<>mcmonkey<>> haha text files amirite<n>gotta abuse em"
//
// @Usage
// Use to send a message and attach a button to it.
// - define my_button <discord_button.with[style].as[primary].with[id].as[my_button].with[label].as[Hello]>
// - ~discordmessage id:mybot channel:<discord[mybot].channel[testing]> rows:<list_single[<list_single[<[my_button]>]>]> "Hello world!"
//
// -->
@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("attach_file_name")
&& arg.matchesPrefix("attach_file_name")) {
scriptEntry.addObject("attach_file_name", arg.asElement());
}
else if (!scriptEntry.hasObject("attach_file_text")
&& arg.matchesPrefix("attach_file_text")) {
scriptEntry.addObject("attach_file_text", arg.asElement());
}
else if (!scriptEntry.hasObject("channel")
&& arg.matchesPrefix("to", "channel")
&& arg.matchesArgumentType(DiscordChannelTag.class)) {
scriptEntry.addObject("channel", arg.asType(DiscordChannelTag.class));
}
else if (!scriptEntry.hasObject("user")
&& arg.matchesPrefix("to", "user")
&& arg.matchesArgumentType(DiscordUserTag.class)) {
scriptEntry.addObject("user", arg.asType(DiscordUserTag.class));
}
else if (!scriptEntry.hasObject("reply")
&& arg.matchesPrefix("reply")
&& arg.matchesArgumentType(DiscordMessageTag.class)) {
scriptEntry.addObject("reply", arg.asType(DiscordMessageTag.class));
}
else if (!scriptEntry.hasObject("no_mention")
&& arg.matches("no_mention")) {
scriptEntry.addObject("no_mention", new ElementTag(true));
}
else if (!scriptEntry.hasObject("rows")
&& arg.matchesPrefix("rows")) {
scriptEntry.addObject("rows", ListTag.getListFor(arg.object, scriptEntry.getContext()).filter(ListTag.class, scriptEntry));
}
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("message") && !scriptEntry.hasObject("attach_file_name")) {
throw new InvalidArgumentsException("Must have a message!");
}
}
public static List<ActionRow> createRows(ScriptEntry scriptEntry, List<ListTag> rows) {
List<ActionRow> actionRows = new ArrayList<>();
if (rows != null) {
for (ListTag row : rows) {
List<Button> buttons = new ArrayList<>();
for (DiscordButtonTag button : row.filter(DiscordButtonTag.class, scriptEntry.getContext())) {
Button built = button.build();
if (built != null) {
buttons.add(built);
}
}
actionRows.add(ActionRow.of(buttons));
}
return actionRows;
}
return null;
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
DiscordChannelTag channel = scriptEntry.getObjectTag("channel");
ElementTag message = scriptEntry.getElement("message");
DiscordUserTag user = scriptEntry.getObjectTag("user");
DiscordMessageTag reply = scriptEntry.getObjectTag("reply");
ElementTag noMention = scriptEntry.getElement("no_mention");
ElementTag attachFileName = scriptEntry.getElement("attach_file_name");
ElementTag attachFileText = scriptEntry.getElement("attach_file_text");
List<ListTag> rows = (List<ListTag>) scriptEntry.getObjectTag("rows");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id.debug()
+ (channel != null ? channel.debug() : "")
+ (message != null ? message.debug() : "")
+ (user != null ? user.debug() : "")
+ (reply != null ? reply.debug() : "")
+ (attachFileName != null ? attachFileName.debug() : "")
+ (attachFileText != null ? attachFileText.debug() : "")
+ (noMention != null ? noMention.debug() : "")
+ (rows != null ? ArgumentHelper.debugList("Rows", rows) : ""));
}
Runnable runner = () -> {
DiscordConnection connection = DenizenDiscordBot.instance.connections.get(id.asString());
if (connection == null) {
Debug.echoError("Failed to process DiscordMessage command: unknown bot ID!");
scriptEntry.setFinished(true);
return;
}
JDA client = connection.client;
MessageChannel toChannel = null;
if (reply != null && reply.channel_id != 0) {
toChannel = client.getTextChannelById(reply.channel_id);
if (toChannel == null) {
toChannel = client.getPrivateChannelById(reply.channel_id);
}
}
else if (channel != null) {
toChannel = client.getTextChannelById(channel.channel_id);
}
else if (user != 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;
}
toChannel = userObj.openPrivateChannel().complete();
}
if (toChannel == null) {
Debug.echoError("Failed to process DiscordMessage command: no channel given!");
scriptEntry.setFinished(true);
return;
}
Message replyTo = null;
if (reply != null) {
replyTo = reply.bot != null ? reply.getMessage() : toChannel.retrieveMessageById(reply.message_id).complete();
}
MessageAction action = null;
boolean isFile = false;
if (message == null || message.asString().length() == 0) {
if (attachFileName != null) {
if (attachFileText != null) {
if (reply != null) {
action = replyTo.reply(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
}
else {
action = toChannel.sendFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
}
isFile = true;
}
}
}
else if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
if (reply != null) {
action = replyTo.replyEmbeds(embed);
}
else {
action = toChannel.sendMessageEmbeds(embed);
}
}
else {
if (reply != null) {
action = replyTo.reply(message.asString());
}
else {
action = toChannel.sendMessage(message.asString());
}
}
if (action == null) {
Debug.echoError("Failed to send message - missing content?");
scriptEntry.setFinished(true);
return;
}
if (!isFile && attachFileName != null) {
if (attachFileText != null) {
action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
}
else {
Debug.echoError("Failed to send attachment - missing content?");
}
}
List<ActionRow> actionRows = DiscordMessageCommand.createRows(scriptEntry, rows);
if (actionRows != null) {
action.setActionRows(actionRows);
}
if (noMention != null && noMention.asBoolean()) {
action = action.mentionRepliedUser(false);
}
Message sentMessage = action.complete();
scriptEntry.addObject("message", new DiscordMessageTag(id.asString(), sentMessage));
scriptEntry.setFinished(true);
};
if (scriptEntry.shouldWaitFor()) {
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, runner);
}
else {
Debug.echoError("DiscordMessage command ran without ~waitable. This will freeze the server. If you wanted your server to freeze, ignore this message.");
runner.run();
}
}
}