Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static void initialize() throws LoginException {
new VerifyCommand(),
new PollCommand(),
new IdeaCommand(),
new StoreCommand(),
//new ChannelMuteCommand(),
// statsbot
new StatsCommand(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.diamondfire.helpbot.bot.command.impl.other;

import com.diamondfire.helpbot.bot.command.argument.ArgumentSet;
import com.diamondfire.helpbot.bot.command.help.*;
import com.diamondfire.helpbot.bot.command.impl.Command;
import com.diamondfire.helpbot.bot.command.permissions.Permission;
import com.diamondfire.helpbot.bot.command.reply.PresetBuilder;
import com.diamondfire.helpbot.bot.command.reply.feature.informative.*;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import com.diamondfire.helpbot.util.WebUtil;
import com.google.gson.*;
import net.dv8tion.jda.api.EmbedBuilder;

import java.util.*;


public class StoreCommand extends Command {

@Override
public String getName() {
return "store";
}

@Override
public String[] getAliases() {
return new String[]{"shop"};
}

@Override
public HelpContext getHelpContext() {
return new HelpContext()
.description("Shows all items, recent purchases and sales (if there is one) from store.mcdiamondfire.com.")
.category(CommandCategory.OTHER);
}

@Override
protected ArgumentSet compileArguments() {
return new ArgumentSet();
}

@Override
public Permission getPermission() {
return Permission.USER;
}

@Override
public void run(CommandEvent event) {
EmbedBuilder builder = new EmbedBuilder();
try{
JsonObject json = WebUtil.getJson("https://df.vatten.dev/store/").getAsJsonObject();
builder.setTitle("<:diamondfire:867472383098486794> DiamondFire Store <:diamondfire:867472383098486794>", "https://store.mcdiamondfire.com/");
boolean sale = json.has("sale");
if(sale) builder.setDescription("\n<a:boostx3:809172442496630815> **SALE! " + (int) (json.get("sale").getAsFloat() * 100) + "% OFF!** <a:boostx3:809172442496630815>\n");

//lazy reorganizing
JsonObject items = new JsonObject();
for(String category : new String[]{"Ranks", "Plots", "Boosters"}) {
items.add(category, json.getAsJsonObject("items").getAsJsonArray(category.toLowerCase()));
}

HashMap<String, String> emotes = new HashMap<>();
emotes.put("Ranks", "<a:overlord:867780069653348402>");
emotes.put("Plots", "<:location:789274944923369483>");
emotes.put("Boosters", "<:potion:789274945162575912>");

for(Map.Entry<String, JsonElement> category : items.entrySet()) {
ArrayList<String> fieldval = new ArrayList<>();
for(JsonElement item : category.getValue().getAsJsonArray()) {
String name = item.getAsJsonObject().get("name").getAsString();
String original_price = item.getAsJsonObject().get("original-price").getAsString();
String discount_price = sale ? item.getAsJsonObject().get("discount-price").getAsString() : null;
fieldval.add("**" + name + "**: " + (sale ? "~~$" + original_price + "~~ <a:boostx2:809172442957217812> $" + discount_price : "$" + original_price));
}
builder.addField(emotes.get(category.getKey()) + " " + category.getKey(), String.join("\n", fieldval) + "\n\u200b", false);
}

ArrayList<String> fieldval = new ArrayList<>();
for(JsonElement purchase : json.getAsJsonArray("recent-purchases")) {
fieldval.add(" **" + purchase.getAsJsonObject().get("player").getAsString() + "** bought **" + purchase.getAsJsonObject().get("item").getAsString() + "**");
}
builder.addField(":partying_face: Recent Purchases", String.join("\n", fieldval), false);

event.getChannel().sendMessage(builder.build()).queue();
}catch(Exception e) {
event.reply(new PresetBuilder().withPreset(new InformativeReply(InformativeReplyType.ERROR, "Failed to retrieve store items.")));
}
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/diamondfire/helpbot/util/WebUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.diamondfire.helpbot.util;

import com.google.gson.*;

import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.*;
import java.security.cert.X509Certificate;

//from codeutilities
public class WebUtil {
private static final TrustManager[] trustAllCerts = {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(X509Certificate[] certs, String authType) {
}

}
};

private static final HostnameVerifier allHostsValid = (hostname, session) -> true;

static {
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}

}

public static String getString(String urlToRead, Charset charset) throws IOException {
URL url = new URL(urlToRead);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), charset));
StringBuilder builder = new StringBuilder();
String line;

while ((line = in.readLine()) != null) {
builder.append("\n").append(line);
}
in.close();
return builder.toString();
}

public static String getString(String urlToRead) throws IOException {
return getString(urlToRead, Charset.defaultCharset());
}

public static JsonElement getJson(String url) throws IOException {
return JsonParser.parseString(getString(url));
}

}