Skip to content
This repository has been archived by the owner on Sep 26, 2018. It is now read-only.

Commit

Permalink
v1.3 - Alpha
Browse files Browse the repository at this point in the history
++Added | --Removed | *Extra
------------------------------------------
++ Added User methods
++ Added Channel methods
--Some lag while using it
*Improved channel and User getting
*Fixed is Mod
  • Loading branch information
CavariuX committed May 31, 2015
1 parent a08b21b commit b9fd731
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin/
75 changes: 63 additions & 12 deletions src/tk/cavariux/twitchirc/Chat/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Channel> channels = new HashMap<String, Channel>();

/**
* The constructor of the channel object
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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<User> 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 = "";
Expand All @@ -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<User> viewers = new ArrayList<User>();
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<User> 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<User> mods = new ArrayList<User>();
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);
}
}
90 changes: 51 additions & 39 deletions src/tk/cavariux/twitchirc/Chat/User.java
Original file line number Diff line number Diff line change
@@ -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<String, User> users = new HashMap<String, User>();

/**
* The constructor for a user.
Expand All @@ -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);
}

/**
Expand All @@ -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<String> mods = new ArrayList<String>();
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));;
}
}
27 changes: 21 additions & 6 deletions src/tk/cavariux/twitchirc/Core/TwitchBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -23,7 +23,7 @@ public class TwitchBot {
private BufferedWriter writer;
private BufferedReader reader;
private ArrayList<String> channels = new ArrayList<String>();
private String version = "v1.0-alpha";
private String version = "v1.3-alpha";

public TwitchBot(){}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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"))
{
Expand Down

0 comments on commit b9fd731

Please sign in to comment.