Skip to content

Commit f3009c3

Browse files
committed
Riot news is working again <3
1 parent 658c6eb commit f3009c3

File tree

11 files changed

+315
-40
lines changed

11 files changed

+315
-40
lines changed

.idea/inspectionProfiles/Project_Default.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/Commands/CustomCommands/BotHelpCommand.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void onMessageReceived(MessageReceivedEvent event) {
7272
}
7373
}
7474

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

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

src/main/java/Commands/CustomCommands/LeagueNewsCommand.java

+89-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package Commands.CustomCommands;
22

3+
import Commands.CustomCommands.Subscribers.RiotGuild;
4+
import Commands.CustomCommands.Subscribers.RiotNewsScheduler;
35
import Constants.Configuration;
46
import Utils.Pair;
57
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
68
import net.dv8tion.jda.api.hooks.ListenerAdapter;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
911

12+
import java.sql.Connection;
13+
import java.sql.DriverManager;
14+
import java.sql.ResultSet;
15+
import java.sql.SQLException;
1016
import java.util.ArrayList;
1117

1218
public class LeagueNewsCommand extends ListenerAdapter {
@@ -23,9 +29,90 @@ public void onMessageReceived(MessageReceivedEvent event) {
2329
return;
2430
}
2531

26-
if (event.getMessage().getContentDisplay().contains(Configuration.kBotPrefix + "rss")) {
27-
event.getChannel().sendMessage("Currently the RSS Feed is unavailable!").queue();
32+
if (event.getMessage().getContentDisplay().contains(Configuration.kBotPrefix + "newsenable")) {
33+
if(alreadyAdded(event)) {
34+
event.getChannel().sendMessage("News is already enabled!").queue();
35+
} else {
36+
boolean found = false;
37+
for (RiotGuild guild : RiotNewsScheduler.riotGuilds) {
38+
if (guild.getGuildId().equalsIgnoreCase(event.getGuild().getId())) {
39+
found = true;
40+
41+
guild.setNewsEnabled(true);
42+
guild.setAnnouncementChannelId(event.getTextChannel().getId());
43+
44+
event.getChannel().sendMessage("Enabled news output for " + event.getGuild().getName()).queue();
45+
46+
try {
47+
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
48+
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());
49+
} catch (SQLException e) {
50+
logger.error("Error connecting to database", e);
51+
}
52+
break;
53+
}
54+
}
55+
56+
if (!found) {
57+
RiotNewsScheduler.riotGuilds.add(new RiotGuild(event.getGuild().getId(), event.getChannel().getId(), true, ""));
58+
try {
59+
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
60+
ResultSet data = connection.createStatement().executeQuery("SELECT * FROM MAIN_GUILD_DATA WHERE Guild_ID=\""+event.getGuild().getId() +"\"");
61+
62+
if (!data.next()) {
63+
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)");
64+
} else {
65+
//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());
66+
}
67+
68+
} catch (SQLException e) {
69+
logger.error("Error connecting to database", e);
70+
}
71+
event.getChannel().sendMessage("Enabled news output for " + event.getGuild().getName()).queue();
72+
}
73+
//TODO add to database!
74+
}
75+
} else if (event.getMessage().getContentDisplay().contains(Configuration.kBotPrefix + "newsdisable")) {
76+
boolean found = false;
77+
for (RiotGuild guild : RiotNewsScheduler.riotGuilds) {
78+
if (guild.getGuildId().equalsIgnoreCase(event.getGuild().getId())) {
79+
found = true;
80+
81+
guild.setNewsEnabled(false);
82+
83+
event.getChannel().sendMessage("Disabled news output for " + event.getGuild().getName()).queue();
84+
try {
85+
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
86+
connection.createStatement().execute("UPDATE MAIN_GUILD_DATA SET Riot_Rss_Enable=0, Riot_Rss_Channel=" + event.getTextChannel().getId() + " WHERE Guild_ID=" + event.getGuild().getId());
87+
} catch (SQLException e) {
88+
logger.error("Error connecting to database", e);
89+
}
90+
break;
91+
}
92+
}
93+
94+
if (!found) {
95+
event.getChannel().sendMessage("News is already disabled on this server!").queue();
96+
}
97+
} else {
98+
//unknown command ignore
99+
}
100+
101+
}
102+
103+
public boolean alreadyAdded(MessageReceivedEvent event) {
104+
for (RiotGuild guild : RiotNewsScheduler.riotGuilds) {
105+
if (guild.getGuildId().equalsIgnoreCase(event.getGuild().getId())) {
106+
if (guild.newsEnabled()) {
107+
return true;
108+
} else {
109+
return false;
110+
}
111+
} else {
112+
return false;
113+
}
28114
}
29115

116+
return false;
30117
}
31118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Commands.CustomCommands.Subscribers;
2+
3+
public class RiotGuild {
4+
private String guildId;
5+
private String announcementChannelId;
6+
private boolean newsEnabled;
7+
private String lastShownTitle;
8+
9+
public RiotGuild(String guildId, String announcementChannelId, boolean newsEnabled, String lastShownTitle) {
10+
this.guildId = guildId;
11+
this.announcementChannelId = announcementChannelId;
12+
this.newsEnabled = newsEnabled;
13+
this.lastShownTitle = lastShownTitle;
14+
}
15+
16+
public String getGuildId() {
17+
return this.guildId;
18+
}
19+
20+
public String getAnnouncementChannelId() {
21+
return this.announcementChannelId;
22+
}
23+
24+
public boolean newsEnabled() {
25+
return this.newsEnabled;
26+
}
27+
28+
public String getLastShownTitle() {
29+
return this.lastShownTitle;
30+
}
31+
32+
public void setNewsEnabled(boolean enabled) {
33+
this.newsEnabled = enabled;
34+
}
35+
36+
public void setLastShownTitle(String title) {
37+
this.lastShownTitle = title;
38+
}
39+
40+
public void setAnnouncementChannelId(String announcementChannelId) {
41+
this.announcementChannelId = announcementChannelId;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package Commands.CustomCommands.Subscribers;
2+
3+
import Constants.Configuration;
4+
import net.dv8tion.jda.api.EmbedBuilder;
5+
import net.dv8tion.jda.api.JDA;
6+
import net.dv8tion.jda.api.entities.Guild;
7+
import net.dv8tion.jda.api.entities.MessageChannel;
8+
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
9+
import org.apache.commons.io.IOUtils;
10+
import org.json.JSONArray;
11+
import org.json.JSONObject;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import java.io.IOException;
16+
import java.net.URL;
17+
import java.nio.charset.StandardCharsets;
18+
import java.sql.Connection;
19+
import java.sql.DriverManager;
20+
import java.sql.SQLException;
21+
import java.util.ArrayList;
22+
import java.util.TimerTask;
23+
24+
public class RiotNewsScheduler extends TimerTask {
25+
private JDA jda;
26+
private int i = 0;
27+
28+
public static ArrayList<RiotGuild> riotGuilds = new ArrayList<>();
29+
private Logger logger = LoggerFactory.getLogger(Configuration.kLoggerName);
30+
31+
public RiotNewsScheduler(JDA jda) {
32+
this.jda = jda;
33+
}
34+
35+
@Override
36+
public void run() {
37+
logger.info("Running News Command!");
38+
JSONObject jsonObject;
39+
try {
40+
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));
41+
} catch (IOException e) {
42+
logger.error("Error retrieving JSON News!", e);
43+
return;
44+
}
45+
46+
JSONObject array = jsonObject.getJSONObject("result").getJSONObject("pageContext").getJSONObject("data").getJSONArray("sections").getJSONObject(0).getJSONObject("props").getJSONArray("articles").getJSONObject(0);
47+
48+
for (RiotGuild guild : riotGuilds) {
49+
if (!guild.newsEnabled()) {
50+
continue;
51+
}
52+
53+
System.out.println(array.toString());
54+
String title = array.getString("title");
55+
String url = "https://na.leagueoflegends.com/en-us/" + array.getJSONObject("link").getString("url");
56+
String imageUrl = array.getString("imageUrl");
57+
58+
if(guild.getLastShownTitle() == null) {
59+
guild.setLastShownTitle("");
60+
}
61+
62+
if (!guild.getLastShownTitle().equalsIgnoreCase(title)){
63+
if (guild.getAnnouncementChannelId() != null) {
64+
EmbedBuilder builder = new EmbedBuilder();
65+
builder.setTitle(title);
66+
builder.setDescription("[Read More!](" + url + ")");
67+
builder.setImage(imageUrl);
68+
builder.setFooter("Riot Patch Notes");
69+
70+
guild.setLastShownTitle(title);
71+
72+
jda.getGuildById(guild.getGuildId()).getTextChannelById(guild.getAnnouncementChannelId()).sendMessage(builder.build()).queue();
73+
74+
try {
75+
Connection connection = DriverManager.getConnection(Configuration.kDatabaseUrl);
76+
connection.createStatement().execute("UPDATE MAIN_GUILD_DATA SET Riot_Rss_Last_Message=\"" + title + "\" WHERE Guild_ID=" + guild.getGuildId());
77+
} catch (SQLException e) {
78+
logger.error("Error connecting to database", e);
79+
}
80+
81+
} else {
82+
logger.error("Announcements are enabled but channel is null!");
83+
}
84+
} else {
85+
logger.info(title + " has already been shown!");
86+
}
87+
}
88+
}
89+
}

src/main/java/Commands/LeagueCommands/CurrentRotationCommand.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected void execute(CommandEvent event) {
6262

6363
String champNames = "";
6464

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

@@ -72,7 +73,7 @@ protected void execute(CommandEvent event) {
7273
}
7374
}
7475

75-
champNames = champNames + champName + " ";
76+
champNames = champNames + champName + " \n";
7677
}
7778

7879
eb.addField("Champions", champNames, false);

src/main/java/Handlers/RateLimitHandler.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ public static boolean isRateLimited() {
1515
if (systemTime == -1 || rateLimit == -1) {
1616
isRatedLimited = false;
1717
} else {
18-
if (systemTime + rateLimit*1000 < System.currentTimeMillis()) {
19-
isRatedLimited = false;
20-
} else {
21-
isRatedLimited = true;
22-
}
18+
isRatedLimited = systemTime + rateLimit * 1000 >= System.currentTimeMillis();
2319
}
2420

2521
return isRatedLimited;

src/main/java/InternalParser/JsonLoader.java

+34-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
import Constants.Configuration;
44
import InternalParser.JsonLol.*;
55
import com.google.gson.JsonArray;
6+
import org.apache.commons.io.IOUtils;
67
import org.json.JSONArray;
78
import org.json.JSONObject;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
1011

12+
import java.io.IOException;
1113
import java.io.InputStream;
14+
import java.net.URL;
15+
import java.nio.charset.Charset;
16+
import java.nio.charset.StandardCharsets;
1217
import java.util.ArrayList;
1318
import java.util.Scanner;
1419

@@ -20,20 +25,21 @@ public class JsonLoader {
2025

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

23-
public void loadJson(InputStream url, DataType type) {
28+
public void loadJson(DataType type) {
2429
logger.info("Parsing Champion Information");
2530

26-
champions.clear();
31+
String currentVersion = getLatestChampVersion();
2732

28-
Scanner scanner = new Scanner(url);
29-
StringBuilder json = new StringBuilder();
30-
31-
while (scanner.hasNext()) {
32-
json.append(scanner.next());
33-
}
33+
logger.info("Current RIOT version is " + currentVersion);
3434

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

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

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

6678
for (int i = 0; i < jsonDataArray.length(); i++) {
6779
JSONObject primaryData = jsonDataArray.getJSONObject(i);
@@ -95,4 +107,16 @@ public void loadJson(InputStream url, DataType type) {
95107
}
96108
}
97109
}
110+
111+
private String getLatestChampVersion() {
112+
JSONArray jsonObject;
113+
try {
114+
jsonObject = new JSONArray(IOUtils.toString(new URL("https://ddragon.leagueoflegends.com/api/versions.json"), StandardCharsets.UTF_8));
115+
} catch (IOException e) {
116+
logger.error("Unable to get latest champ version!");
117+
return "10.2.1";
118+
}
119+
120+
return jsonObject.getString(0);
121+
}
98122
}

0 commit comments

Comments
 (0)