Check for valid json in crypto commands, move class
This commit is contained in:
1 parent
8d442a45fc
commit
c44ddd42be
10 files changed
+46
-11
No files matched your search
@@ -6,7 +6,7 @@ import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import netneutrality.command.CommandManager;
|
||||
import netneutrality.core.CommandManager;
|
||||
import netneutrality.core.LogHandler;
|
||||
import netneutrality.db.DBController;
|
||||
import netneutrality.handler.hooks.guilds.ServerJoinHandler;
|
||||
|
||||
@@ -9,7 +9,7 @@ package netneutrality.command.basic;
|
||||
import net.dv8tion.jda.api.MessageBuilder;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import netneutrality.command.AbstractCommand;
|
||||
import netneutrality.command.CommandManager;
|
||||
import netneutrality.core.CommandManager;
|
||||
import netneutrality.command.CommandType;
|
||||
import netneutrality.command.CommandUsage;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
|
||||
@@ -13,6 +13,7 @@ import netneutrality.core.LogHandler;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
import netneutrality.http.WebModule;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.NumberFormat;
|
||||
@@ -84,7 +85,13 @@ public class BitcoinPriceCommand extends AbstractCommand {
|
||||
|
||||
try {
|
||||
WebModule http = new WebModule(String.format("%s?fsym=%s&tsyms=%s", baseURL, crypto, finalCurrency.toUpperCase())).request();
|
||||
price = dollarFormat.format(http.getAsObject().getDouble(finalCurrency));
|
||||
JSONObject json = http.getAsObject();
|
||||
if (json.has("Response") && json.getString("Response").equalsIgnoreCase("Error")) {
|
||||
t.editMessage(String.format("Failed getting price of %s", crypto)).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
price = dollarFormat.format(json.getDouble(finalCurrency));
|
||||
t.editMessage(String.format("The current price of %s in %s is %s", crypto, finalCurrency, price)).queue();
|
||||
} catch (IOException | JSONException | IllegalArgumentException | ArithmeticException e) {
|
||||
t.editMessage(String.format("Error getting price of %s!", crypto)).queue();
|
||||
|
||||
@@ -13,6 +13,7 @@ import netneutrality.core.LogHandler;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
import netneutrality.http.WebModule;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.NumberFormat;
|
||||
@@ -87,7 +88,13 @@ public class CryptoPriceCommand extends AbstractCommand {
|
||||
|
||||
try {
|
||||
WebModule http = new WebModule(String.format("%s?fsym=%s&tsyms=%s", baseURL, crypto, finalCurrency.toUpperCase())).request();
|
||||
price = dollarFormat.format(http.getAsObject().getDouble(finalCurrency));
|
||||
JSONObject json = http.getAsObject();
|
||||
if (json.has("Response") && json.getString("Response").equalsIgnoreCase("Error")) {
|
||||
t.editMessage("Crypto doesn't exist.. yet").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
price = dollarFormat.format(json.getDouble(finalCurrency));
|
||||
t.editMessage(String.format("The current price of %s in %s is %s", crypto, finalCurrency, price)).queue();
|
||||
|
||||
} catch (IOException | JSONException | IllegalArgumentException | ArithmeticException e) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import netneutrality.core.LogHandler;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
import netneutrality.http.WebModule;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.NumberFormat;
|
||||
@@ -84,7 +85,13 @@ public class EthereumPriceCommand extends AbstractCommand {
|
||||
|
||||
try {
|
||||
WebModule http = new WebModule(String.format("%s?fsym=%s&tsyms=%s", baseURL, crypto, finalCurrency.toUpperCase())).request();
|
||||
price = dollarFormat.format(http.getAsObject().getDouble(finalCurrency));
|
||||
JSONObject json = http.getAsObject();
|
||||
if (json.has("Response") && json.getString("Response").equalsIgnoreCase("Error")) {
|
||||
t.editMessage(String.format("Failed getting price of %s", crypto)).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
price = dollarFormat.format(json.getDouble(finalCurrency));
|
||||
t.editMessage(String.format("The current price of %s in %s is %s", crypto, finalCurrency, price)).queue();
|
||||
} catch (IOException | JSONException | IllegalArgumentException e) {
|
||||
t.editMessage(String.format("Error getting price of %s!", crypto)).queue();
|
||||
|
||||
@@ -13,6 +13,7 @@ import netneutrality.core.LogHandler;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
import netneutrality.http.WebModule;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.NumberFormat;
|
||||
@@ -84,7 +85,13 @@ public class LitecoinPriceCommand extends AbstractCommand {
|
||||
|
||||
try {
|
||||
WebModule http = new WebModule(String.format("%s?fsym=%s&tsyms=%s", baseURL, crypto, finalCurrency.toUpperCase())).request();
|
||||
price = dollarFormat.format(http.getAsObject().getDouble(finalCurrency));
|
||||
JSONObject json = http.getAsObject();
|
||||
if (json.has("Response") && json.getString("Response").equalsIgnoreCase("Error")) {
|
||||
t.editMessage(String.format("Failed getting price of %s", crypto)).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
price = dollarFormat.format(json.getDouble(finalCurrency));
|
||||
t.editMessage(String.format("The current price of %s in %s is %s", crypto, finalCurrency, price)).queue();
|
||||
} catch (IOException | JSONException | IllegalArgumentException | ArithmeticException e) {
|
||||
t.editMessage(String.format("Error getting price of %s!", crypto)).queue();
|
||||
|
||||
+2
-1
@@ -1,4 +1,4 @@
|
||||
package netneutrality.command;
|
||||
package netneutrality.core;
|
||||
|
||||
/*
|
||||
* Copyright (c) anthorob
|
||||
@@ -7,6 +7,7 @@ package netneutrality.command;
|
||||
* https://discord.gg/CdFcyQ
|
||||
*/
|
||||
|
||||
import netneutrality.command.AbstractCommand;
|
||||
import org.reflections.Reflections;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -12,7 +12,7 @@ import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import netneutrality.command.AbstractCommand;
|
||||
import netneutrality.command.CommandManager;
|
||||
import netneutrality.core.CommandManager;
|
||||
import netneutrality.core.GuildManager;
|
||||
import netneutrality.core.context.CommandContext;
|
||||
import netneutrality.utils.Settings;
|
||||
|
||||
@@ -17,6 +17,9 @@ public class WebModule {
|
||||
private final String url;
|
||||
private final OkHttpClient client = new OkHttpClient();
|
||||
private Response response;
|
||||
//private final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 OPR/82.0.4227.43";
|
||||
private final String userAgent = "NetNeutrality/1.0 (Discord bot, [email protected])";
|
||||
|
||||
|
||||
public WebModule(String url) {
|
||||
this.url = url;
|
||||
@@ -31,7 +34,7 @@ public class WebModule {
|
||||
public WebModule request() {
|
||||
Sentry.addBreadcrumb(Breadcrumb.http(url, "get"));
|
||||
Request request = new Request.Builder().url(this.url)
|
||||
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 OPR/82.0.4227.43")
|
||||
.addHeader("User-Agent", userAgent)
|
||||
.build();
|
||||
try {
|
||||
response = client.newCall(request).execute();
|
||||
@@ -46,7 +49,7 @@ public class WebModule {
|
||||
public WebModule request(Map<String, String> headers) throws IOException {
|
||||
Sentry.addBreadcrumb(Breadcrumb.http(url, "get"));
|
||||
Request.Builder builder = new Request.Builder().url(this.url)
|
||||
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
|
||||
.addHeader("User-Agent", userAgent);
|
||||
|
||||
for (Map.Entry<String, String> header : headers.entrySet()) {
|
||||
builder.addHeader(header.getKey(), header.getValue());
|
||||
@@ -68,7 +71,7 @@ public class WebModule {
|
||||
Sentry.addBreadcrumb(Breadcrumb.http(url, "post"));
|
||||
RequestBody body = RequestBody.create(createJson(params), MediaType.parse("application/json; charset=utf-8"));
|
||||
Request.Builder builder = new Request.Builder().url(this.url)
|
||||
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
|
||||
.addHeader("User-Agent", userAgent);
|
||||
|
||||
for (Map.Entry<String, String> header : headers.entrySet()) {
|
||||
builder.addHeader(header.getKey(), header.getValue());
|
||||
|
||||
Reference in new issue
Block a user