2022-04-01 17:36:19 +00:00
|
|
|
package moe.oko.Kiafumi.command.utility;
|
2022-03-28 16:41:43 +00:00
|
|
|
|
2022-04-01 17:36:19 +00:00
|
|
|
import moe.oko.Kiafumi.command.CommandClass;
|
2022-03-28 16:41:43 +00:00
|
|
|
import moe.oko.Kiafumi.util.CommandInfo;
|
2022-03-29 15:35:38 +00:00
|
|
|
import moe.oko.Kiafumi.util.CommandType;
|
2022-03-29 11:52:52 +00:00
|
|
|
import moe.oko.Kiafumi.util.EmbedUI;
|
2022-03-28 16:41:43 +00:00
|
|
|
import net.dv8tion.jda.api.EmbedBuilder;
|
|
|
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
|
|
|
import org.bitnick.net.duckduckgo.WebSearch;
|
|
|
|
import org.bitnick.net.duckduckgo.entity.SearchResult;
|
|
|
|
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-05-22 05:41:04 +00:00
|
|
|
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
2022-04-04 07:27:20 +00:00
|
|
|
|
2022-04-01 17:59:40 +00:00
|
|
|
/**
|
|
|
|
* Helpful Search Command (Uses DDG API)
|
|
|
|
* @author Kay
|
|
|
|
*/
|
2022-04-01 17:36:19 +00:00
|
|
|
public class DuckCommand extends CommandClass {
|
2022-03-28 16:41:43 +00:00
|
|
|
private boolean enabled = true;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "Duck";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void newCommand(String name, SlashCommandInteractionEvent e) {
|
2022-03-29 11:52:52 +00:00
|
|
|
if ("search".equals(name)) {
|
|
|
|
e.deferReply().queue();
|
|
|
|
String option = e.getOption("query").getAsString();
|
2022-05-20 02:41:32 +00:00
|
|
|
slashLog(e, "with search \"" + option + "\".");
|
2022-03-29 11:52:52 +00:00
|
|
|
WebSearch ws = WebSearch.instanceOf();
|
|
|
|
SearchResult sr;
|
|
|
|
try {
|
|
|
|
sr = ws.instantAnswerSearch(option);
|
|
|
|
} catch (Exception ex) {
|
2022-03-28 16:41:43 +00:00
|
|
|
EmbedBuilder eb = new EmbedBuilder()
|
2022-03-29 11:52:52 +00:00
|
|
|
.setColor(EmbedUI.FAILURE)
|
|
|
|
.setTitle("Query Failed")
|
|
|
|
.setDescription("Couldn't find any results.")
|
|
|
|
.setFooter("Query: " + option)
|
2022-03-28 16:41:43 +00:00
|
|
|
.setTimestamp(ZonedDateTime.now());
|
2022-03-28 17:25:03 +00:00
|
|
|
e.getHook().sendMessageEmbeds(eb.build()).queue();
|
2022-03-29 11:52:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
EmbedBuilder eb = new EmbedBuilder()
|
|
|
|
.setColor(EmbedUI.INFO)
|
|
|
|
.setTitle(sr.getTitle(), sr.getUrl().toString())
|
|
|
|
.setDescription(sr.getDescription())
|
|
|
|
.setFooter("Query: " + option)
|
|
|
|
.setTimestamp(ZonedDateTime.now());
|
|
|
|
e.getHook().sendMessageEmbeds(eb.build()).queue();
|
2022-03-28 16:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<CommandInfo> getSlashCommandInfo() {
|
|
|
|
List<CommandInfo> cil = new ArrayList<>();
|
2022-03-29 15:35:38 +00:00
|
|
|
CommandInfo ci = new CommandInfo("search", "Looks up with DuckDuckGo your query!", CommandType.COMMAND);
|
2022-03-28 17:25:03 +00:00
|
|
|
ci.addOption("query", "The query to be searched", OptionType.STRING, true);
|
|
|
|
cil.add(ci);
|
2022-03-28 16:41:43 +00:00
|
|
|
return cil;
|
|
|
|
}
|
|
|
|
}
|