add wikipedia and urban

i thought it would be hard but its acutally VERY VERY easy
This commit is contained in:
ChomeNS 2023-03-25 14:46:11 +07:00
parent 94b7fe8ec8
commit a7e8d79af8
5 changed files with 195 additions and 1 deletions

View file

@ -0,0 +1,84 @@
package me.chayapak1.chomens_bot.commands;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.util.HttpUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class UrbanCommand implements Command {
public String name() { return "urban"; }
public String description() {
return "Urban Dictionary in Minecraft";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<{term}>");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("");
return aliases;
}
public int trustLevel() {
return 0;
}
public Component execute (CommandContext context, String[] args, String[] fullArgs) {
final String term = String.join(" ", args);
final Gson gson = new Gson();
try {
final URL url = new URL(
"https://api.urbandictionary.com/v0/define?term=" +
URLEncoder.encode(term, StandardCharsets.UTF_8)
);
final String jsonOutput = HttpUtilities.request(url);
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
final JsonArray list = jsonObject.getAsJsonArray("list");
if (list.size() == 0) return Component.text("No results found").color(NamedTextColor.RED);
for (JsonElement element : list) {
final JsonObject definitionObject = element.getAsJsonObject();
final String word = definitionObject.get("word").getAsString();
final String definition = definitionObject.get("definition").getAsString();
final Component component = Component.translatable(
"[%s] %s - %s",
Component.text("Urban").color(NamedTextColor.RED),
Component.text(word).color(NamedTextColor.GRAY),
Component.text(definition).color(NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY);
context.sendOutput(component);
}
} catch (Exception e) {
return Component.text(e.toString()).color(NamedTextColor.RED);
}
return Component.text("success");
}
}

View file

@ -0,0 +1,72 @@
package me.chayapak1.chomens_bot.commands;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.util.HttpUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class WikipediaCommand implements Command {
public String name() { return "wikipedia"; }
public String description() {
return "Wikipedia in Minecraft";
}
public List<String> usage() {
final List<String> usages = new ArrayList<>();
usages.add("<{page}>");
return usages;
}
public List<String> alias() {
final List<String> aliases = new ArrayList<>();
aliases.add("wiki");
return aliases;
}
public int trustLevel() {
return 0;
}
public Component execute (CommandContext context, String[] args, String[] fullArgs) {
final String page = String.join(" ", args);
final Gson gson = new Gson();
try {
final URL url = new URL(
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
URLEncoder.encode(
page.replace(" ", "_"), // mabe.
StandardCharsets.UTF_8
)
);
final String jsonOutput = HttpUtilities.request(url);
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
context.sendOutput(
Component.text(jsonObject.get("extract").getAsString()).color(NamedTextColor.GREEN)
);
} catch (FileNotFoundException ignored) {
return Component.text("Cannot find page: " + page).color(NamedTextColor.RED);
} catch (Exception e) {
return Component.text(e.toString()).color(NamedTextColor.RED);
}
return Component.text("success");
}
}

View file

@ -33,6 +33,8 @@ public class CommandHandlerPlugin {
registerCommand(new TPSBarCommand()); registerCommand(new TPSBarCommand());
registerCommand(new NetMessageCommand()); registerCommand(new NetMessageCommand());
registerCommand(new RefillCoreCommand()); registerCommand(new RefillCoreCommand());
registerCommand(new WikipediaCommand());
registerCommand(new UrbanCommand());
} }
public void registerCommand (Command command) { public void registerCommand (Command command) {

View file

@ -13,7 +13,7 @@ import java.security.SecureRandom;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
public class DownloadUtilities { public class DownloadUtilities {
private static class DefaultTrustManager implements X509TrustManager { public static class DefaultTrustManager implements X509TrustManager {
@Override @Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {} public void checkClientTrusted(X509Certificate[] arg0, String arg1) {}

View file

@ -0,0 +1,36 @@
package me.chayapak1.chomens_bot.util;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class HttpUtilities {
public static String request (URL url) throws NoSuchAlgorithmException, KeyManagementException, IOException {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] {new DownloadUtilities.DefaultTrustManager()}, new SecureRandom());
SSLContext.setDefault(ctx);
URLConnection conn = url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
// https://www.whatismybrowser.com/guides/the-latest-user-agent/windows
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
}
}