Skip to content

Commit 16b83d3

Browse files
Merge pull request #114 from Tisawesomeness/dev_jda5
Version 0.17.7
2 parents 8d87706 + 8aba586 commit 16b83d3

File tree

7 files changed

+127
-44
lines changed

7 files changed

+127
-44
lines changed

pom.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.tisawesomeness</groupId>
44
<artifactId>minecord</artifactId>
5-
<version>0.17.6</version>
5+
<version>0.17.7</version>
66

77
<repositories>
88
<repository>
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>net.dv8tion</groupId>
1717
<artifactId>JDA</artifactId>
18-
<version>5.0.0-beta.19</version>
18+
<version>5.0.0-beta.20</version>
1919
<exclusions>
2020
<exclusion>
2121
<groupId>club.minnced</groupId>
@@ -52,12 +52,12 @@
5252
<dependency>
5353
<groupId>com.mysql</groupId>
5454
<artifactId>mysql-connector-j</artifactId>
55-
<version>8.2.0</version>
55+
<version>8.3.0</version>
5656
</dependency>
5757
<dependency>
5858
<groupId>org.xerial</groupId>
5959
<artifactId>sqlite-jdbc</artifactId>
60-
<version>3.44.1.0</version>
60+
<version>3.45.0.0</version>
6161
</dependency>
6262
<dependency>
6363
<groupId>dnsjava</groupId>

src/com/tisawesomeness/minecord/Bot.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public class Bot {
4444
public static final String github = "https://github.com/Tisawesomeness/Minecord";
4545
public static final String terms = "https://minecord.github.io/terms";
4646
public static final String privacy = "https://minecord.github.io/privacy";
47-
private static final String version = "0.17.6";
48-
public static final String jdaVersion = "5.0.0-beta.19";
47+
private static final String version = "0.17.7";
48+
public static final String jdaVersion = "5.0.0-beta.20";
4949
public static final Color color = Color.GREEN;
5050

5151
public static ShardManager shardManager;

src/com/tisawesomeness/minecord/command/Command.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import com.tisawesomeness.minecord.Bot;
44
import com.tisawesomeness.minecord.Config;
55
import com.tisawesomeness.minecord.util.MessageUtils;
6-
76
import net.dv8tion.jda.api.entities.MessageEmbed;
87
import net.dv8tion.jda.api.events.Event;
8+
import net.dv8tion.jda.api.exceptions.InteractionFailureException;
99
import net.dv8tion.jda.api.utils.MarkdownUtil;
1010
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
1111
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
1212

13+
import java.io.PrintWriter;
14+
import java.io.StringWriter;
1315
import java.util.UUID;
1416

1517
public interface Command<T extends Event> {
@@ -34,18 +36,25 @@ default String getHelp() {
3436
String debugRunCommand(T e);
3537

3638
default void handleException(Throwable ex, T e) {
37-
UUID uuid = UUID.randomUUID();
39+
// Discord sometimes fails to respond, causing this exception
40+
// not much we can do about it
41+
if (ex instanceof InteractionFailureException) {
42+
System.err.println("InteractionFailureException");
43+
return;
44+
}
45+
46+
UUID exceptionId = UUID.randomUUID();
47+
String consoleLogMsg = String.format("EXCEPTION: `%s`\nID: `%s`\n%s", debugRunCommand(e), exceptionId, getStackTrace(ex));
48+
System.err.println(consoleLogMsg);
3849

39-
String logMsg = String.format("EXCEPTION: `%s`\nID: `%s`\n%s", debugRunCommand(e), uuid, buildErrorMessage(ex));
50+
String logMsg = String.format("EXCEPTION: `%s`\nID: `%s`\n%s", debugRunCommand(e), exceptionId, buildErrorMessage(ex));
4051
String logMsgTrimmed = MessageUtils.trimCodeblock(logMsg);
41-
System.err.println(uuid);
42-
ex.printStackTrace();
4352
Bot.logger.debugLog(logMsgTrimmed);
4453

4554
if (Config.getDebugMode()) {
4655
sendFailure(e, MessageCreateData.fromContent(logMsgTrimmed));
4756
} else {
48-
String userMsg = ":boom: An unexpected error occurred!\nID: " + MarkdownUtil.monospace(uuid.toString());
57+
String userMsg = ":boom: An unexpected error occurred!\nID: " + MarkdownUtil.monospace(exceptionId.toString());
4958
String userMsgTrimmed = MessageUtils.trimCodeblock(userMsg);
5059
sendFailure(e, MessageCreateData.fromContent(userMsgTrimmed));
5160
}
@@ -82,6 +91,13 @@ static String buildStackTrace(Throwable ex) {
8291
return sb.toString();
8392
}
8493

94+
static String getStackTrace(Throwable ex) {
95+
StringWriter sw = new StringWriter();
96+
PrintWriter pw = new PrintWriter(sw);
97+
ex.printStackTrace(pw);
98+
return sw.toString();
99+
}
100+
85101
/**
86102
* Represents all the data needed to register a command.
87103
*/

src/com/tisawesomeness/minecord/command/CommandListener.java

+18-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.tisawesomeness.minecord.util.ArrayUtils;
77
import com.tisawesomeness.minecord.util.MessageUtils;
88
import com.tisawesomeness.minecord.util.type.Either;
9-
109
import net.dv8tion.jda.api.Permission;
1110
import net.dv8tion.jda.api.entities.Member;
1211
import net.dv8tion.jda.api.entities.Message;
@@ -46,13 +45,18 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent e) {
4645
}
4746

4847
//Check for elevation
49-
if (cmd.getInfo().elevated && !Database.isElevated(e.getUser().getIdLong())) {
50-
e.reply(":warning: Insufficient permissions!").setEphemeral(true).queue();
51-
return;
48+
Boolean elevated = null;
49+
if (cmd.getInfo().elevated) {
50+
elevated = Database.isElevated(e.getUser().getIdLong());
51+
if (!elevated) {
52+
e.reply(":warning: Insufficient permissions!").setEphemeral(true).queue();
53+
return;
54+
}
5255
}
5356

5457
//Check for cooldowns, skipping if user is elevated
55-
if (!(Config.getElevatedSkipCooldown() && Database.isElevated(e.getUser().getIdLong())) && cmd.getInfo().cooldown > 0) {
58+
if (cmd.getInfo().cooldown > 0 &&
59+
(!Config.getElevatedSkipCooldown() || Boolean.TRUE.equals(elevated) || !Database.isElevated(e.getUser().getIdLong()))) {
5660
long cooldownLeft = Registry.getCooldownLeft(cmd, e.getUser());
5761
if (cooldownLeft > 0) {
5862
//Format warning message
@@ -164,13 +168,18 @@ public void onMessageReceived(MessageReceivedEvent e) {
164168
MessageChannel c = e.getChannel();
165169

166170
//Check for elevation
167-
if (ci.elevated && !Database.isElevated(a.getIdLong())) {
168-
c.sendMessage(":warning: Insufficient permissions!").queue();
169-
return;
171+
Boolean elevated = null;
172+
if (ci.elevated) {
173+
elevated = Database.isElevated(a.getIdLong());
174+
if (!elevated) {
175+
c.sendMessage(":warning: Insufficient permissions!").queue();
176+
return;
177+
}
170178
}
171179

172180
//Check for cooldowns, skipping if user is elevated
173-
if (!(Config.getElevatedSkipCooldown() && Database.isElevated(a.getIdLong())) && ci.cooldown > 0) {
181+
if (ci.cooldown > 0 &&
182+
(!Config.getElevatedSkipCooldown() || Boolean.TRUE.equals(elevated) || !Database.isElevated(a.getIdLong()))) {
174183
long cooldownLeft = Registry.getCooldownLeft(cmd, a);
175184
if (cooldownLeft > 0) {
176185
//Format warning message

src/com/tisawesomeness/minecord/command/utility/RandomCommand.java

-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ private static Result chooseSubcommand(SlashCommandInteractionEvent e) {
124124
assert choices != null;
125125

126126
String trimmed = trimLeadingCommas(choices); // split() ignores trailing commas, ignore leading to be consistent
127-
System.out.println(trimmed);
128127
if (trimmed.isEmpty()) {
129128
return new Result(Outcome.WARNING, "Must specify at least one choice.");
130129
}

src/com/tisawesomeness/minecord/command/utility/StatusCommand.java

+52-21
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
import com.tisawesomeness.minecord.util.DiscordUtils;
55
import com.tisawesomeness.minecord.util.MessageUtils;
66
import com.tisawesomeness.minecord.util.RequestUtils;
7-
7+
import lombok.RequiredArgsConstructor;
88
import net.dv8tion.jda.api.entities.MessageEmbed;
99
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
1010

11-
import java.awt.Color;
11+
import java.awt.*;
12+
import java.io.IOException;
1213
import java.util.Arrays;
1314
import java.util.List;
1415
import java.util.concurrent.CompletableFuture;
1516
import java.util.stream.Collectors;
1617

1718
public class StatusCommand extends SlashCommand {
1819

19-
private static final List<String> URLS = Arrays.asList(
20-
"https://minecraft.net",
21-
"https://account.mojang.com",
22-
"https://authserver.mojang.com",
23-
"https://textures.minecraft.net",
24-
"https://api.mojang.com"
20+
private static final List<MinecraftService> SERVICES = Arrays.asList(
21+
new UrlMinecraftService("https://minecraft.net", true),
22+
new AccountsService(),
23+
new UrlMinecraftService("https://sessionserver.mojang.com/session/minecraft/profile/853c80ef3c3749fdaa49938b674adae6"),
24+
new UrlMinecraftService("https://textures.minecraft.net/texture/7fd9ba42a7c81eeea22f1524271ae85a8e045ce0af5a6ae16c6406ae917e68b5"),
25+
new UrlMinecraftService("https://api.mojang.com")
2526
);
2627
private static final int CACHE_TIME = 1000 * 60;
2728

@@ -50,10 +51,10 @@ public Result run(SlashCommandInteractionEvent e) {
5051

5152
private static MessageEmbed getStatusResponse() {
5253
// Pings done in separate threads to speed up in case some URLs timeout
53-
List<CompletableFuture<Boolean>> statusRequests = URLS.stream()
54-
.map(StatusCommand::checkUrl)
54+
List<CompletableFuture<Boolean>> statusRequests = SERVICES.stream()
55+
.map(StatusCommand::check)
5556
.collect(Collectors.toList());
56-
CompletableFuture.allOf(statusRequests.toArray(new CompletableFuture[URLS.size()])).join();
57+
CompletableFuture.allOf(statusRequests.toArray(new CompletableFuture[SERVICES.size()])).join();
5758
List<Boolean> statuses = statusRequests.stream()
5859
.map(CompletableFuture::join)
5960
.collect(Collectors.toList());
@@ -65,23 +66,16 @@ private static MessageEmbed getStatusResponse() {
6566

6667
String m = "**Minecraft:** " + statusEmotes.get(0) +
6768
"\n" + "**Accounts:** " + statusEmotes.get(1) +
68-
"\n" + "**Auth Server:** " + statusEmotes.get(2) +
69+
"\n" + "**Session Server:** " + statusEmotes.get(2) +
6970
"\n" + "**Textures:** " + statusEmotes.get(3) +
7071
"\n" + "**Mojang API:** " + statusEmotes.get(4);
7172

7273
Color color = getColor(statuses);
7374
return MessageUtils.embedMessage("Minecraft Status", null, m, color);
7475
}
7576

76-
private static CompletableFuture<Boolean> checkUrl(String url) {
77-
return CompletableFuture.supplyAsync(() -> check(url));
78-
}
79-
private static boolean check(String url) {
80-
// The Minecraft website likes to bug out for some reason, regular GET request sometimes breaks
81-
if (url.equals("https://minecraft.net")) {
82-
return RequestUtils.checkWithSocket("minecraft.net");
83-
}
84-
return RequestUtils.checkURLWithGet(url);
77+
private static CompletableFuture<Boolean> check(MinecraftService service) {
78+
return CompletableFuture.supplyAsync(service::check);
8579
}
8680

8781
private static Color getColor(List<Boolean> statuses) {
@@ -96,4 +90,41 @@ private static Color getColor(List<Boolean> statuses) {
9690
return Color.YELLOW;
9791
}
9892

93+
private interface MinecraftService {
94+
boolean check();
95+
}
96+
97+
@RequiredArgsConstructor
98+
private static class UrlMinecraftService implements MinecraftService {
99+
private final String url;
100+
private final boolean useSocket;
101+
102+
public UrlMinecraftService(String url) {
103+
this(url, false);
104+
}
105+
106+
public boolean check() {
107+
if (useSocket) {
108+
if (url.startsWith("https://")) {
109+
return RequestUtils.checkWithSocket(url.substring(8));
110+
}
111+
return RequestUtils.checkWithSocket(url);
112+
}
113+
return RequestUtils.checkURLWithGet(url);
114+
}
115+
}
116+
117+
private static class AccountsService implements MinecraftService {
118+
@Override
119+
public boolean check() {
120+
try {
121+
RequestUtils.post("https://api.minecraftservices.com/minecraft/profile/lookup/bulk/byname", "[\"jeb_\"]");
122+
return true;
123+
} catch (IOException ex) {
124+
ex.printStackTrace();
125+
return false;
126+
}
127+
}
128+
}
129+
99130
}

src/com/tisawesomeness/minecord/util/RequestUtils.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import com.tisawesomeness.minecord.Bot;
44
import com.tisawesomeness.minecord.Config;
5-
65
import net.dv8tion.jda.api.JDA;
76
import org.discordbots.api.client.DiscordBotListAPI;
87
import org.json.JSONArray;
98
import org.json.JSONObject;
109

1110
import java.io.IOException;
1211
import java.io.InputStream;
12+
import java.io.OutputStream;
1313
import java.net.HttpURLConnection;
1414
import java.net.Socket;
1515
import java.net.URL;
@@ -84,6 +84,34 @@ public static String getPlain(String url, String auth) throws IOException {
8484
return get(conn);
8585
}
8686

87+
/**
88+
* Performs an HTTP POST request.
89+
* @param url The request URL.
90+
* @param query The request payload, in string form.
91+
* @return The response of the request in string form.
92+
*/
93+
public static String post(String url, String query) throws IOException {
94+
return post(url, query, null);
95+
}
96+
97+
/**
98+
* Performs an HTTP POST request.
99+
* @param url The request URL.
100+
* @param query The request payload, in string form.
101+
* @param auth The content of the Authorization header.
102+
* @return The response of the request in string form.
103+
*/
104+
public static String post(String url, String query, String auth) throws IOException {
105+
URLConnection conn = open(url, auth, jsonType);
106+
((HttpURLConnection) conn).setRequestMethod("POST");
107+
108+
OutputStream output = conn.getOutputStream();
109+
output.write(query.getBytes(charset));
110+
output.close();
111+
112+
return get(conn);
113+
}
114+
87115
private static URLConnection open(String url, String auth, String contentType) throws IOException {
88116
URLConnection conn = new URL(url).openConnection();
89117
conn.setDoOutput(true);

0 commit comments

Comments
 (0)