|
| 1 | +package me.TechsCode.UpdateServer; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonArray; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.net.HttpURLConnection; |
| 10 | +import java.net.URL; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class DiscordAPI { |
| 16 | + |
| 17 | + private final String apiUrl; |
| 18 | + private final String apiKey; |
| 19 | + |
| 20 | + public DiscordAPI(String url, String key){ |
| 21 | + this.apiUrl = url; |
| 22 | + this.apiKey = key; |
| 23 | + } |
| 24 | + |
| 25 | + private JsonObject MakeObjectRequest(String endpoint){ |
| 26 | + JsonObject response = new JsonObject(); |
| 27 | + try{ |
| 28 | + URL url = new URL(apiUrl +"/api/"+ apiKey +"/"+endpoint); |
| 29 | + |
| 30 | + HttpURLConnection con = (HttpURLConnection)url.openConnection(); |
| 31 | + con.setRequestMethod("GET"); |
| 32 | + con.setRequestProperty("Content-Type", "application/json; utf-8"); |
| 33 | + con.setRequestProperty("Accept", "application/json"); |
| 34 | + |
| 35 | + try(BufferedReader br = new BufferedReader( |
| 36 | + new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) { |
| 37 | + StringBuilder responseText = new StringBuilder(); |
| 38 | + String responseLine = null; |
| 39 | + while ((responseLine = br.readLine()) != null) { |
| 40 | + responseText.append(responseLine.trim()); |
| 41 | + } |
| 42 | + response = new Gson().fromJson(responseText.toString(), JsonObject.class); |
| 43 | + response.addProperty("statusCode", con.getResponseCode()); |
| 44 | + } |
| 45 | + }catch (Exception e){ |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + return response; |
| 49 | + } |
| 50 | + private JsonArray MakeArrayRequest(String endpoint){ |
| 51 | + JsonArray response = new JsonArray(); |
| 52 | + try{ |
| 53 | + URL url = new URL(apiUrl +"/api/"+ apiKey +"/"+endpoint); |
| 54 | + |
| 55 | + HttpURLConnection con = (HttpURLConnection)url.openConnection(); |
| 56 | + con.setRequestMethod("GET"); |
| 57 | + con.setRequestProperty("Content-Type", "application/json; utf-8"); |
| 58 | + con.setRequestProperty("Accept", "application/json"); |
| 59 | + |
| 60 | + try(BufferedReader br = new BufferedReader( |
| 61 | + new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) { |
| 62 | + StringBuilder responseText = new StringBuilder(); |
| 63 | + String responseLine = null; |
| 64 | + while ((responseLine = br.readLine()) != null) { |
| 65 | + responseText.append(responseLine.trim()); |
| 66 | + } |
| 67 | + response = new Gson().fromJson(responseText.toString(), JsonArray.class); |
| 68 | + } |
| 69 | + }catch (Exception e){ |
| 70 | + e.printStackTrace(); |
| 71 | + } |
| 72 | + return response; |
| 73 | + } |
| 74 | + |
| 75 | + public boolean HasRole(String memberId, String roleId){ |
| 76 | + String url = "guild/member/"+memberId+"/roles/"+roleId; |
| 77 | + JsonObject response = MakeObjectRequest(url); |
| 78 | + return response.get("hasRole").getAsBoolean(); |
| 79 | + } |
| 80 | + public boolean HasRoles(String memberId, String[] roleIds, Boolean RequireOne){ |
| 81 | + boolean HasRoles = false; |
| 82 | + for (String roleId : roleIds) { |
| 83 | + String url = "guild/member/"+memberId+"/roles/"+roleId; |
| 84 | + JsonObject response = MakeObjectRequest(url); |
| 85 | + HasRoles = response.get("hasRole").getAsBoolean(); |
| 86 | + if (RequireOne){ |
| 87 | + if (HasRoles){ |
| 88 | + return true; |
| 89 | + } |
| 90 | + }else{ |
| 91 | + if (!HasRoles){ |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return HasRoles; |
| 97 | + } |
| 98 | + public List<DiscordRole> GetRoles(String memberId){ |
| 99 | + String url = "guild/member/"+memberId+"/roles"; |
| 100 | + JsonArray response = MakeArrayRequest(url); |
| 101 | + List<DiscordRole> roles = new ArrayList<>(); |
| 102 | + response.forEach(role -> { |
| 103 | + roles.add(new DiscordRole(role.getAsJsonObject())); |
| 104 | + }); |
| 105 | + return roles; |
| 106 | + } |
| 107 | + public DiscordMember GetUserInfo(String memberId){ |
| 108 | + String url = "guild/member/"+memberId+"/info"; |
| 109 | + JsonObject response = MakeObjectRequest(url); |
| 110 | + return new DiscordMember(response); |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments