Create global crypto command
This commit is contained in:
1 parent
2a1c7c4f3b
commit
696d989f7b
3 files changed
+115
-6
No files matched your search
@@ -0,0 +1,95 @@
|
|||||||
|
package netneutrality.command.crypto;
|
||||||
|
|
||||||
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
import netneutrality.command.AbstractCommand;
|
||||||
|
import netneutrality.command.CommandType;
|
||||||
|
import netneutrality.command.Visibility;
|
||||||
|
import netneutrality.http.WebModule;
|
||||||
|
import org.json.JSONException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class CryptoPriceCommand extends AbstractCommand {
|
||||||
|
|
||||||
|
private String currency = "USD";
|
||||||
|
private final String baseURL = "https://min-api.cryptocompare.com/data/price";
|
||||||
|
private String price = "";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean needAdmin() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCommand() {
|
||||||
|
return "crypto";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getAlias() {
|
||||||
|
return new String[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUsage() {
|
||||||
|
return "<crypto> <currency>";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "Get the price of cryptocurrrency";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Visibility getVisibility() {
|
||||||
|
return Visibility.CHANNEL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandType getType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute(MessageReceivedEvent event, String[] args) {
|
||||||
|
|
||||||
|
if (args.length > 0) {
|
||||||
|
|
||||||
|
if (args.length == 2) {
|
||||||
|
if (Currency.isCurrency(args[1].toUpperCase()))
|
||||||
|
this.currency = args[1].toUpperCase();
|
||||||
|
else {
|
||||||
|
event.getTextChannel().sendMessage("Please enter a valid currency").queue();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
event.getTextChannel().sendMessage("Retreiving price, please wait...").queue(t -> {
|
||||||
|
|
||||||
|
try {
|
||||||
|
WebModule ethereumHttp = new WebModule(baseURL + "?fsym=" + args[0] + "&tsyms=" + currency.toUpperCase()).request();
|
||||||
|
price = Double.toString(ethereumHttp.getAsObject().getDouble(currency));
|
||||||
|
|
||||||
|
if (price == null)
|
||||||
|
throw new IllegalArgumentException("Crypto doesn't exist");
|
||||||
|
|
||||||
|
} catch (IOException | JSONException | IllegalArgumentException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
t.editMessage("Error getting ethereum price!").queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
t.editMessage(String.format("The current price of %s in %s is %s$", args[0].toUpperCase(), currency, price)).queue();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ import netneutrality.http.WebModule;
|
|||||||
public class LitecoinPriceCommand extends AbstractCommand {
|
public class LitecoinPriceCommand extends AbstractCommand {
|
||||||
|
|
||||||
private String currency;
|
private String currency;
|
||||||
private final String currencyUrl = "https://api.coinmarketcap.com/v1/ticker/litecoin/?convert=";
|
private final String currencyUrl = "https://min-api.cryptocompare.com/data/price?fsym=LTC&tsyms=";
|
||||||
private String price;
|
private String price;
|
||||||
|
|
||||||
public LitecoinPriceCommand() {
|
public LitecoinPriceCommand() {
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ import java.util.TreeMap;
|
|||||||
|
|
||||||
public class WebModule {
|
public class WebModule {
|
||||||
|
|
||||||
private String url;
|
private final String url;
|
||||||
private OkHttpClient client = new OkHttpClient();
|
private final OkHttpClient client = new OkHttpClient();
|
||||||
private Response response;
|
private Response response;
|
||||||
|
|
||||||
public WebModule(String url) {
|
public WebModule(String url) {
|
||||||
@@ -30,7 +30,7 @@ public class WebModule {
|
|||||||
public WebModule request() {
|
public WebModule request() {
|
||||||
|
|
||||||
Request request = new Request.Builder().url(this.url)
|
Request request = 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", "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")
|
||||||
.build();
|
.build();
|
||||||
try {
|
try {
|
||||||
response = client.newCall(request).execute();
|
response = client.newCall(request).execute();
|
||||||
@@ -72,8 +72,6 @@ public class WebModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String createJson(TreeMap<String, String> params) {
|
public static String createJson(TreeMap<String, String> params) {
|
||||||
|
|
||||||
|
|
||||||
StringBuilder json = new StringBuilder();
|
StringBuilder json = new StringBuilder();
|
||||||
|
|
||||||
for (Map.Entry<String, String> param : params.entrySet()) {
|
for (Map.Entry<String, String> param : params.entrySet()) {
|
||||||
@@ -86,6 +84,21 @@ public class WebModule {
|
|||||||
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "}" : "");
|
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "}" : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String createJsonObject(List<String> key, List<String> value) {
|
||||||
|
if (key.size() != value.size())
|
||||||
|
throw new IllegalArgumentException("Key list size not equal to value list size");
|
||||||
|
|
||||||
|
StringBuilder json = new StringBuilder();
|
||||||
|
|
||||||
|
for (int index = 0; index < key.size();) {
|
||||||
|
json.append(String.format("\"%s\":\"%s\"", key.get(index), value.get(index)));
|
||||||
|
json.append(",");
|
||||||
|
}
|
||||||
|
json.insert(0, "{");
|
||||||
|
|
||||||
|
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "}" : "");
|
||||||
|
}
|
||||||
|
|
||||||
public static String createJsonArray(List<String> list) {
|
public static String createJsonArray(List<String> list) {
|
||||||
StringBuilder json = new StringBuilder();
|
StringBuilder json = new StringBuilder();
|
||||||
|
|
||||||
@@ -111,6 +124,7 @@ public class WebModule {
|
|||||||
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "]" : "[]");
|
return (json.length() > 1 ? json.substring(0, json.length() - 1) + "]" : "[]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the response as a JSONObject
|
* @return the response as a JSONObject
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
|
|||||||
Reference in new issue
Block a user