Search in sources :

Example 1 with RequestHeader

use of de.foryasee.httprequest.RequestHeader in project Rubicon by Rubicon-Bot.

the class CommandJoke method execute.

@Override
protected Message execute(CommandManager.ParsedCommandInvocation parsedCommandInvocation, UserPermissions userPermissions) {
    HttpRequestBuilder request = new HttpRequestBuilder("https://icanhazdadjoke.com/", RequestType.GET);
    RequestHeader header = new RequestHeader();
    header.addField("Accept", "application/json");
    header.addField("User-Agent", "RubiconBot (https://github.com/Rubicon-Bot/Rubicon)");
    request.setRequestHeader(header);
    try {
        RequestResponse response = request.sendRequest();
        JSONObject json = (JSONObject) new JSONParser().parse(response.getResponseMessage());
        SafeMessage.sendMessage(parsedCommandInvocation.getTextChannel(), new EmbedBuilder().setTitle("Joke").setDescription((String) json.get("joke")).setColor(Colors.COLOR_SECONDARY).build());
    } catch (Exception e) {
        e.printStackTrace();
        SafeMessage.sendMessage(parsedCommandInvocation.getTextChannel(), EmbedUtil.error("Error 404", "No joke was found!").build(), 30);
    }
    return null;
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) JSONObject(org.json.simple.JSONObject) RequestHeader(de.foryasee.httprequest.RequestHeader) JSONParser(org.json.simple.parser.JSONParser) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse)

Aggregations

HttpRequestBuilder (de.foryasee.httprequest.HttpRequestBuilder)1 RequestHeader (de.foryasee.httprequest.RequestHeader)1 RequestResponse (de.foryasee.httprequest.RequestResponse)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1