HidekoBot/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java

89 lines
2.9 KiB
Java
Raw Normal View History

package wtf.beatrice.hidekobot.commands.message;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import org.jetbrains.annotations.Nullable;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
2022-12-20 02:17:27 +01:00
import wtf.beatrice.hidekobot.commands.base.UrbanDictionary;
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
2022-12-20 03:17:32 +01:00
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class UrbanDictionaryCommand implements MessageCommand
{
@Override
public LinkedList<String> getCommandLabels() {
2022-12-20 02:17:27 +01:00
return UrbanDictionary.getCommandLabels();
}
@Nullable
@Override
public List<Permission> getPermissions() {
return null; //anyone can use it
}
@Override
public boolean passRawArgs() {
return false;
}
@Override
public void runCommand(MessageReceivedEvent event, String label, String[] args)
{
if(args.length == 0)
{
2022-12-20 02:17:27 +01:00
event.getMessage().reply(UrbanDictionary.getNoArgsError()).queue();
return;
}
// sanitize args by only keeping letters and numbers, and adding "+" instead of spaces for HTML parsing
StringBuilder termBuilder = new StringBuilder();
2022-12-20 02:17:27 +01:00
for (int i = 0; i < args.length; i++) {
String arg = args[i];
termBuilder.append(arg);
2022-12-20 02:17:27 +01:00
if(i + 1 != args.length) // add spaces between args, but not on the last run
termBuilder.append(" ");
}
2022-12-20 02:17:27 +01:00
String term = UrbanDictionary.sanitizeArgs(termBuilder.toString(), false);
String url = UrbanDictionary.generateUrl(term);
Document doc;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
2022-12-20 02:17:27 +01:00
event.getMessage().reply(UrbanDictionary.getNoTermFoundError()).queue();
return;
}
Elements definitions = doc.getElementsByClass("definition");
2022-12-20 02:17:27 +01:00
UrbanDictionary.UrbanSearch search = new UrbanDictionary.UrbanSearch(definitions);
MessageEmbed embed = UrbanDictionary.buildEmbed(term, url, event.getAuthor(), search, 0);
// disable next page if we only have one result
2022-12-20 02:17:27 +01:00
Button nextPageBtnLocal = UrbanDictionary.getNextPageButton();
if(search.getPages() == 1) nextPageBtnLocal = nextPageBtnLocal.asDisabled();
event.getChannel()
.sendMessageEmbeds(embed)
2022-12-20 02:17:27 +01:00
.addActionRow(UrbanDictionary.getPreviousPageButton().asDisabled(),
//disabled by default because we're on page 0
nextPageBtnLocal,
2022-12-20 02:17:27 +01:00
UrbanDictionary.getDeleteButton())
.queue(message -> UrbanDictionary.track(message, event.getAuthor(), search, term));
}
}