Skip to content

Commit

Permalink
Cleaned & Added Preorder Module.
Browse files Browse the repository at this point in the history
  • Loading branch information
EazyFTW committed Aug 28, 2020
1 parent 0742a1e commit 32004c4
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package me.TechsCode.TechDiscordBot.module.modules;

import me.TechsCode.TechDiscordBot.TechDiscordBot;
import me.TechsCode.TechDiscordBot.module.Module;
import me.TechsCode.TechDiscordBot.mysql.storage.Preorder;
import me.TechsCode.TechDiscordBot.objects.Requirement;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class PreorderModule extends Module {

private List<String> roles = new ArrayList<>();

public PreorderModule(TechDiscordBot bot) {
super(bot);
}

@Override
public void onEnable() {
new Thread(() -> {
while (true) {
updateRoles();

try {
Thread.sleep(TimeUnit.MINUTES.toMillis(5)); //Wait every 5 minutes
} catch (Exception ignored) {}
}
}).start();

new Thread(() -> {
while (true) {
updateRolesMembers();

try {
Thread.sleep(TimeUnit.SECONDS.toMillis(10)); //Wait every 10 seconds
} catch (Exception ignored) {}
}
}).start();
}

public void updateRoles() {
roles = TechDiscordBot.getJDA().getRoles().stream().filter(role -> role.getName().endsWith(" Preorders")).map(Role::getName).collect(Collectors.toList());
}

public void updateRolesMembers() {
if(roles.size() == 0) return;

for(String roleS : roles) {
if(!bot.getRoles(roleS).hasAny()) continue;
String pluginName = roleS.replace(" Preorder", "");

List<Preorder> preorders = new ArrayList<>(TechDiscordBot.getStorage().getPreorders(pluginName));

for(Preorder preorder : preorders) {
Member member = bot.getMember(String.valueOf(preorder.getDiscordId()));
if(member == null) continue;

if(member.getRoles().stream().anyMatch(r -> r.getName().equals(roleS))) continue;

Role role = bot.getRoles(roleS).first();
TechDiscordBot.getGuild().addRoleToMember(member, role).queue();
}
}
}

@Override
public void onDisable() {}

@Override
public String getName() {
return "Preorder";
}

@Override
public Requirement[] getRequirements() {
return new Requirement[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.TechsCode.TechDiscordBot.mysql.storage;

public class Preorder {

private final String plugin, email, discordName, transactionId;
private final long discordId;

public Preorder(String plugin, String email, long discordId, String discordName, String transactionId) {
this.plugin = plugin;
this.email = email;
this.discordId = discordId;
this.discordName = discordName;
this.transactionId = transactionId;
}

public String getPlugin() {
return plugin;
}

public String getEmail() {
return email;
}

public String getDiscordName() {
return discordName;
}

public long getDiscordId() {
return discordId;
}

public String getTransactionId() {
return transactionId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

public class Storage {

private MySQL mysql;
private final MySQL mysql;
private boolean connected;

private final String VERIFICATIONS_TABLE = "Verifications";
private final String SERVERS_TABLE = "Servers";
private final String TRANSCRIPTS_TABLE = "Transcripts";
private final String REMINDERS_TABLE = "Reminders";

private Storage(MySQLSettings mySQLSettings) {
Expand All @@ -45,8 +43,6 @@ public boolean isConnected() {

public void createDefault() {
mysql.update("CREATE TABLE IF NOT EXISTS " + VERIFICATIONS_TABLE + " (userid VARCHAR(10), discordid VARCHAR(32));");
mysql.update("CREATE TABLE IF NOT EXISTS " + TRANSCRIPTS_TABLE + " (id VARCHAR(32), html LONGTEXT, password TEXT, PRIMARY KEY (id));");
mysql.update("CREATE TABLE IF NOT EXISTS " + SERVERS_TABLE + " (user_id int(64), discord_id VARCHAR(64));");
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);");

this.connected = true;
Expand Down Expand Up @@ -98,6 +94,21 @@ public Set<Reminder> retrieveReminders() {
return ret;
}

public Set<Preorder> getPreorders(String plugin) {
Set<Preorder> ret = new HashSet<>();
try {
Connection connection = mysql.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM " + plugin.replace(" ", "") + "Preorders;");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) ret.add(new Preorder(plugin, rs.getString("email"), rs.getLong("discordId"), rs.getString("discordName"), rs.getString("transactionId")));
rs.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return ret;
}

public void saveReminder(Reminder reminder) {
mysql.update("INSERT INTO " + REMINDERS_TABLE + " (user_id, channel_id, time, type, reminder) VALUES ('" + reminder.getUserId() + "', " + (reminder.getChannelId() == null ? "NULL" : "'" + reminder.getChannelId() + "'") + ", '" + reminder.getTime() + "', " + reminder.getType().getI() + ", '" + reminder.getReminder().replace("'", "''") + "');");
}
Expand Down

0 comments on commit 32004c4

Please sign in to comment.