82 lines
2.8 KiB
Java
82 lines
2.8 KiB
Java
|
package moe.oko.Kiafumi.command;
|
||
|
|
||
|
import moe.oko.Kiafumi.util.CommandInfo;
|
||
|
import moe.oko.Kiafumi.util.CommandType;
|
||
|
import moe.oko.Kiafumi.util.EmbedUI;
|
||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||
|
import org.apache.http.HttpEntity;
|
||
|
import org.apache.http.client.ClientProtocolException;
|
||
|
import org.apache.http.client.ResponseHandler;
|
||
|
import org.apache.http.client.methods.HttpGet;
|
||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||
|
import org.apache.http.impl.client.HttpClients;
|
||
|
import org.apache.http.util.EntityUtils;
|
||
|
import org.json.JSONArray;
|
||
|
import org.json.JSONObject;
|
||
|
|
||
|
import java.net.URI;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
import static moe.oko.Kiafumi.Kiafumi.error;
|
||
|
|
||
|
/**
|
||
|
* Mrow :3
|
||
|
*/
|
||
|
public class CatCommand extends CommandClass{
|
||
|
|
||
|
private final URI catUri = URI.create("https://api.thecatapi.com/v1/images/search");
|
||
|
|
||
|
@Override
|
||
|
public boolean isEnabled() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getName() {
|
||
|
return "Cat";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void newCommand(String name, SlashCommandInteractionEvent e) {
|
||
|
switch (name) {
|
||
|
case "cat":
|
||
|
e.deferReply().queue();
|
||
|
|
||
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||
|
HttpGet httpGet = new HttpGet(catUri);
|
||
|
ResponseHandler<String> responseHandler = response -> {
|
||
|
int status = response.getStatusLine().getStatusCode();
|
||
|
if (status >= 200 && status < 300) {
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
return entity != null ? EntityUtils.toString(entity) : null;
|
||
|
} else {
|
||
|
throw new ClientProtocolException("Unexpected response status: " + status);
|
||
|
}
|
||
|
};
|
||
|
try {
|
||
|
String responseBody = httpClient.execute(httpGet, responseHandler);
|
||
|
JSONArray array = new JSONArray(responseBody);
|
||
|
JSONObject obj = array.getJSONObject(0);
|
||
|
EmbedBuilder eb = new EmbedBuilder()
|
||
|
.setColor(EmbedUI.INFO)
|
||
|
.setTitle("mrow")
|
||
|
.setImage(obj.getString("url"))
|
||
|
.setFooter(EmbedUI.BRAND);
|
||
|
e.getHook().sendMessageEmbeds(eb.build()).queue();
|
||
|
} catch (Exception ex) {
|
||
|
ex.printStackTrace();
|
||
|
error("It brokie...");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public List<CommandInfo> getSlashCommandInfo() {
|
||
|
List<CommandInfo> cil = new ArrayList<>();
|
||
|
cil.add(new CommandInfo("cat", "Provides a random cat!", CommandType.COMMAND));
|
||
|
return cil;
|
||
|
}
|
||
|
}
|