lazily fix the yfd chomens bot crash exploit
This commit is contained in:
parent
c75de26c38
commit
7cb3164a3c
3 changed files with 83 additions and 70 deletions
src/main/java/land/chipmunk/chayapak/chomens_bot/commands
|
@ -53,39 +53,45 @@ public class TranslateCommand implements Command {
|
||||||
|
|
||||||
final Gson gson = new Gson();
|
final Gson gson = new Gson();
|
||||||
|
|
||||||
try {
|
new Thread(() -> {
|
||||||
final URL url = new URL("https://translate.google.com/translate_a/single?client=at&dt=t&dt=rm&dj=1");
|
try {
|
||||||
|
final URL url = new URL("https://translate.google.com/translate_a/single?client=at&dt=t&dt=rm&dj=1");
|
||||||
|
|
||||||
final String jsonOutput = HttpUtilities.postRequest(
|
final String jsonOutput = HttpUtilities.postRequest(
|
||||||
url,
|
url,
|
||||||
"application/x-www-form-urlencoded;charset=utf-8",
|
"application/x-www-form-urlencoded;charset=utf-8",
|
||||||
String.format(
|
String.format(
|
||||||
"sl=%s&tl=%s&q=%s",
|
"sl=%s&tl=%s&q=%s",
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
URLEncoder.encode(
|
URLEncoder.encode(
|
||||||
message,
|
message,
|
||||||
StandardCharsets.UTF_8
|
StandardCharsets.UTF_8
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
||||||
|
|
||||||
|
final JsonArray sentences = jsonObject.getAsJsonArray("sentences");
|
||||||
|
|
||||||
|
final JsonObject translation = sentences.get(0).getAsJsonObject();
|
||||||
|
|
||||||
|
final String output = translation.get("trans").getAsString();
|
||||||
|
|
||||||
|
context.sendOutput(
|
||||||
|
Component
|
||||||
|
.translatable(
|
||||||
|
"Result: %s",
|
||||||
|
Component.text(output).color(NamedTextColor.GREEN)
|
||||||
)
|
)
|
||||||
)
|
.color(ColorUtilities.getColorByString(bot.config().colorPalette().secondary()))
|
||||||
);
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
return null;
|
||||||
|
|
||||||
final JsonArray sentences = jsonObject.getAsJsonArray("sentences");
|
|
||||||
|
|
||||||
final JsonObject translation = sentences.get(0).getAsJsonObject();
|
|
||||||
|
|
||||||
final String output = translation.get("trans").getAsString();
|
|
||||||
|
|
||||||
return Component
|
|
||||||
.translatable(
|
|
||||||
"Result: %s",
|
|
||||||
Component.text(output).color(NamedTextColor.GREEN)
|
|
||||||
)
|
|
||||||
.color(ColorUtilities.getColorByString(bot.config().colorPalette().secondary()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
return Component.text(e.toString()).color(NamedTextColor.RED);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,38 +46,40 @@ public class UrbanCommand implements Command {
|
||||||
|
|
||||||
final Gson gson = new Gson();
|
final Gson gson = new Gson();
|
||||||
|
|
||||||
try {
|
new Thread(() -> {
|
||||||
final URL url = new URL(
|
try {
|
||||||
"https://api.urbandictionary.com/v0/define?term=" +
|
final URL url = new URL(
|
||||||
URLEncoder.encode(term, StandardCharsets.UTF_8)
|
"https://api.urbandictionary.com/v0/define?term=" +
|
||||||
);
|
URLEncoder.encode(term, StandardCharsets.UTF_8)
|
||||||
|
);
|
||||||
|
|
||||||
final String jsonOutput = HttpUtilities.getRequest(url);
|
final String jsonOutput = HttpUtilities.getRequest(url);
|
||||||
|
|
||||||
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
||||||
|
|
||||||
final JsonArray list = jsonObject.getAsJsonArray("list");
|
final JsonArray list = jsonObject.getAsJsonArray("list");
|
||||||
|
|
||||||
if (list.size() == 0) return Component.text("No results found").color(NamedTextColor.RED);
|
if (list.size() == 0) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED));
|
||||||
|
|
||||||
for (JsonElement element : list) {
|
for (JsonElement element : list) {
|
||||||
final JsonObject definitionObject = element.getAsJsonObject();
|
final JsonObject definitionObject = element.getAsJsonObject();
|
||||||
|
|
||||||
final String word = definitionObject.get("word").getAsString();
|
final String word = definitionObject.get("word").getAsString();
|
||||||
final String definition = definitionObject.get("definition").getAsString();
|
final String definition = definitionObject.get("definition").getAsString();
|
||||||
|
|
||||||
final Component component = Component.translatable(
|
final Component component = Component.translatable(
|
||||||
"[%s] %s - %s",
|
"[%s] %s - %s",
|
||||||
Component.text("Urban").color(NamedTextColor.RED),
|
Component.text("Urban").color(NamedTextColor.RED),
|
||||||
Component.text(word).color(NamedTextColor.GRAY),
|
Component.text(word).color(NamedTextColor.GRAY),
|
||||||
Component.text(definition).color(NamedTextColor.GRAY)
|
Component.text(definition).color(NamedTextColor.GRAY)
|
||||||
).color(NamedTextColor.DARK_GRAY);
|
).color(NamedTextColor.DARK_GRAY);
|
||||||
|
|
||||||
context.sendOutput(component);
|
context.sendOutput(component);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
}).start();
|
||||||
return Component.text(e.toString()).color(NamedTextColor.RED);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,24 +45,29 @@ public class WikipediaCommand implements Command {
|
||||||
|
|
||||||
final Gson gson = new Gson();
|
final Gson gson = new Gson();
|
||||||
|
|
||||||
try {
|
// TODO: mabe use the executor service thingy instead of threads because threads are b a d
|
||||||
final URL url = new URL(
|
new Thread(() -> {
|
||||||
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
|
try {
|
||||||
URLEncoder.encode(
|
final URL url = new URL(
|
||||||
page.replace(" ", "_"), // mabe.
|
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
|
||||||
StandardCharsets.UTF_8
|
URLEncoder.encode(
|
||||||
)
|
page.replace(" ", "_"), // mabe.
|
||||||
);
|
StandardCharsets.UTF_8
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
final String jsonOutput = HttpUtilities.getRequest(url);
|
final String jsonOutput = HttpUtilities.getRequest(url);
|
||||||
|
|
||||||
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
||||||
|
|
||||||
return Component.text(jsonObject.get("extract").getAsString()).color(NamedTextColor.GREEN);
|
context.sendOutput(Component.text(jsonObject.get("extract").getAsString()).color(NamedTextColor.GREEN));
|
||||||
} catch (FileNotFoundException ignored) {
|
} catch (FileNotFoundException ignored) {
|
||||||
return Component.text("Cannot find page: " + page).color(NamedTextColor.RED);
|
context.sendOutput(Component.text("Cannot find page: " + page).color(NamedTextColor.RED));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return Component.text(e.toString()).color(NamedTextColor.RED);
|
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
|
||||||
}
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue