First working version
This commit is contained in:
59
src/main/java/ru/bitheaven/donpayinteg/DonateThread.java
Normal file
59
src/main/java/ru/bitheaven/donpayinteg/DonateThread.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package ru.bitheaven.donpayinteg;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import ru.bitheaven.donpayinteg.config.Action;
|
||||
import ru.bitheaven.donpayinteg.config.Config;
|
||||
import ru.bitheaven.donpayinteg.config.ConfigHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
import static ru.bitheaven.donpayinteg.DonPayInteg.LOGGER;
|
||||
|
||||
public class DonateThread extends Thread {
|
||||
private int lastDonate = ConfigHandler.load().getLastDonate();
|
||||
public void run() {
|
||||
String token = ConfigHandler.load().getDonpayToken();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://donatepay.ru/api/v1/notifications?access_token=" + token + "&type=donation&order=ASC&after=" + lastDonate))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response;
|
||||
try {
|
||||
response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
JSONArray array = new JSONObject(response.body()).getJSONArray("data");
|
||||
for(int i = 0; i < array.length(); i++) {
|
||||
JSONObject objects = array.getJSONObject(i);
|
||||
boolean finded = false;
|
||||
String cmd = "", msg = "";
|
||||
for(Action element : ConfigHandler.load().getActions()) {
|
||||
if(element.getSum() == objects.getJSONObject("vars").getFloat("sum")) {
|
||||
cmd = element.getCommand();
|
||||
msg = element.getMessage();
|
||||
finded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!finded) continue;
|
||||
|
||||
lastDonate = objects.getInt("id");
|
||||
|
||||
DonPayInteg.commands.addLast("title @a title \"" + msg.replace("{username}", objects.getJSONObject("vars").getString("name")) + "\"");
|
||||
DonPayInteg.commands.addLast(cmd.replace("{username}", objects.getJSONObject("vars").getString("name")));
|
||||
|
||||
LOGGER.info("Exec donate #{}", lastDonate);
|
||||
Config config = ConfigHandler.load();
|
||||
config.setLastDonate(lastDonate);
|
||||
ConfigHandler.save(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user