Skip to content

Commit

Permalink
Update to better RestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jul 16, 2024
1 parent b62d975 commit 226002a
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 225 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/geysermc/discordbot/GeyserBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ public static void main(String[] args) throws IOException {
new VoiceGroupHandler(),
new BadLinksHandler(),
new HelpHandler(),
new SupportHandler(),
new DeleteHandler(),
new PreviewHandler(),
client.build(),
Expand Down Expand Up @@ -282,8 +281,8 @@ public static void main(String[] args) throws IOException {

// Start the bStats tracking thread
generalThreadPool.scheduleAtFixedRate(() -> {
JSONArray servers = RestClient.simpleGetJsonArray("https://bstats.org/api/v1/plugins/5273/charts/servers/data");
JSONArray players = RestClient.simpleGetJsonArray("https://bstats.org/api/v1/plugins/5273/charts/players/data");
JSONArray servers = RestClient.get("https://bstats.org/api/v1/plugins/5273/charts/servers/data").asJSONArray();
JSONArray players = RestClient.get("https://bstats.org/api/v1/plugins/5273/charts/players/data").asJSONArray();
int serverCount = servers.getJSONArray(servers.length() - 1).getInt(1);
int playerCount = players.getJSONArray(players.length() - 1).getInt(1);
jda.getPresence().setActivity(Activity.playing(BotHelpers.coolFormat(serverCount) + " servers, " + BotHelpers.coolFormat(playerCount) + " players"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,7 +33,6 @@
import org.geysermc.discordbot.util.BotColors;
import org.json.JSONObject;
import pw.chew.chewbotcca.util.RestClient;
import pw.chew.chewbotcca.util.RestClient.RestResponse;

import java.util.Collections;
import java.util.UUID;
Expand All @@ -59,10 +58,9 @@ protected void execute(SlashCommandEvent event) {
EmbedBuilder builder = new EmbedBuilder();
builder.setTitle("Floodgate Player UUID");

RestResponse<JSONObject> restResponse =
RestClient.getJsonObject("https://api.geysermc.org/v2/xbox/xuid/" + username);
int serverCode = restResponse.statusCode();
JSONObject response = restResponse.body();
RestClient.Response restResponse = RestClient.get("https://api.geysermc.org/v2/xbox/xuid/" + username);
int serverCode = restResponse.code();
JSONObject response = restResponse.asJSONObject();

// Check what code the server returns and send the corresponding embed to author.
switch (serverCode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -61,7 +61,7 @@ protected void execute(CommandEvent event) {
}

protected MessageEmbed handle() {
JSONObject stats = RestClient.simpleGetJsonObject("https://api.geysermc.org/v2/stats");
JSONObject stats = RestClient.get("https://api.geysermc.org/v2/stats").asJSONObject();
if (stats.has("error")) {
return MessageHelper.errorResponse(
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -172,7 +172,7 @@ List<Provider> getProviders() {
}

// Fetch the search page
JSONObject contents = RestClient.simpleGetJsonObject("https://raw.githubusercontent.com/GeyserMC/GeyserWiki/master/_data/providers.json");
JSONObject contents = RestClient.get("https://raw.githubusercontent.com/GeyserMC/GeyserWiki/master/_data/providers.json").asJSONObject();
Map<String, Object> descriptionTemplates = contents.getJSONObject("description_templates").toMap();

List<Provider> providers = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -45,7 +45,7 @@ public List<String> checkIssues(JSONObject dump) {
}

String md5Hash = dump.getJSONObject("hashInfo").getString("md5Hash");
String response = RestClient.simpleGetString("https://ci.opencollab.dev/fingerprint/" + md5Hash + "/api/json");
String response = RestClient.get("https://ci.opencollab.dev/fingerprint/" + md5Hash + "/api/json").asString();

// Check if 404
if (response.startsWith("<html>")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {

if (!foundMatch) {
// Make request to https://anti-fish.bitflow.dev/check
RequestBody body = RequestBody.create("{\"message\":" + JSONObject.quote(event.getMessage().getContentRaw()) + "}", RestClient.JSON);
JSONObject response = RestClient.simplePost("https://anti-fish.bitflow.dev/check", body);
JSONObject body = new JSONObject().put("message", event.getMessage().getContentRaw());
JSONObject response = RestClient.post("https://anti-fish.bitflow.dev/check", body).asJSONObject();
if (response.getBoolean("match")) {
JSONArray matches = response.getJSONArray("matches");
for (int i = 0; i < matches.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -92,7 +92,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
// Check attached files
for (Message.Attachment attachment : event.getMessage().getAttachments()) {
if (attachment.getFileName().equals("dump.json")) {
String contents = RestClient.simpleGetString(attachment.getUrl());
String contents = RestClient.get(attachment.getUrl()).asString();

if (isDump(contents)) {
parseDump(event, null, contents);
Expand All @@ -106,7 +106,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
String cleanURL = "https://geysermc.org/utilities/dump_viewer#" + matcher.group(2);
String rawURL = "https://dump.geysermc.org/raw/" + matcher.group(2);

parseDump(event, cleanURL, RestClient.simpleGetString(rawURL));
parseDump(event, cleanURL, RestClient.get(rawURL).asString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -162,7 +162,7 @@ public void onMessageReceived(MessageReceivedEvent event) {
extensions.add("0");
}
if (extensions.contains(attachment.getFileExtension())) {
handleLog(event, RestClient.simpleGetString(attachment.getUrl()), false);
handleLog(event, RestClient.get(attachment.getUrl()).asString(), false);
}
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public void onMessageReceived(MessageReceivedEvent event) {
content = rawContent;
} else {
// We didn't find a url so use the message content
content = RestClient.simpleGetString(url);
content = RestClient.get(url).asString();
}

handleLog(event, content, false);
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/org/geysermc/discordbot/listeners/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.nimbusds.jose.shaded.json.JSONArray;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
Expand Down Expand Up @@ -85,21 +86,24 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
try {
File attachmentFile = attachment.getProxy().downloadToFile(File.createTempFile("GeyserBotTempFile", ".temp")).get();

RequestBody body = RequestBody.create("{" +
"\"name\":" + JSONObject.quote(attachment.getFileName()) + "," +
"\"expires\":\"" + event.getMessage().getTimeCreated().plusDays(1) + "\"," +
"\"files\": [" +
"{" +
"\"name\":" + JSONObject.quote(attachment.getFileName()) + "," +
"\"content\": {" +
"\"format\": \"text\"," +
"\"value\": " + JSONObject.quote(new String(Files.readAllBytes(attachmentFile.toPath()))) +
"}" +
"}" +
"]" +
"}", RestClient.JSON);

JSONObject response = RestClient.simplePost("https://api.paste.gg/v1/pastes", body);
JSONObject body = new JSONObject();
body.put("name", attachment.getFileName());
body.put("expires", event.getMessage().getTimeCreated().plusDays(1));

JSONObject file = new JSONObject();
file.put("name", attachment.getFileName());

JSONObject content = new JSONObject();
content.put("format", "text");
content.put("value", new String(Files.readAllBytes(attachmentFile.toPath())));

file.put("content", content);

JSONArray files = new JSONArray();
files.add(file);
body.put("files", files);

JSONObject response = RestClient.post("https://api.paste.gg/v1/pastes", body).asJSONObject();

// Cleanup the file
attachmentFile.delete();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -49,7 +49,7 @@ public JiraUpdateCheck(String project, String platform) {

@Override
public void populate() throws JSONException {
JSONArray versions = RestClient.simpleGetJsonArray(CHECK_URL + project + "/versions");
JSONArray versions = RestClient.get(CHECK_URL + project + "/versions").asJSONArray();

for (int i = 0; i < versions.length(); i++) {
String name = versions.getJSONObject(i).getString("name");
Expand All @@ -63,7 +63,7 @@ public void populate() throws JSONException {

@Override
public void check() {
String versionsText = RestClient.simpleGetString(CHECK_URL + project + "/versions");
String versionsText = RestClient.get(CHECK_URL + project + "/versions").asString();

try {
JSONArray versions = new JSONArray(versionsText);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022 GeyserMC. http://geysermc.org
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,7 +41,7 @@ public class MinecraftUpdateCheck extends AbstractUpdateCheck {

@Override
public void populate() throws JSONException {
JSONObject versionsData = RestClient.simpleGetJsonObject(CHECK_URL);
JSONObject versionsData = RestClient.get(CHECK_URL).asJSONObject();
JSONArray versions = versionsData.getJSONArray("versions");

for (int i = 0; i < versions.length(); i++) {
Expand All @@ -53,7 +53,7 @@ public void populate() throws JSONException {

@Override
public void check() {
JSONObject versionsData = RestClient.simpleGetJsonObject(CHECK_URL);
JSONObject versionsData = RestClient.get(CHECK_URL).asJSONObject();
if (versionsData.has("error")) {
GeyserBot.LOGGER.warn("Error while checking '" + CHECK_URL + "': " + versionsData.getString("error"));
return;
Expand Down
Loading

0 comments on commit 226002a

Please sign in to comment.