Skip to content

Commit

Permalink
Merge pull request #44 from Mazawrath/TwitchFix
Browse files Browse the repository at this point in the history
Twitch fix
  • Loading branch information
Mazawrath authored Jan 8, 2019
2 parents 4383fd5 + b27cd21 commit 6eb0483
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@ public void onCommand(String[] args, DiscordApi api, ServerTextChannel serverTex

private String getRecentChangeLog() {
return "**New beanBOT update released.**\n" +
"Release can be found on https://github.com/Mazawrath/beanBOT/releases/tag/v3.1.0\n" +
"Detailed changelog can be found on https://github.com/Mazawrath/beanBOT/compare/v3.0.1...v3.1.0\n" +
"Release can be found on https://github.com/Mazawrath/beanBOT/releases/tag/v3.1.1\n" +
"Detailed changelog can be found on https://github.com/Mazawrath/beanBOT/compare/v3.1.0...v3.1.1\n" +
"\n" +
"**v3.1.0**\n" +
"**New**\n" +
"\t- Twitch Update\n" +
"\t\t- beanBOT can now notify servers when a Twitch channel goes live.\n" +
"\t\t- Server owners can now type `.admintwitch add [twitch channel name]` to subscribe to live notifications for a twitch channel.\n" +
"**v3.1.1**\n" +
// "**New**\n" +
// "\t- Twitch Update\n" +
// "\t\t- beanBOT can now notify servers when a Twitch channel goes live.\n" +
// "\t\t- Server owners can now type `.admintwitch add [twitch channel name]` to subscribe to live notifications for a twitch channel.\n" +
"**Changes**\n" +
"\t- Increased price of `.beanlottery draw` from 400 beanCoin to 50,000 beanCoin.\n" +
"\t- Users can now be mentioned when transferring beancoin or looking up user info.\n" +
"\t\t- Commands like `.userinfo` and `.beantransfer` now can have users tagged with `@`.\n" +
"\t- Added a mysterious tax for coins going to beanBOT...";
"\t- Set up Twitch to only notify servers if streamer was previously offline and has not been online within the past 10 minutes.\n" +
"\t- Changed price of `.beanlottery draw` from 400 beanCoin to 20,000 beanCoin.\n" +
"\t- Lowered time of bean lottery drawing from 7 days to 3 days.";
// "**Bug Fixes**\n" +
// "\t- Fixed an issue with `.beanlottery` not giving help information.\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mazawrath/beanbot/utilities/Lottery.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void run() {
}
}
}
}, 0, 10050, TimeUnit.MINUTES);
}, 0, 4280, TimeUnit.MINUTES);
}

public void drawNumbers(Points points, Server server, DiscordApi api, ServerTextChannel serverTextChannel) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mazawrath/beanbot/utilities/Points.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Points {
public static final BigDecimal COMMAND_COST = new BigDecimal("2.00").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal COMMAND_COST_SPECIAL = new BigDecimal("10.00").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal LOTTERY_TICKET_COST = new BigDecimal("20.00").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal LOTTERY_DRAWING_COST = new BigDecimal("400.00").setScale(SCALE, ROUNDING_MODE);
public static final BigDecimal LOTTERY_DRAWING_COST = new BigDecimal("20000.00").setScale(SCALE, ROUNDING_MODE);
private Connection conn;

private static final BigDecimal NUMBER_TO_PERCENT = new BigDecimal(.01);
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/com/mazawrath/beanbot/utilities/Twitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void checkTable() {

if (r.db(DB_NAME).tableList().contains(TWITCH_CHANNEL_LIST_TABLE).run(conn)) {
} else {
r.db(DB_NAME).tableCreate(TWITCH_CHANNEL_LIST_TABLE).withFields(r.hashMap("previously_live", false)).run(conn);
r.db(DB_NAME).tableCreate(TWITCH_CHANNEL_LIST_TABLE).run(conn);
}
}

Expand All @@ -79,6 +79,7 @@ public int addServer(String user, String serverId, String channelId) {
return -1;

checkServer(serverId);
checkTwitchId(Long.valueOf(userId));
r.db(DB_NAME).table(SERVER_SUBSCRIPTION_LIST_TABLE).filter(
r.hashMap("id", serverId)).update(
r.hashMap("userId", userId)).run(conn);
Expand All @@ -88,6 +89,9 @@ public int addServer(String user, String serverId, String channelId) {
r.db(DB_NAME).table(SERVER_SUBSCRIPTION_LIST_TABLE).filter(
r.hashMap("id", serverId)).update(
r.hashMap("delete_requested", false)).run(conn);

r.db(DB_NAME).table(TWITCH_CHANNEL_LIST_TABLE).filter(r.hashMap("id", Long.valueOf(userId))).update(r.hashMap("previously_live", false)).run(conn);
r.db(DB_NAME).table(TWITCH_CHANNEL_LIST_TABLE).filter(r.hashMap("id", Long.valueOf(userId))).update(r.hashMap("previous_time_live", 0)).run(conn);
if (subscribeToLiveNotifications(Long.parseLong(userId)))
retVal = 1;
else {
Expand Down Expand Up @@ -177,7 +181,7 @@ public boolean checkIfLive(String channel) {
return client.getStreamEndpoint().isLive(client.getChannelEndpoint().getChannel(channel));
}

public long getUserID(String user) {
public static long getUserID(String user) {
HttpResponse response = curl("-H 'Client-ID: " + clientId + "' https://api.twitch.tv/helix/users?login=" + user);
long retVal = -1;

Expand All @@ -200,7 +204,7 @@ public long getUserID(String user) {
return retVal;
}

public String getUserName(long userId) {
public static String getUserName(long userId) {
HttpResponse response = curl("-H 'Client-ID: " + clientId + "' https://api.twitch.tv/helix/users?id=" + userId);
String retVal = null;

Expand Down Expand Up @@ -322,6 +326,27 @@ public static String getHubSecret(long userId) {
return password.size() == 0 ? null : password.get(0).toString();
}

public static boolean getStatus(long userId) {
Cursor dbReturn = r.db(DB_NAME).table(TWITCH_CHANNEL_LIST_TABLE).filter(r.hashMap("id", userId)).getField("previously_live").run(conn);

return ((boolean) dbReturn.toList().get(0));
}

public static void setStatus(long userId, boolean status) {
r.db(DB_NAME).table(TWITCH_CHANNEL_LIST_TABLE).filter(r.hashMap("id", userId)).update(r.hashMap("previously_live", status)).run(conn);
}

public static long getOfflineTime(long userId) {
Cursor dbReturn = r.db(DB_NAME).table(TWITCH_CHANNEL_LIST_TABLE).filter(r.hashMap("id", userId)).getField("previous_time_live").run(conn);
List offlineTime = dbReturn.toList();

return (long) offlineTime.get(0);
}

public static void setOfflineTime(long userId) {
r.db(DB_NAME).table(TWITCH_CHANNEL_LIST_TABLE).filter(r.hashMap("id", userId)).update(r.hashMap("previous_time_live", System.currentTimeMillis())).run(conn);
}

private String checkPassword(long userId) {
checkTwitchId(userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.crypto.spec.SecretKeySpec;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.awt.*;
import java.time.Instant;

@Path("/twitchapi")
Expand Down Expand Up @@ -45,10 +46,10 @@ else if (mode.equals("unsubscribe")) {
@POST
@Path("/stream_changed")
public void response(@HeaderParam("x-hub-signature") String signature, @HeaderParam("content-length") int length, @QueryParam("password") String password, @QueryParam("username") String userName, String payload) {
System.out.println("Someone went live");

System.out.println("POST received.");
JSONObject payloadJson = new JSONObject(payload);
if (payloadJson.getJSONArray("data").length() != 0) {
System.out.println("Someone went live");
payloadJson = new JSONObject(payload).getJSONArray("data").getJSONObject(0);

System.out.println("ID: " + payloadJson.getString("user_id"));
Expand Down Expand Up @@ -89,11 +90,20 @@ public void response(@HeaderParam("x-hub-signature") String signature, @HeaderPa
long streamStartInstant = Instant.parse(payloadJson.getString("started_at")).getEpochSecond();
System.out.println("Started at: " + streamStartInstant);

if ((System.currentTimeMillis() / 1000) - streamStartInstant < 180)
//if ((System.currentTimeMillis() / 1000) - streamStartInstant < 180)
if (!Twitch.getStatus(payloadJson.getLong("user_id")) && System.currentTimeMillis() - Twitch.getOfflineTime(payloadJson.getLong("user_id")) > 600000) {
Twitch.notifyLive(new LivestreamNotification(payloadJson.getString("user_id"), userName, payloadJson.getString("title"), payloadJson.getString("game_id"), payloadJson.getInt("viewer_count"), payloadJson.getString("thumbnail_url")));
Twitch.setStatus(payloadJson.getLong("user_id"), true);
} else
System.out.println("Streamer recently live. Not notifying.");

} else
} else {
System.out.println("Someone went offline");
long userId = Twitch.getUserID(userName);

Twitch.setStatus(userId, false);
Twitch.setOfflineTime(userId);
}

System.out.println(length);
}
Expand Down

0 comments on commit 6eb0483

Please sign in to comment.