· 5 years ago · Dec 21, 2020, 12:46 AM
1package de.kittyGolem.commands;
2
3import com.google.gson.Gson;
4import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
5import de.kittyGolem.interfaces.CommandInterface;
6import de.kittyGolem.utils.EmbedHelper;
7import de.kittyGolem.utils.SQLUtil;
8import net.dv8tion.jda.api.EmbedBuilder;
9import net.dv8tion.jda.api.entities.Message;
10import net.dv8tion.jda.api.entities.MessageChannel;
11import net.dv8tion.jda.api.entities.User;
12import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
13import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
14
15import java.io.BufferedReader;
16import java.io.IOException;
17import java.io.InputStreamReader;
18import java.net.URL;
19import java.sql.ResultSet;
20import java.sql.SQLException;
21import java.util.Arrays;
22import java.util.List;
23import java.util.concurrent.TimeUnit;
24
25public class SetKey implements CommandInterface {
26 private List<String> permissionList = Arrays.asList("tradingpost", "characters", "pvp", "progression", "wallet", "guilds", "builds", "account", "inventories", "unlocks");
27
28 @Override
29 public void performCommand(User user, MessageChannel channel, Message message, MessageReceivedEvent event, EventWaiter waiter) {
30
31 try {
32 String[] fieldsSelect = {"GW2_Key"};
33 Object[] valuesSelect = {user.getIdLong()};
34 ResultSet resultSet = SQLUtil.select(fieldsSelect, "API_Keys", "userID = ?", valuesSelect);
35
36 if (!resultSet.next()) {
37 channel.sendTyping().queue();
38 Thread.sleep(1000);
39 channel.sendMessage("Hihi das ist ein Test!").queue((mes -> {
40 mes.addReaction(":green_yes:790257685962293278").queue();
41 mes.addReaction(":red_no:790257685987590154").queue();
42 }));
43 } else {
44 String apiKey = resultSet.getString("key");
45 GWCallAccount gwCallAccount = getGWCallAccount("https://api.guildwars2.com/v2/account?access_token=", apiKey);
46 GWCallToken gwCallToken = getGWCallToken("https://api.guildwars2.com/v2/tokeninfo?access_token=", apiKey);
47
48 channel.sendTyping().queue();
49 Thread.sleep(1000);
50 EmbedBuilder builder = EmbedHelper.getBuilderWithAuthor(user, "Guild Wars 2 API-Key", user.getAsMention() + ", i got this key from you.")
51 .addField(gwCallAccount.name, "`" + apiKey + "`", false)
52 .addField("Access:", Arrays.toString(gwCallToken.permissions), false)
53 .addField("Options:", "<:key_plus:790365643215732787> Update API-Key\n<:key_minus:790365643085840454> Remove API-Key\n<:red_no:790257685987590154> Cancel", false);
54 channel.sendMessage(builder.build()).queue((mes -> {
55 mes.addReaction(":key_plus:790365643215732787").queue();
56 mes.addReaction(":key_minus:790365643085840454").queue();
57 mes.addReaction(":red_no:790257685987590154").queue();
58
59 waiter.waitForEvent(MessageReactionAddEvent.class, e1 -> e1.getChannel().equals(event.getChannel()) && e1.getUser().equals(event.getAuthor()), e1 -> {
60 String emote = e1.getReactionEmote().isEmoji() ? e1.getReactionEmote().getEmoji() : e1.getReactionEmote().getId();
61
62 if(emote.equalsIgnoreCase("790365643215732787")) {
63 channel.sendTyping().queue();
64 try {
65 Thread.sleep(1000);
66 EmbedBuilder builder1 = EmbedHelper.getBuilderWithAuthor(user, "Update Guild Wars 2 API-Key", user.getAsMention() + ", please copy the new API-Key in the chat down below.");
67 mes.clearReactions().queue();
68 mes.editMessage(builder1.build()).queue();
69
70 waiter.waitForEvent(MessageReceivedEvent.class, e2 -> e2.getAuthor().equals(event.getAuthor()) && e2.getChannel().equals(event.getChannel()) && !e2.getMessage().equals(event.getMessage()), e2 -> {
71 String apiKeyNew = e2.getMessage().getContentRaw();
72
73 channel.sendTyping().queue();
74 try {
75 Thread.sleep(1000);
76 EmbedBuilder builder2 = EmbedHelper.getBuilderWithoutAuthorAndTitle("<a:loading_gw2:790374221485441065> I'm checking your Guild Wars 2 API-Key...");
77 channel.sendMessage(builder2.build()).queue();
78 GWCallCheckToken gwCallCheckToken = getGWCallCheckToken("https://api.guildwars2.com/v2/tokeninfo?access_token=", apiKeyNew);
79 Thread.sleep(3000);
80
81 //TODO IF TOKEN IS WRONG OR HAS NO ACCESS - ERROR.
82
83 } catch (InterruptedException e) {
84 e.printStackTrace();
85 }
86 }, 1, TimeUnit.MINUTES, () -> mes.editMessage(EmbedHelper.timeOut().build()).queue());
87 } catch (InterruptedException e) {
88 e.printStackTrace();
89 }
90 } else if(emote.equalsIgnoreCase("790365643085840454")) {
91 try {
92 Object[] valuesDelete = {user.getIdLong()};
93 SQLUtil.delete("API_Keys", "userID = ?", valuesDelete);
94 } catch (SQLException e) {
95 e.printStackTrace();
96 }
97 } else if(emote.equalsIgnoreCase("790257685987590154")) {
98 return;
99 }
100 }, 1, TimeUnit.MINUTES, () -> mes.editMessage(EmbedHelper.timeOut().build()).queue());
101 }));
102 }
103 } catch (Exception e) {
104 e.printStackTrace();
105 }
106 }
107
108 static class GWCallAccount {
109 String id;
110 String name;
111 long age;
112 long world;
113 String[] guilds;
114 String[] leader;
115 String created;
116 String[] access;
117 boolean commander;
118 long fractal_level;
119 long daily_ap;
120 long monthly_ap;
121 long wvw_rank;
122 }
123
124 private GWCallAccount getGWCallAccount(String API, String token) {
125
126 String json = "";
127 try {
128 json = getJson(API + token);
129 } catch (IOException e) {
130 e.printStackTrace();
131 }
132
133 Gson gson = new Gson();
134 GWCallAccount gwCallAccount = gson.fromJson(json, GWCallAccount.class);
135 return gwCallAccount;
136 }
137
138 static class GWCallToken {
139 String id;
140 String name;
141 String[] permissions;
142 }
143
144 private GWCallToken getGWCallToken(String API, String token) {
145
146 String json = "";
147 try {
148 json = getJson(API + token);
149 } catch (IOException e) {
150 e.printStackTrace();
151 }
152
153 Gson gson = new Gson();
154 GWCallToken gwCallToken = gson.fromJson(json, GWCallToken.class);
155 return gwCallToken;
156 }
157
158 static class GWCallCheckToken {
159 String id;
160 String name;
161 String[] permissions;
162 }
163
164 private GWCallCheckToken getGWCallCheckToken(String API, String token) {
165
166 String json = "";
167 try {
168 json = getJson(API + token);
169 } catch (IOException e) {
170 e.printStackTrace();
171 }
172
173 Gson gson = new Gson();
174 GWCallCheckToken gwCallCheckToken = gson.fromJson(json, GWCallCheckToken.class);
175 return gwCallCheckToken;
176 }
177
178 private String getJson(String urlString) throws IOException {
179 BufferedReader reader = null;
180 try {
181 URL url = new URL(urlString);
182 reader = new BufferedReader(new InputStreamReader(url.openStream()));
183 StringBuffer buffer = new StringBuffer();
184 int read;
185 char[] chars = new char[1024];
186 while ((read = reader.read(chars)) != -1)
187 buffer.append(chars, 0, read);
188 return buffer.toString();
189 } finally {
190 if (reader != null)
191 reader.close();
192 }
193 }
194}
195