2022-12-19 21:42:55 +01:00
|
|
|
package wtf.beatrice.hidekobot.commands.message;
|
|
|
|
|
|
|
|
import net.dv8tion.jda.api.EmbedBuilder;
|
|
|
|
import net.dv8tion.jda.api.Permission;
|
2022-12-20 00:34:49 +01:00
|
|
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
|
|
|
import net.dv8tion.jda.api.entities.User;
|
|
|
|
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
|
|
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
2022-12-19 21:42:55 +01:00
|
|
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
2022-12-20 00:34:49 +01:00
|
|
|
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
|
|
|
import net.dv8tion.jda.api.interactions.components.ItemComponent;
|
|
|
|
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
|
|
|
import org.apache.commons.lang3.SerializationException;
|
|
|
|
import org.apache.commons.text.StringEscapeUtils;
|
2022-12-19 21:42:55 +01:00
|
|
|
import org.apache.commons.text.WordUtils;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
import wtf.beatrice.hidekobot.Cache;
|
2022-12-20 00:34:49 +01:00
|
|
|
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
|
2022-12-19 21:42:55 +01:00
|
|
|
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
|
|
|
|
2022-12-20 00:34:49 +01:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
2022-12-19 21:42:55 +01:00
|
|
|
|
|
|
|
public class UrbanDictionaryCommand implements MessageCommand
|
|
|
|
{
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public LinkedList<String> getCommandLabels() {
|
|
|
|
return new LinkedList<>(Arrays.asList("urban", "urbandictionary", "ud"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public List<Permission> getPermissions() {
|
|
|
|
return null; //anyone can use it
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean passRawArgs() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-20 00:34:49 +01:00
|
|
|
static final String baseUrl = "https://www.urbandictionary.com/define.php?term=";
|
|
|
|
static final Button previousPageButton = Button.primary("urban_previouspage", "Back")
|
|
|
|
.withEmoji(Emoji.fromFormatted("⬅️"));
|
|
|
|
static final Button nextPageButton = Button.primary("urban_nextpage", "Next")
|
|
|
|
.withEmoji(Emoji.fromFormatted("➡️"));
|
|
|
|
|
|
|
|
private static MessageEmbed buildEmbed(String term,
|
|
|
|
String url,
|
|
|
|
User author,
|
|
|
|
String meaning,
|
|
|
|
String example,
|
|
|
|
String contributor,
|
|
|
|
String date,
|
|
|
|
int page)
|
|
|
|
{
|
|
|
|
EmbedBuilder embedBuilder = new EmbedBuilder();
|
|
|
|
embedBuilder.setColor(Cache.getBotColor());
|
|
|
|
embedBuilder.setTitle(term + ", on Urban Dictionary", url);
|
|
|
|
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
|
|
|
|
embedBuilder.addField("Definition", meaning, false);
|
|
|
|
embedBuilder.addField("Example", example, false);
|
|
|
|
embedBuilder.addField("Submission",
|
|
|
|
"*Entry " + (page+1) + " | Sent by " + contributor + " on " + date + "*",
|
|
|
|
false);
|
|
|
|
|
|
|
|
return embedBuilder.build();
|
|
|
|
}
|
|
|
|
|
2022-12-19 21:42:55 +01:00
|
|
|
@Override
|
|
|
|
public void runCommand(MessageReceivedEvent event, String label, String[] args)
|
|
|
|
{
|
|
|
|
if(args.length == 0)
|
|
|
|
{
|
|
|
|
event.getMessage().reply("\uD83D\uDE22 I need to know what to search for!").queue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sanitize args by only keeping letters and numbers, and adding "+" instead of spaces for HTML parsing
|
|
|
|
StringBuilder termBuilder = new StringBuilder();
|
|
|
|
for(int pos = 0; pos < args.length; pos++)
|
|
|
|
{
|
|
|
|
String arg = args[pos];
|
|
|
|
|
|
|
|
arg = arg.replaceAll("[^\\d\\w]", "");
|
|
|
|
termBuilder.append(arg);
|
|
|
|
|
|
|
|
if(pos + 1 != args.length)
|
|
|
|
termBuilder.append("+"); // add everywhere except on last iteration
|
|
|
|
}
|
|
|
|
|
|
|
|
String term = termBuilder.toString();
|
|
|
|
|
|
|
|
// cut it to length to avoid abuse
|
|
|
|
if (term.length() > 64) term = term.substring(0, 64);
|
|
|
|
|
2022-12-20 00:34:49 +01:00
|
|
|
String url = baseUrl + term;
|
2022-12-19 21:42:55 +01:00
|
|
|
|
2022-12-20 00:34:49 +01:00
|
|
|
Document doc;
|
2022-12-19 21:42:55 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
doc = Jsoup.connect(url).get();
|
|
|
|
} catch (IOException e) {
|
|
|
|
event.getMessage().reply("\uD83D\uDE22 I couldn't find that term!").queue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-19 22:45:02 +01:00
|
|
|
List<String> plaintextMeanings = new ArrayList<>();
|
|
|
|
List<String> plaintextExamples = new ArrayList<>();
|
2022-12-20 00:34:49 +01:00
|
|
|
List<String> contributorsNames = new ArrayList<>();
|
|
|
|
List<String> submissionDates = new ArrayList<>();
|
2022-12-19 21:42:55 +01:00
|
|
|
|
|
|
|
Elements definitions = doc.getElementsByClass("definition");
|
2022-12-19 22:45:02 +01:00
|
|
|
for(Element definition : definitions)
|
2022-12-19 21:42:55 +01:00
|
|
|
{
|
2022-12-19 22:45:02 +01:00
|
|
|
Elements meaningSingleton = definition.getElementsByClass("meaning");
|
|
|
|
if(meaningSingleton.isEmpty())
|
2022-12-19 21:42:55 +01:00
|
|
|
{
|
2022-12-19 22:45:02 +01:00
|
|
|
plaintextMeanings.add(" ");
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
Element meaning = meaningSingleton.get(0);
|
|
|
|
String text = meaning.html()
|
|
|
|
.replaceAll("<br\\s*?>", "\n") // keep newlines
|
|
|
|
.replaceAll("<.*?>", ""); // remove all other html tags
|
2022-12-20 00:34:49 +01:00
|
|
|
// this is used to fix eg. & being shown literally instead of being parsed
|
|
|
|
text = StringEscapeUtils.unescapeHtml4(text);
|
2022-12-19 22:45:02 +01:00
|
|
|
// discord only allows 1024 characters for embed fields
|
2022-12-19 22:53:25 +01:00
|
|
|
if(text.length() > 1024) text = text.substring(0, 1020) + "...";
|
2022-12-19 22:45:02 +01:00
|
|
|
plaintextMeanings.add(text);
|
2022-12-19 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 22:45:02 +01:00
|
|
|
Elements exampleSingleton = definition.getElementsByClass("example");
|
|
|
|
|
|
|
|
if(exampleSingleton.isEmpty())
|
2022-12-19 21:42:55 +01:00
|
|
|
{
|
2022-12-19 22:45:02 +01:00
|
|
|
plaintextExamples.add(" ");
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
Element example = exampleSingleton.get(0);
|
|
|
|
String text = example.html()
|
|
|
|
.replaceAll("<br\\s*?>", "\n") // keep newlines
|
|
|
|
.replaceAll("<.*?>", ""); // remove all other html tags
|
2022-12-20 00:34:49 +01:00
|
|
|
// this is used to fix eg. & being shown literally instead of being parsed
|
|
|
|
text = StringEscapeUtils.unescapeHtml4(text);
|
2022-12-19 22:45:02 +01:00
|
|
|
// discord only allows 1024 characters for embed fields
|
2022-12-19 22:53:25 +01:00
|
|
|
if(text.length() > 1024) text = text.substring(0, 1020) + "...";
|
2022-12-19 22:45:02 +01:00
|
|
|
plaintextExamples.add(text);
|
2022-12-19 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 22:45:02 +01:00
|
|
|
Elements contributorSingleton = definition.getElementsByClass("contributor");
|
|
|
|
if(contributorSingleton.isEmpty())
|
|
|
|
{
|
|
|
|
contributorsNames.add("Unknown");
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
Element contributor = contributorSingleton.get(0);
|
2022-12-19 21:42:55 +01:00
|
|
|
|
2022-12-19 22:45:02 +01:00
|
|
|
String htmlContributor = contributor.html();
|
|
|
|
String htmlContributorName = contributor.select("a").html();
|
|
|
|
String htmlSubmitDate = htmlContributor.substring(
|
|
|
|
htmlContributor.indexOf("</a>") + 4);
|
2022-12-19 21:42:55 +01:00
|
|
|
|
2022-12-19 22:45:02 +01:00
|
|
|
contributorsNames.add(htmlContributorName
|
|
|
|
.replaceAll("<.*?>", "")); // remove all html tags;
|
|
|
|
|
|
|
|
submissionDates.add(htmlSubmitDate
|
|
|
|
.replaceAll("<.*?>", "")); // remove all html tags;
|
|
|
|
}
|
2022-12-19 21:42:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// make it nice to look at, compared to the html value
|
|
|
|
term = term.replaceAll("\\+", " ");
|
|
|
|
term = WordUtils.capitalizeFully(term);
|
|
|
|
|
|
|
|
|
2022-12-20 00:34:49 +01:00
|
|
|
|
|
|
|
String serializedMeanings = serialize(plaintextMeanings);
|
|
|
|
String serializedExamples = serialize(plaintextExamples);
|
|
|
|
String serializedContributors = serialize(contributorsNames);
|
|
|
|
String serializedDates = serialize(submissionDates);
|
|
|
|
|
|
|
|
// disable next page if we only have one result
|
|
|
|
Button nextPageBtnLocal = nextPageButton;
|
|
|
|
if(submissionDates.size() == 1) nextPageBtnLocal = nextPageBtnLocal.asDisabled();
|
|
|
|
|
|
|
|
MessageEmbed embed = buildEmbed(term, url, event.getAuthor(), plaintextMeanings.get(0),
|
|
|
|
plaintextExamples.get(0), contributorsNames.get(0), submissionDates.get(0), 0);
|
|
|
|
|
|
|
|
// copy term for async thing
|
|
|
|
final String finalTerm = term;
|
|
|
|
event.getChannel()
|
|
|
|
.sendMessageEmbeds(embed)
|
|
|
|
.addActionRow(previousPageButton.asDisabled(), //disabled by default because we're on page 0
|
|
|
|
nextPageBtnLocal)
|
|
|
|
.queue(message ->
|
|
|
|
{
|
|
|
|
|
|
|
|
Cache.getDatabaseSource().queueDisabling(message);
|
|
|
|
Cache.getDatabaseSource().trackRanCommandReply(message, event.getAuthor());
|
|
|
|
Cache.getDatabaseSource().trackUrban(serializedMeanings,
|
|
|
|
serializedExamples,
|
|
|
|
serializedContributors,
|
|
|
|
serializedDates,
|
|
|
|
message,
|
|
|
|
finalTerm);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private String serialize(List dataList) {
|
|
|
|
|
|
|
|
try (ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
|
|
|
ObjectOutputStream so = new ObjectOutputStream(bo)) {
|
|
|
|
so.writeObject(dataList);
|
|
|
|
so.flush();
|
|
|
|
return Base64.getEncoder().encodeToString(bo.toByteArray());
|
|
|
|
}
|
|
|
|
catch (IOException ignored) {}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ArrayList deserialize(String dataStr) {
|
|
|
|
|
|
|
|
byte[] b = Base64.getDecoder().decode(dataStr);
|
|
|
|
ByteArrayInputStream bi = new ByteArrayInputStream(b);
|
|
|
|
ObjectInputStream si;
|
|
|
|
try {
|
|
|
|
si = new ObjectInputStream(bi);
|
|
|
|
return ArrayList.class.cast(si.readObject());
|
|
|
|
}
|
|
|
|
catch (IOException | ClassNotFoundException e) {
|
|
|
|
throw new SerializationException("Error during deserialization", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void changePage(ButtonInteractionEvent event, boolean increase)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
String messageId = event.getMessageId();
|
|
|
|
DatabaseSource database = Cache.getDatabaseSource();
|
|
|
|
|
|
|
|
// check if the user interacting is the same one who ran the command
|
|
|
|
if (!(database.isUserTrackedFor(event.getUser().getId(), messageId))) {
|
|
|
|
event.reply("❌ You did not run this command!").setEphemeral(true).queue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get current page and calculate how many pages there are
|
|
|
|
int page = Cache.getDatabaseSource().getUrbanPage(messageId);
|
|
|
|
int pages;
|
|
|
|
|
|
|
|
|
|
|
|
String serializedMeanings = database.getUrbanMeanings(messageId);
|
|
|
|
List<String> meanings = deserialize(serializedMeanings);
|
|
|
|
String serializedExamples = database.getUrbanExamples(messageId);
|
|
|
|
List<String> examples = deserialize(serializedExamples);
|
|
|
|
String serializedContributors = database.getUrbanContributors(messageId);
|
|
|
|
List<String> contributors = deserialize(serializedContributors);
|
|
|
|
String serializedDates = database.getUrbanDates(messageId);
|
|
|
|
List<String> dates = deserialize(serializedDates);
|
|
|
|
String term = database.getUrbanTerm(messageId);
|
|
|
|
String url = baseUrl + term;
|
|
|
|
|
|
|
|
// count how many pages there are
|
|
|
|
pages = meanings.size();
|
|
|
|
|
|
|
|
// move to new page
|
|
|
|
if(increase)
|
|
|
|
page++;
|
|
|
|
else page--;
|
|
|
|
|
|
|
|
MessageEmbed updatedEmbed = buildEmbed(term, url, event.getUser(),
|
|
|
|
meanings.get(page), examples.get(page), contributors.get(page),
|
|
|
|
dates.get(page), page);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ItemComponent> components = new ArrayList<>();
|
|
|
|
|
|
|
|
if(page > 0)
|
|
|
|
{
|
|
|
|
components.add(previousPageButton.asEnabled());
|
|
|
|
} else {
|
|
|
|
components.add(previousPageButton.asDisabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(page + 1 == pages)
|
|
|
|
{
|
|
|
|
components.add(nextPageButton.asDisabled());
|
|
|
|
} else {
|
|
|
|
components.add(nextPageButton.asEnabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionRow currentRow = ActionRow.of(components);
|
|
|
|
List<ActionRow> actionRows = new ArrayList<>(Collections.singletonList(currentRow));
|
|
|
|
|
|
|
|
event.getMessage().editMessageEmbeds(updatedEmbed).complete();
|
|
|
|
event.editComponents(actionRows).complete();
|
|
|
|
database.setUrbanPage(messageId, page);
|
|
|
|
database.resetExpiryTimestamp(messageId);
|
|
|
|
|
2022-12-19 21:42:55 +01:00
|
|
|
}
|
|
|
|
}
|