From b9fd7315fca62b5559a052463126978a59de2c26 Mon Sep 17 00:00:00 2001 From: CavariuX Date: Sun, 31 May 2015 13:55:32 -0500 Subject: [PATCH] v1.3 - Alpha ++Added | --Removed | *Extra ------------------------------------------ ++ Added User methods ++ Added Channel methods --Some lag while using it *Improved channel and User getting *Fixed is Mod --- .gitignore | 1 + src/tk/cavariux/twitchirc/Chat/Channel.java | 75 +++++++++++++--- src/tk/cavariux/twitchirc/Chat/User.java | 90 +++++++++++-------- src/tk/cavariux/twitchirc/Core/TwitchBot.java | 27 ++++-- 4 files changed, 136 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 32858aa..a031ac2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +/bin/ diff --git a/src/tk/cavariux/twitchirc/Chat/Channel.java b/src/tk/cavariux/twitchirc/Chat/Channel.java index d8f752e..2ed2bec 100644 --- a/src/tk/cavariux/twitchirc/Chat/Channel.java +++ b/src/tk/cavariux/twitchirc/Chat/Channel.java @@ -5,6 +5,9 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import tk.cavariux.twitchirc.Core.TwitchBot; import tk.cavariux.twitchirc.Json.JsonArray; @@ -14,13 +17,14 @@ /** * The channel object * @author Leonardo Mariscal - * @version 1.2-alpha + * @version 1.3-alpha */ public class Channel { private String urln = "http://tmi.twitch.tv/group/user/$channel$/chatters"; private String channel; private TwitchBot bot; + private static HashMap channels = new HashMap(); /** * The constructor of the channel object @@ -31,6 +35,21 @@ public Channel (String channel, TwitchBot bot) { this.bot = bot; this.channel = channel; + channels.put(channel, this); + } + + /** + * Get a channel from an existing variable + * @param channel The channel name + * @param bot The bot tha use it + * @return The channel + */ + public static final Channel getChannel(String channel, TwitchBot bot) + { + if (channels.containsKey(channel)) + return channels.get(channel); + else + return new Channel(channel, bot); } /** @@ -157,12 +176,11 @@ public final void unhost() * Get the currently viewers (This method is on beta so it may not be optimized) * @return A String[] with all the current viewers */ - public final String[] getViewers() + public final List getViewers() { URL url; try { url = new URL(urln.replace("$channel$", channel.toString().substring(1))); - System.out.println(url); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String inputLine = ""; @@ -175,22 +193,55 @@ public final String[] getViewers() JsonObject jsonObj = JsonObject.readFrom(inputLine); JsonArray array = jsonObj.get("chatters").asObject().get("viewers").asArray(); JsonArray array2 = jsonObj.get("chatters").asObject().get("moderators").asArray(); - String[] viewers = new String[array.size() + array2.size()]; - int i = 0; + List viewers = new ArrayList(); for (JsonValue value : array) - { - viewers[i] = value.toString().substring(1, value.toString().length() - 1); - i++; - } + viewers.add(User.getUser(value.toString().substring(1, value.toString().length() - 1))); for (JsonValue value : array2) + viewers.add(User.getUser(value.toString().substring(1, value.toString().length() - 1))); + return viewers; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + /** + * Get the currently viewers (This method is on beta so it may not be optimized) + * @return A String[] with all the current viewers + */ + public final List getMods() + { + URL url; + try { + url = new URL(urln.replace("$channel$", channel.toString().substring(1))); + URLConnection conn = url.openConnection(); + BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); + String inputLine = ""; + String str = ""; + while ((str = br.readLine()) != null) { - viewers[i] = value.toString().substring(1, value.toString().length() - 1); - i++; + inputLine = inputLine + str; } - return viewers; + br.close(); + JsonObject jsonObj = JsonObject.readFrom(inputLine); + JsonArray array2 = jsonObj.get("chatters").asObject().get("moderators").asArray(); + List mods = new ArrayList(); + for (JsonValue value : array2) + mods.add(User.getUser(value.toString().substring(1, value.toString().length() - 1))); + return mods; } catch (IOException e) { e.printStackTrace(); } return null; } + + /** + * Check if the user is a mod + * @param user The user + * @return true : false + */ + public final boolean isMod(User user) + { + return this.getMods().contains(user); + } } diff --git a/src/tk/cavariux/twitchirc/Chat/User.java b/src/tk/cavariux/twitchirc/Chat/User.java index 1134107..1b2904c 100644 --- a/src/tk/cavariux/twitchirc/Chat/User.java +++ b/src/tk/cavariux/twitchirc/Chat/User.java @@ -1,25 +1,18 @@ package tk.cavariux.twitchirc.Chat; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; +import java.util.HashMap; -import tk.cavariux.twitchirc.Json.JsonArray; -import tk.cavariux.twitchirc.Json.JsonObject; -import tk.cavariux.twitchirc.Json.JsonValue; +import tk.cavariux.twitchirc.Core.TwitchBot; /** * The user object * @author Leonardo Mariscal - * @version 1.2-alpha + * @version 1.3-alpha */ public class User { - private String urln = "http://tmi.twitch.tv/group/user/$channel$/chatters"; private String user; + private static HashMap users = new HashMap(); /** * The constructor for a user. @@ -28,6 +21,15 @@ public class User public User(String user) { this.user = user; + users.put(user, this); + } + + public static final User getUser(String ign) + { + if (users.containsKey(ign)) + return users.get(ign); + else + return new User(ign); } /** @@ -45,34 +47,44 @@ public String toString() * @param channel The channel to check * @return A boolean if its op returns true */ - public final boolean isOp(Channel channel) + public final boolean isMod(Channel channel) + { + return channel.isMod(this); + } + + /** + * Ban a player from a channel (Requires Mod) + * @param channel The channel + */ + public final void ban(Channel channel) + { + channel.ban(this); + } + + /** + * UnBan a player from a channel (Requires Mod) + * @param channel The channel + */ + public final void unBan(Channel channel) + { + channel.unBan(this); + } + + /** + * Timeout a player from a channel (Requires Mod) + * @param channel The channel + */ + public final void timeout(Channel channel, int time) + { + channel.timeOut(this, time); + } + + /** + * Timeout a player from a channel (Requires Streamer/Editor) + * @param channel The channel + */ + public final void hostthisUser(Channel channel, TwitchBot bot) { - URL url; - try { - url = new URL(urln.replace("$channel$", channel.toString().substring(1))); - System.out.println(url); - URLConnection conn = url.openConnection(); - BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); - String inputLine = ""; - String str = ""; - while ((str = br.readLine()) != null) - { - inputLine = inputLine + str; - } - br.close(); - JsonObject jsonObj = JsonObject.readFrom(inputLine); - JsonArray array = jsonObj.get("chatters").asObject().get("moderators").asArray(); - ArrayList mods = new ArrayList(); - for (JsonValue value : array) - { - mods.add(value.toString().substring(1, value.toString().length() - 1)); - } - if (mods.contains(this.toString())) - return true; - } catch (IOException e) { - e.printStackTrace(); - } - - return false; + channel.host(Channel.getChannel(user, bot));; } } diff --git a/src/tk/cavariux/twitchirc/Core/TwitchBot.java b/src/tk/cavariux/twitchirc/Core/TwitchBot.java index c09e2d6..3a39f82 100644 --- a/src/tk/cavariux/twitchirc/Core/TwitchBot.java +++ b/src/tk/cavariux/twitchirc/Core/TwitchBot.java @@ -13,8 +13,8 @@ /** * The main object to start making your bot - * @author CavariuX - * @version 1.2-alpha + * @author Leonardo Mariscal + * @version 1.3-alpha */ public class TwitchBot { @@ -23,7 +23,7 @@ public class TwitchBot { private BufferedWriter writer; private BufferedReader reader; private ArrayList channels = new ArrayList(); - private String version = "v1.0-alpha"; + private String version = "v1.3-alpha"; public TwitchBot(){} @@ -90,6 +90,7 @@ public final void setOauth_Key (String oauth_key) { this.oauth_key = oauth_key; } + /** * This method is called when a message is sent on the Twitch Chat. * @param user The user is sent, if you put it on a String it will give you the user's nick @@ -101,6 +102,17 @@ protected void onMessage(User user, Channel channel, String message) } + /** + * This method is called when a command is sent on the Twitch Chat. + * @param user The user is sent, if you put it on a String it will give you the user's nick + * @param channel The channel where the command was sent + * @param message The command + */ + protected void onCommand(User user, Channel channel, String command) + { + + } + /** * This method is used if you want to send a command to the IRC server, not commontly used * @param message the command you will sent @@ -172,7 +184,7 @@ public final Channel joinChannel (String channel) { sendRawMessage("JOIN " + channel + "\r\n"); System.out.println("> JOIN " + channel); - return new Channel(channel, this); + return Channel.getChannel(channel, this); } /** @@ -212,12 +224,15 @@ public final void start() { String str[]; str = line.split("!"); - final User msg_user = new User(str[0].substring(1, str[0].length())); + final User msg_user = User.getUser(str[0].substring(1, str[0].length())); str = line.split(" "); Channel msg_channel; - msg_channel = new Channel(str[2], this); + msg_channel = Channel.getChannel(str[2], this); String msg_msg = line.substring((str[0].length() + str[1].length() + str[2].length() + 4), line.length()); System.out.println("> " + msg_channel + " | " + msg_user + " >> " + msg_msg); + if (msg_msg.startsWith("!")) + onCommand(msg_user, msg_channel, msg_msg.substring(1)); + onMessage(msg_user, msg_channel, msg_msg); } else if (line.toLowerCase().contains("disconnected")) {