Skip to content

Commit

Permalink
Riot news is working again <3
Browse files Browse the repository at this point in the history
  • Loading branch information
Daltz333 committed Jan 27, 2020
1 parent 658c6eb commit f3009c3
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 40 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/main/java/Commands/CustomCommands/BotHelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public void onMessageReceived(MessageReceivedEvent event) {
}
}

ebAdmin.addField("rssenable", "Enables the league feed to the channel - args: none", false);
ebAdmin.addField("rssdisable", "Disables the league feed to the channel - args: none", false);
ebAdmin.addField("newsenable", "Enables the league feed to the channel - args: none", false);
ebAdmin.addField("newsdisable", "Disables the league feed to the channel - args: none", false);

event.getChannel().sendMessage("Check your DMs " + event.getAuthor().getAsMention()).queue();

Expand Down
91 changes: 89 additions & 2 deletions src/main/java/Commands/CustomCommands/LeagueNewsCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package Commands.CustomCommands;

import Commands.CustomCommands.Subscribers.RiotGuild;
import Commands.CustomCommands.Subscribers.RiotNewsScheduler;
import Constants.Configuration;
import Utils.Pair;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class LeagueNewsCommand extends ListenerAdapter {
Expand All @@ -23,9 +29,90 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}

if (event.getMessage().getContentDisplay().contains(Configuration.kBotPrefix + "rss")) {
event.getChannel().sendMessage("Currently the RSS Feed is unavailable!").queue();
if (event.getMessage().getContentDisplay().contains(Configuration.kBotPrefix + "newsenable")) {
if(alreadyAdded(event)) {
event.getChannel().sendMessage("News is already enabled!").queue();
} else {
boolean found = false;
for (RiotGuild guild : RiotNewsScheduler.riotGuilds) {
if (guild.getGuildId().equalsIgnoreCase(event.getGuild().getId())) {
found = true;

guild.setNewsEnabled(true);
guild.setAnnouncementChannelId(event.getTextChannel().getId());

event.getChannel().sendMessage("Enabled news output for " + event.getGuild().getName()).queue();

try {
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
connection.createStatement().execute("UPDATE MAIN_GUILD_DATA SET Riot_Rss_Enable=1, Riot_Rss_Channel=" + event.getTextChannel().getId() + ", Riot_Rss_Last_Message=\"" + guild.getLastShownTitle() + "\" WHERE Guild_ID=" + event.getGuild().getId());
} catch (SQLException e) {
logger.error("Error connecting to database", e);
}
break;
}
}

if (!found) {
RiotNewsScheduler.riotGuilds.add(new RiotGuild(event.getGuild().getId(), event.getChannel().getId(), true, ""));
try {
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
ResultSet data = connection.createStatement().executeQuery("SELECT * FROM MAIN_GUILD_DATA WHERE Guild_ID=\""+event.getGuild().getId() +"\"");

if (!data.next()) {
connection.createStatement().execute("INSERT INTO MAIN_GUILD_DATA (Guild_ID, Member_Id, Member_Name, Member_Xp, Riot_Rss_Enable, Riot_Rss_Channel, Bot_Prefix) VALUES (" + event.getGuild().getId() + ", NULL, NULL, NULL, 1," + event.getTextChannel().getId() + ", NULL)");
} else {
//connection.createStatement().execute("UPDATE MAIN_GUILD_DATA SET Riot_Rss_Enable=1, Riot_Rss_Channel=" + event.getTextChannel().getId() + ", Riot_Rss_Last_Message=\"" + + "\" WHERE Guild_ID=" + event.getGuild().getId());
}

} catch (SQLException e) {
logger.error("Error connecting to database", e);
}
event.getChannel().sendMessage("Enabled news output for " + event.getGuild().getName()).queue();
}
//TODO add to database!
}
} else if (event.getMessage().getContentDisplay().contains(Configuration.kBotPrefix + "newsdisable")) {
boolean found = false;
for (RiotGuild guild : RiotNewsScheduler.riotGuilds) {
if (guild.getGuildId().equalsIgnoreCase(event.getGuild().getId())) {
found = true;

guild.setNewsEnabled(false);

event.getChannel().sendMessage("Disabled news output for " + event.getGuild().getName()).queue();
try {
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
connection.createStatement().execute("UPDATE MAIN_GUILD_DATA SET Riot_Rss_Enable=0, Riot_Rss_Channel=" + event.getTextChannel().getId() + " WHERE Guild_ID=" + event.getGuild().getId());
} catch (SQLException e) {
logger.error("Error connecting to database", e);
}
break;
}
}

if (!found) {
event.getChannel().sendMessage("News is already disabled on this server!").queue();
}
} else {
//unknown command ignore
}

}

public boolean alreadyAdded(MessageReceivedEvent event) {
for (RiotGuild guild : RiotNewsScheduler.riotGuilds) {
if (guild.getGuildId().equalsIgnoreCase(event.getGuild().getId())) {
if (guild.newsEnabled()) {
return true;
} else {
return false;
}
} else {
return false;
}
}

return false;
}
}
43 changes: 43 additions & 0 deletions src/main/java/Commands/CustomCommands/Subscribers/RiotGuild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Commands.CustomCommands.Subscribers;

public class RiotGuild {
private String guildId;
private String announcementChannelId;
private boolean newsEnabled;
private String lastShownTitle;

public RiotGuild(String guildId, String announcementChannelId, boolean newsEnabled, String lastShownTitle) {
this.guildId = guildId;
this.announcementChannelId = announcementChannelId;
this.newsEnabled = newsEnabled;
this.lastShownTitle = lastShownTitle;
}

public String getGuildId() {
return this.guildId;
}

public String getAnnouncementChannelId() {
return this.announcementChannelId;
}

public boolean newsEnabled() {
return this.newsEnabled;
}

public String getLastShownTitle() {
return this.lastShownTitle;
}

public void setNewsEnabled(boolean enabled) {
this.newsEnabled = enabled;
}

public void setLastShownTitle(String title) {
this.lastShownTitle = title;
}

public void setAnnouncementChannelId(String announcementChannelId) {
this.announcementChannelId = announcementChannelId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package Commands.CustomCommands.Subscribers;

import Constants.Configuration;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.TimerTask;

public class RiotNewsScheduler extends TimerTask {
private JDA jda;
private int i = 0;

public static ArrayList<RiotGuild> riotGuilds = new ArrayList<>();
private Logger logger = LoggerFactory.getLogger(Configuration.kLoggerName);

public RiotNewsScheduler(JDA jda) {
this.jda = jda;
}

@Override
public void run() {
logger.info("Running News Command!");
JSONObject jsonObject;
try {
jsonObject = new JSONObject(IOUtils.toString(new URL("https://lolstatic-a.akamaihd.net/frontpage/apps/prod/harbinger-l10-website/en-us/master/en-us/page-data/news/game-updates/page-data.json"), StandardCharsets.UTF_8));
} catch (IOException e) {
logger.error("Error retrieving JSON News!", e);
return;
}

JSONObject array = jsonObject.getJSONObject("result").getJSONObject("pageContext").getJSONObject("data").getJSONArray("sections").getJSONObject(0).getJSONObject("props").getJSONArray("articles").getJSONObject(0);

for (RiotGuild guild : riotGuilds) {
if (!guild.newsEnabled()) {
continue;
}

System.out.println(array.toString());
String title = array.getString("title");
String url = "https://na.leagueoflegends.com/en-us/" + array.getJSONObject("link").getString("url");
String imageUrl = array.getString("imageUrl");

if(guild.getLastShownTitle() == null) {
guild.setLastShownTitle("");
}

if (!guild.getLastShownTitle().equalsIgnoreCase(title)){
if (guild.getAnnouncementChannelId() != null) {
EmbedBuilder builder = new EmbedBuilder();
builder.setTitle(title);
builder.setDescription("[Read More!](" + url + ")");
builder.setImage(imageUrl);
builder.setFooter("Riot Patch Notes");

guild.setLastShownTitle(title);

jda.getGuildById(guild.getGuildId()).getTextChannelById(guild.getAnnouncementChannelId()).sendMessage(builder.build()).queue();

try {
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
connection.createStatement().execute("UPDATE MAIN_GUILD_DATA SET Riot_Rss_Last_Message=\"" + title + "\" WHERE Guild_ID=" + guild.getGuildId());
} catch (SQLException e) {
logger.error("Error connecting to database", e);
}

} else {
logger.error("Announcements are enabled but channel is null!");
}
} else {
logger.info(title + " has already been shown!");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ protected void execute(CommandEvent event) {

String champNames = "";

logger.info("Retrieved Random Champ IDS: " + champIds.toString());
for (Integer champ : champIds) {
String champName = "";

Expand All @@ -72,7 +73,7 @@ protected void execute(CommandEvent event) {
}
}

champNames = champNames + champName + " ";
champNames = champNames + champName + " \n";
}

eb.addField("Champions", champNames, false);
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/Handlers/RateLimitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ public static boolean isRateLimited() {
if (systemTime == -1 || rateLimit == -1) {
isRatedLimited = false;
} else {
if (systemTime + rateLimit*1000 < System.currentTimeMillis()) {
isRatedLimited = false;
} else {
isRatedLimited = true;
}
isRatedLimited = systemTime + rateLimit * 1000 >= System.currentTimeMillis();
}

return isRatedLimited;
Expand Down
44 changes: 34 additions & 10 deletions src/main/java/InternalParser/JsonLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import Constants.Configuration;
import InternalParser.JsonLol.*;
import com.google.gson.JsonArray;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;

Expand All @@ -20,20 +25,21 @@ public class JsonLoader {

private static Logger logger = LoggerFactory.getLogger(Configuration.kLoggerName);

public void loadJson(InputStream url, DataType type) {
public void loadJson(DataType type) {
logger.info("Parsing Champion Information");

champions.clear();
String currentVersion = getLatestChampVersion();

Scanner scanner = new Scanner(url);
StringBuilder json = new StringBuilder();

while (scanner.hasNext()) {
json.append(scanner.next());
}
logger.info("Current RIOT version is " + currentVersion);

if (type == DataType.CHAMPIONS) {
JSONObject jsonObject = new JSONObject(json.toString());
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(IOUtils.toString(new URL("https://ddragon.leagueoflegends.com/cdn/" + currentVersion + "/data/en_US/champion.json"), StandardCharsets.UTF_8));
} catch (IOException e) {
logger.error("Unable to read from URL", e);
return;
}

//load champion json data
JSONObject champData = jsonObject.getJSONObject("data");
Expand Down Expand Up @@ -61,7 +67,13 @@ public void loadJson(InputStream url, DataType type) {
//TODO

} else if (type == DataType.RUNES) {
JSONArray jsonDataArray = new JSONArray(json.toString());
JSONArray jsonDataArray = null;
try {
jsonDataArray = new JSONArray(IOUtils.toString(new URL("https://ddragon.leagueoflegends.com/cdn/" + currentVersion + "/data/en_US/runesReforged.json"), StandardCharsets.UTF_8));
} catch (IOException e) {
logger.error("Unable to read from URL!", e);
return;
}

for (int i = 0; i < jsonDataArray.length(); i++) {
JSONObject primaryData = jsonDataArray.getJSONObject(i);
Expand Down Expand Up @@ -95,4 +107,16 @@ public void loadJson(InputStream url, DataType type) {
}
}
}

private String getLatestChampVersion() {
JSONArray jsonObject;
try {
jsonObject = new JSONArray(IOUtils.toString(new URL("https://ddragon.leagueoflegends.com/api/versions.json"), StandardCharsets.UTF_8));
} catch (IOException e) {
logger.error("Unable to get latest champ version!");
return "10.2.1";
}

return jsonObject.getString(0);
}
}
Loading

0 comments on commit f3009c3

Please sign in to comment.