4
4
import com .tisawesomeness .minecord .util .DiscordUtils ;
5
5
import com .tisawesomeness .minecord .util .MessageUtils ;
6
6
import com .tisawesomeness .minecord .util .RequestUtils ;
7
-
7
+ import lombok . RequiredArgsConstructor ;
8
8
import net .dv8tion .jda .api .entities .MessageEmbed ;
9
9
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
10
10
11
- import java .awt .Color ;
11
+ import java .awt .*;
12
+ import java .io .IOException ;
12
13
import java .util .Arrays ;
13
14
import java .util .List ;
14
15
import java .util .concurrent .CompletableFuture ;
15
16
import java .util .stream .Collectors ;
16
17
17
18
public class StatusCommand extends SlashCommand {
18
19
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" )
25
26
);
26
27
private static final int CACHE_TIME = 1000 * 60 ;
27
28
@@ -50,10 +51,10 @@ public Result run(SlashCommandInteractionEvent e) {
50
51
51
52
private static MessageEmbed getStatusResponse () {
52
53
// 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 )
55
56
.collect (Collectors .toList ());
56
- CompletableFuture .allOf (statusRequests .toArray (new CompletableFuture [URLS .size ()])).join ();
57
+ CompletableFuture .allOf (statusRequests .toArray (new CompletableFuture [SERVICES .size ()])).join ();
57
58
List <Boolean > statuses = statusRequests .stream ()
58
59
.map (CompletableFuture ::join )
59
60
.collect (Collectors .toList ());
@@ -65,23 +66,16 @@ private static MessageEmbed getStatusResponse() {
65
66
66
67
String m = "**Minecraft:** " + statusEmotes .get (0 ) +
67
68
"\n " + "**Accounts:** " + statusEmotes .get (1 ) +
68
- "\n " + "**Auth Server:** " + statusEmotes .get (2 ) +
69
+ "\n " + "**Session Server:** " + statusEmotes .get (2 ) +
69
70
"\n " + "**Textures:** " + statusEmotes .get (3 ) +
70
71
"\n " + "**Mojang API:** " + statusEmotes .get (4 );
71
72
72
73
Color color = getColor (statuses );
73
74
return MessageUtils .embedMessage ("Minecraft Status" , null , m , color );
74
75
}
75
76
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 );
85
79
}
86
80
87
81
private static Color getColor (List <Boolean > statuses ) {
@@ -96,4 +90,41 @@ private static Color getColor(List<Boolean> statuses) {
96
90
return Color .YELLOW ;
97
91
}
98
92
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
+
99
130
}
0 commit comments