-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
315 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/Commands/CustomCommands/Subscribers/RiotGuild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/Commands/CustomCommands/Subscribers/RiotNewsScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.