-
Notifications
You must be signed in to change notification settings - Fork 13
/
DiscordConnectCommand.java
267 lines (247 loc) · 12 KB
/
DiscordConnectCommand.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
package com.denizenscript.ddiscordbot.commands;
import com.denizenscript.ddiscordbot.DenizenDiscordBot;
import com.denizenscript.ddiscordbot.DiscordConnection;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import com.denizenscript.denizencore.objects.Argument;
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.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.dv8tion.jda.internal.utils.JDALogger;
import org.bukkit.Bukkit;
import org.slf4j.Logger;
import java.io.*;
import java.lang.invoke.MethodHandle;
import java.util.*;
import java.util.stream.Collectors;
public class DiscordConnectCommand extends AbstractCommand implements Holdable {
public DiscordConnectCommand() {
setName("discordconnect");
setSyntax("discordconnect [id:<id>] [tokenfile:<file>] (intents:<intent>|...)");
setRequiredArguments(2, 3);
}
// <--[command]
// @Name discordconnect
// @Syntax discordconnect [id:<id>] [tokenfile:<file>] (intents:<intent>|...)
// @Required 2
// @Maximum 3
// @Short Connects to Discord.
// @Plugin dDiscordBot
// @Guide https://guide.denizenscript.com/guides/expanding/ddiscordbot.html
// @Group external
//
// @Description
// Connects to Discord.
//
// The connection will automatically specify the following gateway intents:
// GUILD_MEMBERS, GUILD_EMOJIS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS
// Optionally specify additional Gateway Intents to use as a list of any of:
// GUILD_BANS, GUILD_WEBHOOKS, GUILD_INVITES, GUILD_VOICE_STATES, GUILD_PRESENCES, GUILD_MESSAGE_TYPING, DIRECT_MESSAGE_TYPING
//
// Note that you need to enable the 'members' intent on your bot in Discord bot settings https://discord.com/developers/applications
// And also may need to manually enable other intents if you specify any.
// If the members intent is not enabled, a significant amount of dDiscordBot's functionality will not work.
//
// Store your Discord bot token in a file, like "plugins/Denizen/data/discord_token.txt", with the token as the only content of the file, which you can then use like "tokenfile:data/discord_token.txt"
//
// The command should always be ~waited for. See <@link language ~waitable>.
//
// @Tags
// <discord[<bot_id>]>
//
// @Usage
// Use to connect to Discord with a token stored in text file 'plugins/Denizen/data/discord_token.txt'.
// - ~discordconnect id:mybot tokenfile:data/discord_token.txt
//
// -->
@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("tokenfile")
&& arg.matchesPrefix("tokenfile")) {
scriptEntry.addObject("tokenfile", arg.asElement());
}
else if (!scriptEntry.hasObject("intents")
&& arg.matchesPrefix("intents")
&& arg.matchesEnum(GatewayIntent.values())) {
scriptEntry.addObject("intents", arg.asType(ListTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("id")) {
throw new InvalidArgumentsException("Must have an ID!");
}
if (!scriptEntry.hasObject("tokenfile")) {
throw new InvalidArgumentsException("Must have a tokenfile!");
}
}
public static boolean loggerIsFixed = false;
public static PrintStream altLogger = new PrintStream(new ByteArrayOutputStream()) {
@Override
public void println(String s) {
Debug.log("JDA", s);
}
};
/**
* This method is a dirty hack to minimize the amount of broken output from JDA.
*/
public static void fixJDALogger() {
if (loggerIsFixed) {
return;
}
loggerIsFixed = true;
// Dirty hack step 1: break System.err so Paper won't complain when JDALogger's static init whines into System.err
PrintStream currentErr = System.err;
System.setErr(altLogger);
Logger defaultLogger = null;
try {
// Force JDALogger to init now, which will do that spam, and get a SimpleLogger instance while we're at it.
defaultLogger = JDALogger.getLog(DiscordConnectCommand.class);
}
finally {
// Fix the logger back, with a try/finally to avoid breaking it.
System.setErr(currentErr);
}
try {
// Dirty hack step 2: use that SimpleLogger instance to modify the class and redirect its log path to one that won't get complained about by Paper.
MethodHandle streamSetter = ReflectionHelper.getFinalSetter(defaultLogger.getClass(), "TARGET_STREAM");
streamSetter.invoke(altLogger);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
static {
fixJDALogger();
}
public static class DiscordConnectThread extends Thread {
public String code;
public DiscordConnection conn;
public Runnable ender;
public ScriptQueue queue;
public HashSet<GatewayIntent> intents = new HashSet<>(Arrays.asList(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS, GatewayIntent.GUILD_MESSAGE_REACTIONS,
GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGE_REACTIONS, GatewayIntent.DIRECT_MESSAGES));
@Override
public void run() {
try {
try {
// Try with intents
JDA jda;
// Hack to bypass Paper whining about JDA whining into System.err
PrintStream currentErr = System.err;
System.setErr(altLogger);
try {
jda = JDABuilder.createDefault(code)
.enableCache(Arrays.stream(CacheFlag.values()).filter(f -> f.getRequiredIntent() == null || intents.contains(f.getRequiredIntent())).collect(Collectors.toList()))
.enableIntents(intents)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.setAutoReconnect(true)
.setLargeThreshold(100000)
.setChunkingFilter(ChunkingFilter.ALL)
.build();
}
finally {
System.setErr(currentErr);
}
conn.client = jda;
jda.awaitReady();
}
catch (Exception ex) {
if (Debug.verbose) {
Debug.echoError(ex);
}
DiscordCommand.errorMessage(queue, "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, () -> {
conn.flags = SavableMapFlagTracker.loadFlagFile(flagFilePathFor(conn.botID));
ender.run();
});
}
}
public static String flagFilePathFor(String bot) {
return DenizenDiscordBot.instance.getDataFolder().getPath() + "/flags/bot_" + Argument.prefixCharsAllowed.trimToMatches(CoreUtilities.toLowerCase(bot)) + ".dat";
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
ElementTag tokenFile = scriptEntry.getElement("tokenfile");
ListTag intents = scriptEntry.getObjectTag("intents");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, tokenFile, intents);
}
if (DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
DiscordCommand.errorMessage(scriptEntry.getResidingQueue(), "Failed to connect: duplicate ID!");
return;
}
DiscordConnection dc = new DiscordConnection();
dc.botID = id.asString();
DenizenDiscordBot.instance.connections.put(id.asString(), dc);
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
File f = new File(Denizen.getInstance().getDataFolder(), tokenFile.asString());
if (!Utilities.canReadFile(f)) {
DiscordCommand.errorMessage(scriptEntry.getResidingQueue(), "Cannot read from that token file path due to security settings in Denizen/config.yml.");
scriptEntry.setFinished(true);
DenizenDiscordBot.instance.connections.remove(id.asString());
return;
}
if (!f.exists()) {
DiscordCommand.errorMessage(scriptEntry.getResidingQueue(), "Invalid tokenfile specified. File does not exist.");
scriptEntry.setFinished(true);
DenizenDiscordBot.instance.connections.remove(id.asString());
return;
}
String codeRaw = CoreUtilities.journallingLoadFile(f.getAbsolutePath());
if (codeRaw == null || codeRaw.length() < 5 || codeRaw.length() > 200) {
DiscordCommand.errorMessage(scriptEntry.getResidingQueue(), "Invalid tokenfile specified. File content doesn't look like a bot token.");
scriptEntry.setFinished(true);
DenizenDiscordBot.instance.connections.remove(id.asString());
return;
}
codeRaw = codeRaw.trim();
DiscordConnectThread dct = new DiscordConnectThread();
dct.queue = scriptEntry.getResidingQueue();
dct.code = codeRaw;
dct.conn = dc;
dct.ender = () -> scriptEntry.setFinished(true);
if (intents != null) {
for (String intent : intents) {
dct.intents.add(GatewayIntent.valueOf(intent.toUpperCase()));
}
}
dct.start();
});
}
}