1717import java .io .InputStreamReader ;
1818import java .net .URI ;
1919import java .net .http .HttpResponse ;
20+ import java .nio .charset .StandardCharsets ;
21+ import java .util .Scanner ;
2022
2123public class LinkCommandHandler {
2224 public static Response handleModrinthLink (SlashCommandInteraction interaction ) {
@@ -34,7 +36,7 @@ public static Response handleModrinthLink(SlashCommandInteraction interaction) {
3436 return new MessageResponse ()
3537 .setMessage ("You do not have a Mod Garden account.\n Please create one with **/account create**." )
3638 .markEphemeral ();
37- } else {
39+ } else if ( stream . statusCode () < 200 && stream . statusCode () > 299 ) {
3840 JsonElement json = JsonParser .parseReader (new InputStreamReader (stream .body ()));
3941 String errorDescription = json .isJsonObject () && json .getAsJsonObject ().has ("description" ) ?
4042 json .getAsJsonObject ().getAsJsonPrimitive ("description" ).getAsString () :
@@ -52,8 +54,12 @@ public static Response handleModrinthLink(SlashCommandInteraction interaction) {
5254 return new EmbedResponse ()
5355 .setTitle ("Link your Modrinth Account!" )
5456 .setDescription (
55- "1. Authorize with Modrinth, which will redirect you to a page with a link code.\n " +
56- "2. Enter your link code inside the modal." )
57+ """
58+ 1. Authorize with Modrinth, which will redirect you to a page with a link code.
59+ 2. 2. Enter your link code inside the modal.
60+
61+ You may only have one Modrinth account linked to your Mod Garden account.
62+ """ )
5763 .setColor (0xA9FFA7 )
5864 .addButtonUrl (
5965 URI .create ("https://modrinth.com/auth/authorize?client_id=Q2tuKyb4&redirect_uri=" + GardenBot .API_URL + "discord/oauth/modrinth&scope=USER_READ+PROJECT_READ+VERSION_READ+ORGANIZATION_READ" ),
@@ -70,36 +76,58 @@ public static Response handleModrinthLink(SlashCommandInteraction interaction) {
7076
7177
7278 public static Response handleMinecraftLink (SlashCommandInteraction interaction ) {
79+ interaction .event ().deferReply (true ).queue ();
7380 User user = interaction .event ().getUser ();
81+ String challengeCode = null ;
7482
7583 try {
76- HttpResponse <InputStream > stream = ModGardenAPIClient .get ("user/" + user .getId () + "?service=discord" , HttpResponse .BodyHandlers .ofInputStream ());
77- if (stream .statusCode () == 404 ) {
84+ HttpResponse <InputStream > userResponse = ModGardenAPIClient .get ("user/" + user .getId () + "?service=discord" , HttpResponse .BodyHandlers .ofInputStream ());
85+ if (userResponse .statusCode () == 404 ) {
7886 return new MessageResponse ()
7987 .setMessage ("You do not have a Mod Garden account.\n Please create one with **/account create**." );
80- } else if (stream .statusCode () != 200 ) {
81- JsonElement json = JsonParser .parseReader (new InputStreamReader (stream .body ()));
82- String errorDescription = json .isJsonObject () && json .getAsJsonObject ().has ("description" ) ?
83- json .getAsJsonObject ().getAsJsonPrimitive ("description" ).getAsString () :
84- "Undefined Error." ;
85- return new EmbedResponse ()
86- .setTitle ("Encountered an exception whilst attempting to send the setup for linking your Minecraft account to your Mod Garden account." )
87- .setDescription (stream .statusCode () + ": " + errorDescription + "\n Please report this to a team member." )
88- .setColor (0xFF0000 )
89- .markEphemeral ();
88+ } else if (userResponse .statusCode () != 200 ) {
89+ try (var userStream = new InputStreamReader (userResponse .body ())) {
90+ JsonElement json = JsonParser .parseReader (userStream );
91+ String errorDescription = json .isJsonObject () && json .getAsJsonObject ().has ("description" ) ?
92+ json .getAsJsonObject ().getAsJsonPrimitive ("description" ).getAsString () :
93+ "Undefined Error." ;
94+ return new EmbedResponse ()
95+ .setTitle ("Encountered an exception whilst attempting to send the setup for linking your Minecraft account to your Mod Garden account." )
96+ .setDescription (userResponse .statusCode () + ": " + errorDescription + "\n Please report this to a team member." )
97+ .setColor (0xFF0000 )
98+ .markEphemeral ();
99+ }
100+ }
101+
102+ HttpResponse <InputStream > challengeCodeResponse = ModGardenAPIClient .get ("discord/oauth/minecraft/challenge" , HttpResponse .BodyHandlers .ofInputStream ());
103+ try (var challengeCodeStream = new InputStreamReader (challengeCodeResponse .body (), StandardCharsets .UTF_8 )) {
104+ challengeCode = new Scanner (challengeCodeStream ).next ();
90105 }
91106 } catch (IOException | InterruptedException ex ) {
92107 GardenBot .LOG .error ("" , ex );
93108 }
94109
110+ if (challengeCode == null ) {
111+ return new EmbedResponse ()
112+ .setTitle ("Encountered an exception whilst attempting to send the setup for linking your Minecraft account to your Mod Garden account." )
113+ .setDescription ("Failed to create challenge code for Microsoft Authentication.\n Please report this to a team member." )
114+ .setColor (0xFF0000 )
115+ .markEphemeral ();
116+ }
117+
95118 return new EmbedResponse ()
96119 .setTitle ("Link your Minecraft Account!" )
97120 .setDescription (
98- "1. Authorize with Microsoft, which will redirect you to a page with a link code.\n " +
99- "2. Enter your link code inside the modal." )
121+ """
122+ 1. Authorize with Microsoft, which will redirect you to a page with a link code.
123+ 2. Enter your link code inside the modal.
124+
125+ You may have multiple Minecraft accounts linked to your Mod Garden account.
126+ You are only able to use each Microsoft Authorization link once. Please generate a new message if you need another one.
127+ """ )
100128 .setColor (0xA9FFA7 )
101129 .addButtonUrl (
102- URI .create ("https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=5023fb8b-017e-497a-82af-de70b3b754f0 &response_type=code&redirect_uri=" + GardenBot .API_URL + "discord/oauth/minecraft& scope=XboxLive.signin api.minecraftservices.com " ),
130+ URI .create ("https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=e7ee42f6-e542-4ce6-9f7b-1d31941e84c6 &response_type=code&redirect_uri=" + GardenBot .API_URL . replaceAll ( ":" , "%3A" ). replaceAll ( "/" , "%2F" ) + "discord%2Foauth%2Fminecraft&response_mode=query& scope=XboxLive.signIn&state=" + challengeCode + "&prompt=select_account&code_challenge=" + challengeCode + "&code_challenge_method=S256 " ),
103131 "1. Authorize" ,
104132 Emoji .fromCustom ("microsoft" , 1360176270687731842L , false )
105133 )
0 commit comments