This commit is contained in:
Tyler Murphy 2022-08-23 11:41:32 -04:00
parent 0fa1a8f455
commit 4ef55c0912
4 changed files with 126 additions and 2 deletions

View file

@ -51,8 +51,7 @@ public abstract class Request {
connection.setDoInput(true); connection.setDoInput(true);
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setRequestMethod(method.toString()); connection.setRequestMethod(method.toString());
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0");
for(int i=0; i+1<headers.size(); i+=2) { for(int i=0; i+1<headers.size(); i+=2) {
connection.addRequestProperty(headers.get(i), headers.get(i+1)); connection.addRequestProperty(headers.get(i), headers.get(i+1));
} }

View file

@ -84,6 +84,8 @@ public class Help {
.appendDescription("**/rule34 <query> <page>** Searches on rule34\n") .appendDescription("**/rule34 <query> <page>** Searches on rule34\n")
.appendDescription("**/gelbooru <query> <page>** Searches on gelbooru\n") .appendDescription("**/gelbooru <query> <page>** Searches on gelbooru\n")
.appendDescription("**/danbooru <query> <page>** Searches on danbooru\n") .appendDescription("**/danbooru <query> <page>** Searches on danbooru\n")
.appendDescription("**/konachan <query> <page>** Searches on konachan\n")
.appendDescription("**/yandere <query> <page>** Searches on yande.re\n")
.appendDescription("**/e621 <query> <page>** Searches on e621\n") .appendDescription("**/e621 <query> <page>** Searches on e621\n")
.appendDescription("**/nhentai <query>** Searches on nhentai\n") .appendDescription("**/nhentai <query>** Searches on nhentai\n")
.setFooter("Page "+page+"/"+PAGES) .setFooter("Page "+page+"/"+PAGES)

View file

@ -0,0 +1,62 @@
package net.tylermurphy.ken.command.nsfw;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken;
import net.tylermurphy.ken.api.HTTPMethod;
import net.tylermurphy.ken.api.XmlRequest;
import net.tylermurphy.ken.command.Response;
import net.tylermurphy.ken.util.Command;
import net.tylermurphy.ken.util.Option;
import org.json.JSONException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.List;
public class Konachan {
@Command(name="konachan",description="Searches for an image off of konachan")
@Option(name="query",description="Search query for konachan",type= OptionType.STRING,required=true)
@Option(name="page",description="Page number for konachan",type= OptionType.INTEGER)
public Response execute(GuildMessageChannel channel, List<Object> args){
if(!(channel instanceof TextChannel)) {
return Response.error("This command can only be used in a text channel");
}
TextChannel textChannel = (TextChannel) channel;
if(!textChannel.isNSFW()){
return Response.error("This command can only be used in an NSFW channel");
}
int page = args.size() > 1 ? (int)args.get(1) : 1;
String url = request((String)args.get(0), page);
if(url == null){
return Response.error("Unable to find post with search: "+args.get(0));
} else {
if(url.endsWith(".mp4") || url.endsWith(".webm"))
return Response.success(url);
return Response.success(Ken.getInstance().getDefaultEmbed().setImage(url).build());
}
}
private String request(String query, int page) {
Document doc = (Document) new XmlRequest()
.setURL("https://konachan.com/post.xml?limit=100&tags="+query+"&page="+page)
.setType(HTTPMethod.GET)
.request();
try {
NodeList nList = doc.getElementsByTagName("post");
int choice = (int) (Math.random() * nList.getLength() - 1);
if (choice < 0) return null;
Node node = nList.item(choice);
Element element = (Element) node;
return element.getAttribute("file_url");
} catch (JSONException e) {
return null;
}
}
}

View file

@ -0,0 +1,61 @@
package net.tylermurphy.ken.command.nsfw;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken;
import net.tylermurphy.ken.api.HTTPMethod;
import net.tylermurphy.ken.api.XmlRequest;
import net.tylermurphy.ken.command.Response;
import net.tylermurphy.ken.util.Command;
import net.tylermurphy.ken.util.Option;
import org.json.JSONException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.List;
public class Yandere {
@Command(name="yandere",description="Searches for an image off of yande.re")
@Option(name="query",description="Search query for yande.re",type= OptionType.STRING,required=true)
@Option(name="page",description="Page number for yande.re",type= OptionType.INTEGER)
public Response execute(GuildMessageChannel channel, List<Object> args){
if(!(channel instanceof TextChannel)) {
return Response.error("This command can only be used in a text channel");
}
TextChannel textChannel = (TextChannel) channel;
if(!textChannel.isNSFW()){
return Response.error("This command can only be used in an NSFW channel");
}
int page = args.size() > 1 ? (int)args.get(1) : 1;
String url = request((String)args.get(0), page);
if(url == null){
return Response.error("Unable to find post with search: "+args.get(0));
} else {
if(url.endsWith(".mp4") || url.endsWith(".webm"))
return Response.success(url);
return Response.success(Ken.getInstance().getDefaultEmbed().setImage(url).build());
}
}
private String request(String query, int page){
Document document = (Document) new XmlRequest()
.setURL("https://yande.re/post.xml?limit=100&tags="+query+"&page="+page)
.setType(HTTPMethod.GET)
.request();
try {
NodeList nList = document.getElementsByTagName("post");
int choice = (int) (Math.random() * nList.getLength() - 1);
if (choice < 0) return null;
Node node = nList.item(choice);
Element element = (Element) node;
return element.getAttribute("file_url");
} catch (JSONException e) {
return null;
}
}
}