-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordMessageCommand.java
190 lines (183 loc) · 8.65 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
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.objects.*;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
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.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.requests.restaction.MessageAction;
import org.bukkit.Bukkit;
public class DiscordMessageCommand extends AbstractCommand implements Holdable {
public DiscordMessageCommand() {
setName("discordmessage");
setSyntax("discordmessage [id:<id>] [reply:<message>/channel:<channel>/user:<user>] [<message>] (no_mention)");
setRequiredArguments(3, 5);
}
// <--[command]
// @Name discordmessage
// @Syntax discordmessage [id:<id>] (reply:<message>/channel:<channel>/user:<user>) [<message>] (no_mention)
// @Required 3
// @Maximum 5
// @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.
//
// The command should usually be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <entry[saveName].message_id> returns the ID 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.with[title].as[hi].with[description].as[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!"
//
// -->
@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("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("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")) {
throw new InvalidArgumentsException("Must have a message!");
}
}
@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");
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() : "")
+ (noMention != null ? noMention.debug() : ""));
}
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (client == null) {
Debug.echoError("Failed to process DiscordMessage command: unknown bot ID!");
scriptEntry.setFinished(true);
return;
}
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;
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
if (reply != null) {
action = replyTo.reply(embed);
}
else {
action = toChannel.sendMessage(embed);
}
}
else {
if (reply != null) {
action = replyTo.reply(message.asString());
}
else {
action = toChannel.sendMessage(message.asString());
}
}
if (noMention != null && noMention.asBoolean()) {
action = action.mentionRepliedUser(false);
}
Message sentMessage = action.complete();
scriptEntry.addObject("message_id", new ElementTag(sentMessage.getId()));
scriptEntry.setFinished(true);
});
}
}