Skip to content

Instantly share code, notes, and snippets.

@fpigerre
Last active December 22, 2015 02:29
Show Gist options
  • Select an option

  • Save fpigerre/6403736 to your computer and use it in GitHub Desktop.

Select an option

Save fpigerre/6403736 to your computer and use it in GitHub Desktop.
!stats command for HukyIRC IRC bot.
package com.huskehhh.code.commands;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.MessageEvent;
public class StatsCommand extends ListenerAdapter {
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonKills = "kills";
String jsonDeaths = "deaths";
String jsonFfa = "ffa_wins";
String jsonInfection = "infection_wins";
String jsonGames = "games_played";
JSONObject stats = new JSONObject(jsonKills + jsonDeaths + jsonFfa + jsonInfection + jsonGames);
return stats;
} finally {
is.close();
}
}
public void onMessage(MessageEvent event) throws IOException, JSONException {
String[] line = event.getMessage().split(" ");
if (event.getMessage().startsWith("!stats ")) {
if (line[1] != null) {
String target = line[1];
try {
JSONObject json = readJsonFromUrl("http://oresomecraft.net/battleapi.php?name=" + target);
event.respond(target + " has:");
event.respond(json.getString("kills") + " Kills");
event.respond(json.getString("deaths") + " Deaths");
event.respond(json.getString("ffa_wins") + " FFA Wins");
event.respond(json.getString("infection_wins") + " Infection Wins");
event.respond("and has played " + json.getString("games_played") + " games!");
} catch (Exception e) {
event.respond("An error has occured!");
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment