Skip to content

Commit

Permalink
Fixed Activities module
Browse files Browse the repository at this point in the history
  • Loading branch information
faab007nl committed Sep 4, 2021
1 parent 025a709 commit 47b2b7f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.TechsCode.TechDiscordBot.TechDiscordBot;
import me.TechsCode.TechDiscordBot.module.Module;
import me.TechsCode.TechDiscordBot.mysql.storage.Storage;
import me.TechsCode.TechDiscordBot.mysql.storage.Verification;
import me.TechsCode.TechDiscordBot.objects.DefinedQuery;
import me.TechsCode.TechDiscordBot.objects.Query;
Expand All @@ -13,7 +14,7 @@
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;

import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class ActivitiesModule extends Module {
Expand All @@ -30,8 +31,6 @@ protected Query<TextChannel> newQuery() {
}
};

private ArrayList<String> announcedIds;

public ActivitiesModule(TechDiscordBot bot) {
super(bot);
}
Expand All @@ -40,21 +39,7 @@ public ActivitiesModule(TechDiscordBot bot) {
public void onEnable() {
new Thread(() -> {
while (true) {
if(!TechDiscordBot.getBot().getSpigotStatus().isUsable())
continue;

TechDiscordBot.getSpigotAPI().getSpigotResources().forEach(resource -> {
Plugin plugin = Plugin.fromId(resource.getId());
if (plugin == null)
return;

Update latestUpdate = plugin.getLatestUpdate();

boolean isNewUpdate = TechDiscordBot.getStorage().isNewUpdate(resource.getId(), latestUpdate.getId());
if(isNewUpdate){
printUpdate(plugin, latestUpdate);
}
});
checkResources();

try {
Thread.sleep(TimeUnit.MINUTES.toMillis(5));
Expand All @@ -80,22 +65,37 @@ public Requirement[] getRequirements() {
};
}

public void printReview(Plugin plugin, Review review) {
StringBuilder ratingSB = new StringBuilder();
for(int i = 0; i < review.getRating(); ++i) ratingSB.append(":star:");
Verification verification = TechDiscordBot.getStorage().retrieveVerificationWithSpigot(review.getUser().getUserId());
public void checkResources(){
if(!TechDiscordBot.getBot().getSpigotStatus().isUsable())
return;

boolean isVerified = verification != null;
boolean isMemberInDiscord = (isVerified && TechDiscordBot.getBot().getMember(verification.getDiscordId()) != null);
String usernameOrAt = (isMemberInDiscord ? TechDiscordBot.getBot().getMember(verification.getDiscordId()).getAsMention() : review.getUser().getUsername());
TechDiscordBot.getSpigotAPI().getSpigotResources().forEach(resource -> {
Plugin plugin = Plugin.fromId(resource.getId());
if (plugin == null)
return;

new TechEmbedBuilder("Review from " + review.getUser().getUsername())
.color(plugin.getColor())
.thumbnail(review.getResource().getIcon())
.field("Rating", ratingSB.toString(), true)
.text("```" + review.getText() + "```\nThanks to " + usernameOrAt + " for making this review!")
.queue(ACTIVITIES_CHANNEL.query().first());
announcedIds.add(review.getResource().getId() + "-" + review.getUser().getUserId());
Update latestUpdate = plugin.getLatestUpdate();
boolean isNewUpdate = isNewUpdate(resource.getId(), latestUpdate.getId());
if(isNewUpdate){
printUpdate(plugin, latestUpdate);
}
});
}

public boolean isNewUpdate(String resourceId, String updateId){
Storage s = TechDiscordBot.getStorage();
if(s.lastPluginExists(resourceId)){
String latestUpdate = s.getLastPluginUpdate(resourceId);
if(!Objects.equals(updateId, latestUpdate)){
s.updateLastPluginUpdate(resourceId, updateId);
return true;
}else{
return false;
}
}else{
s.insertLastPluginUpdate(resourceId, updateId);
return true;
}
}

public void printUpdate(Plugin plugin, Update update) {
Expand All @@ -113,6 +113,22 @@ public void printUpdate(Plugin plugin, Update update) {

if(update.getImages() != null && update.getImages().length > 0 && !update.getImages()[0].isEmpty()) ceb.image(update.getImages()[0]);
ceb.queue(ACTIVITIES_CHANNEL.query().first());
announcedIds.add((update.getId()));
}

public void printReview(Plugin plugin, Review review) {
StringBuilder ratingSB = new StringBuilder();
for(int i = 0; i < review.getRating(); ++i) ratingSB.append(":star:");
Verification verification = TechDiscordBot.getStorage().retrieveVerificationWithSpigot(review.getUser().getUserId());

boolean isVerified = verification != null;
boolean isMemberInDiscord = (isVerified && TechDiscordBot.getBot().getMember(verification.getDiscordId()) != null);
String usernameOrAt = (isMemberInDiscord ? TechDiscordBot.getBot().getMember(verification.getDiscordId()).getAsMention() : review.getUser().getUsername());

new TechEmbedBuilder("Review from " + review.getUser().getUsername())
.color(plugin.getColor())
.thumbnail(review.getResource().getIcon())
.field("Rating", ratingSB.toString(), true)
.text("```" + review.getText() + "```\nThanks to " + usernameOrAt + " for making this review!")
.queue(ACTIVITIES_CHANNEL.query().first());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void createDefault() {
mysql.update("CREATE TABLE IF NOT EXISTS " + REMINDERS_TABLE + " (user_id varchar(32), channel_id varchar(32), time varchar(32), type tinyint(1), reminder longtext);");
mysql.update("CREATE TABLE IF NOT EXISTS " + SUB_VERIFICATIONS_TABLE + " (discordId_verified varchar(32), discordId_subVerified varchar(32));");
mysql.update("CREATE TABLE IF NOT EXISTS " + TRANSCRIPTS_TABLE + " (id varchar(36), value longtext) DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;");
mysql.update("CREATE TABLE IF NOT EXISTS " + PLUGIN_UPDATES_TABLE + " (resourceId varchar(32), id varchar(32), PRIMARY KEY (resourceId) ) DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;");
mysql.update("CREATE TABLE IF NOT EXISTS " + PLUGIN_UPDATES_TABLE + " (resourceId varchar(32), updateId varchar(32), PRIMARY KEY (resourceId) ) DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;");

this.connected = true;
}
Expand Down Expand Up @@ -279,33 +279,51 @@ public void deleteReminder(Reminder reminder) {
mysql.update("DELETE FROM " + REMINDERS_TABLE + " WHERE user_id='" + reminder.getUserId() + "' AND channel_id='" + (reminder.getChannelId() == null ? "NULL" : "'" + reminder.getChannelId() + "'") + "' AND time='" + reminder.getTime() + "' AND type=" + reminder.getType().getI() + " AND reminder='" + reminder.getReminder().replace("'", "''") + "';");
}

public boolean isNewUpdate(String id, String resourceId){
boolean gotResult = false;
boolean isNewUpdate = false;
public String getLastPluginUpdate(String resourceId){
String update = "unknown";

try {
Connection connection = mysql.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " + PLUGIN_UPDATES_TABLE + " WHERE `resourceId`=" + resourceId);

PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " + PLUGIN_UPDATES_TABLE + " WHERE `resourceId`='"+resourceId+"';");
ResultSet rs = preparedStatement.executeQuery();

gotResult = rs.next();
if(gotResult){
String updateId = rs.getString("id");
while (rs.next())
update = rs.getString("updateId");

if(!id.equals(updateId)){
isNewUpdate = true;
}
}
mysql.update("INSERT INTO " + PLUGIN_UPDATES_TABLE + " (resourceId, id) VALUES ('"+resourceId+"', '"+id+"') ON DUPLICATE KEY UPDATE id='"+id+"';");
rs.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}

return update;
}

public void updateLastPluginUpdate(String resourceId, String newUpdateId){
mysql.update("UPDATE " + PLUGIN_UPDATES_TABLE + " SET `updateId`=? WHERE `resourceId`=?;", newUpdateId, resourceId);
}

public void insertLastPluginUpdate(String resourceId, String newUpdateId){
mysql.update("INSERT INTO " + PLUGIN_UPDATES_TABLE + " (resourceId, updateId) VALUES (?, ?)", resourceId, newUpdateId);
}

public boolean lastPluginExists(String resourceId){
boolean exists = false;

try {
Connection connection = mysql.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " + PLUGIN_UPDATES_TABLE + " WHERE `resourceId`='"+resourceId+"';");
ResultSet rs = preparedStatement.executeQuery();

exists = rs.next();

rs.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}

return isNewUpdate;
return exists;
}

}
2 changes: 1 addition & 1 deletion src/main/java/me/TechsCode/TechDiscordBot/util/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public Resource getResource() {
}

public Update getLatestUpdate() {
return TechDiscordBot.getSpigotAPI().getSpigotUpdates().resource(getResourceId()).get(TechDiscordBot.getSpigotAPI().getSpigotUpdates().resource(getResourceId()).size() - 1);
return TechDiscordBot.getSpigotAPI().getSpigotUpdates().resource(getResourceId()).stream().findFirst().orElse(null);
}

public static List<Plugin> allWithWiki() {
Expand Down

0 comments on commit 47b2b7f

Please sign in to comment.