From 2f5305517e70f561f0775a100d9bae4d070ac3c9 Mon Sep 17 00:00:00 2001 From: Presti Date: Fri, 22 Jul 2022 15:55:11 +0200 Subject: [PATCH 1/7] * Added Birthday-wish Database Entity. --- .../ree6/sql/entities/BirthdayWish.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java diff --git a/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java b/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java new file mode 100644 index 000000000..bf1a54dbc --- /dev/null +++ b/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java @@ -0,0 +1,94 @@ +package de.presti.ree6.sql.entities; + +import de.presti.ree6.sql.base.annotations.Property; +import de.presti.ree6.sql.base.annotations.Table; + +import java.util.Date; + +/** + * This class is used to represent a Birthday-Wish, in our Database. + */ +@Table(name = "BirthdayWish") +public class BirthdayWish { + + /** + * The Guild ID. + */ + @Property(name = "gid") + String guildId; + + /** + * The Channel ID. + */ + @Property(name = "cid") + String channelId; + + /** + * The User ID. + */ + @Property(name = "uid") + String userId; + + /** + * The Birthday. + */ + @Property(name = "birthday") + Date birthdate; + + /** + * Constructor. + */ + public BirthdayWish() { + } + + /** + * Constructor. + * + * @param guildId the Guild ID. + * @param channelId the Channel ID. + * @param userId the User ID. + * @param birthdate the Birthday. + */ + public BirthdayWish(String guildId, String channelId, String userId, Date birthdate) { + this.guildId = guildId; + this.channelId = channelId; + this.userId = userId; + this.birthdate = birthdate; + } + + /** + * Get the Guild ID. + * + * @return the Guild ID. + */ + public String getGuildId() { + return guildId; + } + + /** + * Get the Channel ID. + * + * @return the Channel ID. + */ + public String getChannelId() { + return channelId; + } + + /** + * Get the User ID. + * + * @return the User ID. + */ + public String getUserId() { + return userId; + } + + /** + * Get the Birthday. + * + * @return the Birthday. + */ + public Date getBirthdate() { + return birthdate; + } +} From 9f65324be7e57a33472d80f40931122a574c99f4 Mon Sep 17 00:00:00 2001 From: DxsSucuk Date: Sun, 24 Jul 2022 02:17:04 +0200 Subject: [PATCH 2/7] * Fixed SQL Entity. * Added Command. --- .../commands/impl/community/Birthday.java | 38 +++++++++++++++++++ .../ree6/sql/entities/BirthdayWish.java | 3 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/presti/ree6/commands/impl/community/Birthday.java diff --git a/src/main/java/de/presti/ree6/commands/impl/community/Birthday.java b/src/main/java/de/presti/ree6/commands/impl/community/Birthday.java new file mode 100644 index 000000000..c7a2f2ad7 --- /dev/null +++ b/src/main/java/de/presti/ree6/commands/impl/community/Birthday.java @@ -0,0 +1,38 @@ +package de.presti.ree6.commands.impl.community; + +import de.presti.ree6.commands.Category; +import de.presti.ree6.commands.CommandEvent; +import de.presti.ree6.commands.interfaces.Command; +import de.presti.ree6.commands.interfaces.ICommand; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; + +/** + * This command is used to let the bot remember your Birthday. + */ +@Command(name = "birthday", description = "Let the bot remember your Birthday.", category = Category.COMMUNITY) +public class Birthday implements ICommand { + + /** + * @inheritDoc + */ + @Override + public void onPerform(CommandEvent commandEvent) { + + } + + /** + * @inheritDoc + */ + @Override + public CommandData getCommandData() { + return null; + } + + /** + * @inheritDoc + */ + @Override + public String[] getAlias() { + return new String[] {"bday"}; + } +} diff --git a/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java b/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java index bf1a54dbc..52a681d75 100644 --- a/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java +++ b/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java @@ -2,6 +2,7 @@ import de.presti.ree6.sql.base.annotations.Property; import de.presti.ree6.sql.base.annotations.Table; +import de.presti.ree6.sql.base.data.SQLEntity; import java.util.Date; @@ -9,7 +10,7 @@ * This class is used to represent a Birthday-Wish, in our Database. */ @Table(name = "BirthdayWish") -public class BirthdayWish { +public class BirthdayWish extends SQLEntity { /** * The Guild ID. From 185df087f53bf3f479031e22d9f2578e11f86c5f Mon Sep 17 00:00:00 2001 From: DxsSucuk Date: Thu, 28 Jul 2022 13:57:05 +0200 Subject: [PATCH 3/7] * Added support for Date mapping. * Clean up. --- .../de/presti/ree6/sql/base/data/SQLUtil.java | 84 +++++++++++-------- .../presti/ree6/sql/mapper/EntityMapper.java | 13 +-- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java b/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java index 6a7580862..71ba3be72 100644 --- a/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java +++ b/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java @@ -10,10 +10,7 @@ import java.io.InputStreamReader; import java.lang.reflect.Field; import java.sql.Blob; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Base64; -import java.util.List; +import java.util.*; /** * SQLUtil class to help with SQL-Queries. @@ -225,7 +222,7 @@ public static List getAllSQLParameter(Object entity, boolean onlyU * @param entityClass the Entity. * @param entityInstance the Entity instance. * @param onlyUpdateField if fields with the updateQuery value set to false should still be included. - * @param ignoreNull if null values should be ignored. + * @param ignoreNull if null values should be ignored. * @return {@link List} of {@link Object} as the {@link SQLParameter} value. */ public static List getValuesFromSQLEntity(Class entityClass, Object entityInstance, boolean onlyUpdateField, boolean ignoreNull) { @@ -241,22 +238,7 @@ public static List getValuesFromSQLEntity(Class entityClass, Object e return !onlyUpdateField || property.updateQuery(); }).map(field -> { try { - if (!field.canAccess(entityInstance)) field.trySetAccessible(); - - Property property = field.getAnnotation(Property.class); - - Object value = field.get(entityInstance); - - if (!property.keepOriginalValue()) { - if (value instanceof String valueString && - field.getType().isAssignableFrom(byte[].class)) { - value = Base64.getDecoder().decode(valueString); - } else if (value instanceof Blob blob) { - value = SQLUtil.convertBlobToJSON(blob); - } - } - - return value; + return getValueFromField(field, entityInstance); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -278,22 +260,7 @@ public static List getValuesFromSQLEntity(Class entityClass, Object e return !onlyUpdateField || property.updateQuery(); }).map(field -> { try { - if (!field.canAccess(entityInstance)) field.trySetAccessible(); - - Property property = field.getAnnotation(Property.class); - - Object value = field.get(entityInstance); - - if (!property.keepOriginalValue()) { - if (value instanceof String valueString && - field.getType().isAssignableFrom(byte[].class)) { - value = Base64.getDecoder().decode(valueString); - } else if (value instanceof Blob blob) { - value = SQLUtil.convertBlobToJSON(blob); - } - } - - return value; + return getValueFromField(field, entityInstance); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -308,8 +275,50 @@ public static List getValuesFromSQLEntity(Class entityClass, Object e return args; } + /** + * Get the value of the Field. + * + * @param field the Field. + * @param objectInstance the object instance. + * @return the value of the Field. + * @throws IllegalAccessException if the Field is not accessible. + */ + public static Object getValueFromField(Field field, Object objectInstance) throws IllegalAccessException { + if (!field.canAccess(objectInstance)) field.trySetAccessible(); + + Object value = field.get(objectInstance); + + value = mapCustomField(field, value); + + return value; + } + + + /** + * @param field the Field. + * @param currentValue the current value of the Field. + * @return the value of the Field. + */ + public static Object mapCustomField(Field field, Object currentValue) { + Property property = field.getAnnotation(Property.class); + if (property.keepOriginalValue()) return currentValue; + + if (currentValue instanceof String valueString && + field.getType().isAssignableFrom(byte[].class)) { + currentValue = Base64.getDecoder().decode(valueString); + } else if (currentValue instanceof Blob blob) { + currentValue = SQLUtil.convertBlobToJSON(blob); + } else if (currentValue instanceof Long longValue && + field.getType().isInstance(Date.class)) { + currentValue = new Date(longValue); + } + + return currentValue; + } + /** * Convert a Blob to a {@link JsonElement} + * * @param blob the Blob to convert. * @return the {@link JsonElement} or {@link JsonNull} if the Blob is null. */ @@ -335,6 +344,7 @@ public static JsonElement convertBlobToJSON(Blob blob) { /** * Convert a {@link JsonElement} to a Blob. + * * @param jsonElement the {@link JsonElement} to convert. * @return the Blob or null if the {@link JsonElement} is null. */ diff --git a/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java b/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java index 063046efc..1715578f6 100644 --- a/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java +++ b/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java @@ -9,8 +9,10 @@ import java.lang.reflect.Field; import java.sql.Blob; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Base64; +import java.util.Date; /** * This class is used to map an SQL Result into the right Class-Entity. @@ -93,16 +95,7 @@ public void setAllFields(StoredResultSet.StoredData resultSet, Object entity, Fi try { if (!field.canAccess(entity)) field.trySetAccessible(); - Object value = resultSet.getValue(columnName); - - if (!property.keepOriginalValue()) { - if (value instanceof String valueString && - field.getType().isAssignableFrom(byte[].class)) { - value = Base64.getDecoder().decode(valueString); - } else if (value instanceof Blob blob) { - value = SQLUtil.convertBlobToJSON(blob); - } - } + Object value = SQLUtil.mapCustomField(field, resultSet.getValue(columnName)); field.set(entity, value); } catch (Exception e) { From 8c199ca5c1309b2172ceba07e900ae4638d7cba8 Mon Sep 17 00:00:00 2001 From: DxsSucuk Date: Thu, 28 Jul 2022 15:19:09 +0200 Subject: [PATCH 4/7] * Actual command implementation. * Daily check. --- pom.xml | 5 ++ .../commands/impl/community/Birthday.java | 61 ++++++++++++++- src/main/java/de/presti/ree6/main/Main.java | 20 +++++ .../java/de/presti/ree6/sql/SQLWorker.java | 75 +++++++++++++++++++ 4 files changed, 159 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index bbdbb1a1b..836341252 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,11 @@ json 20220320 + + commons-validator + commons-validator + 1.7 + club.minnced discord-webhooks diff --git a/src/main/java/de/presti/ree6/commands/impl/community/Birthday.java b/src/main/java/de/presti/ree6/commands/impl/community/Birthday.java index c7a2f2ad7..e10dd5c95 100644 --- a/src/main/java/de/presti/ree6/commands/impl/community/Birthday.java +++ b/src/main/java/de/presti/ree6/commands/impl/community/Birthday.java @@ -4,7 +4,10 @@ import de.presti.ree6.commands.CommandEvent; import de.presti.ree6.commands.interfaces.Command; import de.presti.ree6.commands.interfaces.ICommand; +import de.presti.ree6.main.Main; +import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import org.apache.commons.validator.GenericValidator; /** * This command is used to let the bot remember your Birthday. @@ -17,7 +20,61 @@ public class Birthday implements ICommand { */ @Override public void onPerform(CommandEvent commandEvent) { - + if (commandEvent.isSlashCommand()) { + Main.getInstance().getCommandManager().sendMessage("This Command doesn't support slash commands yet.", commandEvent.getChannel(), commandEvent.getInteractionHook()); + return; + } + + if (commandEvent.getArguments().length == 1) { + if (commandEvent.getArguments()[0].equalsIgnoreCase("remove")) { + Main.getInstance().getSqlConnector().getSqlWorker().removeBirthday(commandEvent.getGuild().getId(), commandEvent.getMember().getId()); + Main.getInstance().getCommandManager().sendMessage("Your Birthday has been removed!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } else { + Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "birthday add/remove [Birthday(day.month.year)] [@User]", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } + if (commandEvent.getArguments().length == 2) { + if (commandEvent.getArguments()[0].equalsIgnoreCase("remove")) { + if (commandEvent.getMember().hasPermission(Permission.ADMINISTRATOR)) { + if (commandEvent.getMessage() != null && + commandEvent.getMessage().getMentions().getMembers().isEmpty()) { + Main.getInstance().getCommandManager().sendMessage("Please mention a user!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } else { + Main.getInstance().getSqlConnector().getSqlWorker().removeBirthday(commandEvent.getGuild().getId(), commandEvent.getMessage().getMentions().getMembers().get(0).getId()); + Main.getInstance().getCommandManager().sendMessage("The Birthday of <@" + commandEvent.getMessage().getMentions().getMembers().get(0).getId() + "> has been removed!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } else { + Main.getInstance().getCommandManager().sendMessage("You don't have the permission to remove a Birthday!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } else if (commandEvent.getArguments()[0].equalsIgnoreCase("add")) { + if (GenericValidator.isDate(commandEvent.getArguments()[1], "dd.MM.yyyy", true)) { + Main.getInstance().getSqlConnector().getSqlWorker().addBirthday(commandEvent.getGuild().getId(), commandEvent.getChannel().getId(), commandEvent.getMember().getId(), commandEvent.getArguments()[1]); + Main.getInstance().getCommandManager().sendMessage("Your Birthday has been added!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } else { + Main.getInstance().getCommandManager().sendMessage("Please use a valid Date!\nNote that we use the the format dd.MM.yyyy (day.month.year)!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } else { + Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "birthday add/remove [Birthday(day.month.year)] [@User]", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } else if (commandEvent.getArguments().length == 3) { + if (commandEvent.getArguments()[0].equalsIgnoreCase("add")) { + if (GenericValidator.isDate(commandEvent.getArguments()[1], "dd.MM.yyyy", true)) { + if (commandEvent.getMessage() != null && + commandEvent.getMessage().getMentions().getMembers().isEmpty()) { + Main.getInstance().getCommandManager().sendMessage("Please mention a user!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } else { + Main.getInstance().getSqlConnector().getSqlWorker().addBirthday(commandEvent.getGuild().getId(), commandEvent.getChannel().getId(), commandEvent.getMessage().getMentions().getMembers().get(0).getId(), commandEvent.getArguments()[1]); + Main.getInstance().getCommandManager().sendMessage("The Birthday of <@" + commandEvent.getMessage().getMentions().getMembers().get(0).getId() + "> has been added!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } else { + Main.getInstance().getCommandManager().sendMessage("Please use a valid Date!\nNote that we use the the format dd.MM.yyyy (day.month.year)!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } else { + Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "birthday add/remove [Birthday(day.month.year)] [@User]", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } + } else { + Main.getInstance().getCommandManager().sendMessage("Please use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "birthday add/remove [Birthday(day.month.year)] [@User]", 5, commandEvent.getChannel(), commandEvent.getInteractionHook()); + } } /** @@ -33,6 +90,6 @@ public CommandData getCommandData() { */ @Override public String[] getAlias() { - return new String[] {"bday"}; + return new String[]{"bday"}; } } diff --git a/src/main/java/de/presti/ree6/main/Main.java b/src/main/java/de/presti/ree6/main/Main.java index 2de4b967a..42d90cd3d 100644 --- a/src/main/java/de/presti/ree6/main/Main.java +++ b/src/main/java/de/presti/ree6/main/Main.java @@ -14,17 +14,21 @@ import de.presti.ree6.events.OtherEvents; import de.presti.ree6.logger.events.LoggerQueue; import de.presti.ree6.sql.SQLConnector; +import de.presti.ree6.sql.entities.BirthdayWish; import de.presti.ree6.utils.apis.Notifier; import de.presti.ree6.utils.data.ArrayUtil; import de.presti.ree6.utils.data.Config; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.TextChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; +import java.util.List; /** * Main Application class, used to store Instances of System Relevant classes. @@ -283,6 +287,22 @@ public void createCheckerThread() { instance.logger.info("[Stats] Guilds: {}", BotWorker.getShardManager().getGuilds().size()); instance.logger.info("[Stats] Overall Users: {}", BotWorker.getShardManager().getGuilds().stream().mapToInt(Guild::getMemberCount).sum()); instance.logger.info("[Stats] "); + + Calendar currentCalendar = Calendar.getInstance(); + + getSqlConnector().getSqlWorker() + .getBirthdays().stream().filter(birthday -> { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(birthday.getBirthdate()); + return calendar.get(Calendar.MONTH) == currentCalendar.get(Calendar.MONTH) && + calendar.get(Calendar.DAY_OF_MONTH) == currentCalendar.get(Calendar.DAY_OF_MONTH); + }).forEach(birthday -> { + TextChannel textChannel = BotWorker.getShardManager().getTextChannelById(birthday.getChannelId()); + + if (textChannel != null && textChannel.canTalk()) + textChannel.sendMessage("Happy birthday to <@" + birthday.getUserId() + ">!").queue(); + }); + lastDay = new SimpleDateFormat("dd").format(new Date()); } diff --git a/src/main/java/de/presti/ree6/sql/SQLWorker.java b/src/main/java/de/presti/ree6/sql/SQLWorker.java index cde846ceb..587f52b68 100644 --- a/src/main/java/de/presti/ree6/sql/SQLWorker.java +++ b/src/main/java/de/presti/ree6/sql/SQLWorker.java @@ -11,6 +11,7 @@ import de.presti.ree6.sql.base.data.SQLParameter; import de.presti.ree6.sql.base.data.SQLResponse; import de.presti.ree6.sql.base.data.SQLUtil; +import de.presti.ree6.sql.entities.BirthdayWish; import de.presti.ree6.sql.entities.Blacklist; import de.presti.ree6.sql.entities.Invite; import de.presti.ree6.sql.entities.Setting; @@ -27,6 +28,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.*; /** @@ -1784,6 +1787,78 @@ public void optIn(String guildId, String userId) { //endregion + //region Birthday + + /** + * Store the birthday of the user in the database + * @param guildId the ID of the Guild. + * @param channelId the ID of the Channel. + * @param userId the ID of the User. + * @param birthday the birthday of the user. + */ + public void addBirthday(String guildId, String channelId, String userId, String birthday) { + try { + if (isBirthdaySaved(guildId, userId)) { + BirthdayWish newBirthday = new BirthdayWish(guildId, channelId, userId, new SimpleDateFormat("dd.MM.yyyy").parse(birthday)); + updateEntity(getBirthday(guildId, userId), newBirthday, true); + } else { + saveEntity(new BirthdayWish(guildId, channelId, userId, new SimpleDateFormat("dd.MM.yyyy").parse(birthday))); + } + } catch (ParseException e) { + e.printStackTrace(); + } + } + + /** + * Check if there is any saved birthday for the given User. + * @param guildId the ID of the Guild. + * @param userId the ID of the User. + */ + public void removeBirthday(String guildId, String userId) { + if (isBirthdaySaved(guildId, userId)) { + sqlConnector.querySQL("DELETE FROM Birthday WHERE GID=? AND UID=?", guildId, userId); + } + } + + /** + * Check if a birthday is saved for the given User. + * @param guildId the ID of the Guild. + * @param userId the ID of the User. + * @return {@link Boolean} as result. If true, there is data saved in the Database | If false, there is no data saved. + */ + public boolean isBirthdaySaved(String guildId, String userId) { + return sqlConnector.querySQL("SELECT * FROM Birthday WHERE GID=? AND UID=?", guildId, userId).hasResults(); + } + + /** + * Get the birthday of the given User. + * @param guildId the ID of the Guild. + * @param userId the ID of the User. + * @return {@link BirthdayWish} as result. If true, there is data saved in the Database | If false, there is no data saved. + */ + public BirthdayWish getBirthday(String guildId, String userId) { + return (BirthdayWish) getEntity(BirthdayWish.class, "SELECT * FROM Birthday WHERE GID=? AND UID=?", guildId, userId).getEntity(); + } + + /** + * Get all saved birthdays. + * @param guildId the ID of the Guild. + * @return {@link List} of {@link BirthdayWish} as result. If true, there is data saved in the Database | If false, there is no data saved. + */ + public List getBirthdays(String guildId) { + return getEntity(BirthdayWish.class, "SELECT * FROM Birthday WHERE GID=?", guildId).getEntities().stream().map(BirthdayWish.class::cast).toList(); + } + + /** + * Get all saved birthdays. + * @return {@link List} of {@link BirthdayWish} as result. If true, there is data saved in the Database | If false, there is no data saved. + */ + public List getBirthdays() { + return getEntity(BirthdayWish.class, "SELECT * FROM Birthday").getEntities().stream().map(BirthdayWish.class::cast).toList(); + } + + //endregion + //region Data delete /** From 3ecd0e90218f74697f7a1429752df2ffbed962bf Mon Sep 17 00:00:00 2001 From: DxsSucuk Date: Thu, 28 Jul 2022 17:23:29 +0200 Subject: [PATCH 5/7] * Tested and working. --- src/main/java/de/presti/ree6/main/Main.java | 34 +++---------------- .../java/de/presti/ree6/sql/SQLConnector.java | 3 ++ .../java/de/presti/ree6/sql/SQLWorker.java | 10 +++--- .../de/presti/ree6/sql/base/data/SQLUtil.java | 4 +-- .../ree6/sql/entities/BirthdayWish.java | 2 +- 5 files changed, 15 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/presti/ree6/main/Main.java b/src/main/java/de/presti/ree6/main/Main.java index 62b796c38..a4bca3035 100644 --- a/src/main/java/de/presti/ree6/main/Main.java +++ b/src/main/java/de/presti/ree6/main/Main.java @@ -18,6 +18,7 @@ import de.presti.ree6.utils.apis.Notifier; import de.presti.ree6.utils.data.ArrayUtil; import de.presti.ree6.utils.data.Config; +import de.presti.ree6.utils.others.ThreadUtil; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Guild; @@ -27,6 +28,7 @@ import org.slf4j.LoggerFactory; import java.text.SimpleDateFormat; +import java.time.Duration; import java.util.Calendar; import java.util.Date; import java.util.List; @@ -86,11 +88,6 @@ public class Main { */ Config config; - /** - * A Thread used to check if a day has passed, and if so to clean the cache. - */ - Thread checker; - /** * String used to identify the last day. */ @@ -254,13 +251,6 @@ private void shutdown() { getNotifier().getTwitchClient().close(); instance.logger.info("[Main] Twitch API Instance closed!"); - if (checker != null && !checker.isInterrupted()) { - // Shutdown Checker Thread. - instance.logger.info("[Main] Interrupting the Checker thread!"); - checker.interrupt(); - instance.logger.info("[Main] Interrupted the Checker thread!"); - } - // Shutdown the Bot instance. instance.logger.info("[Main] JDA Instance shutdown init. !"); BotWorker.shutdown(); @@ -275,7 +265,7 @@ private void shutdown() { * Method creates a Thread used to create a Checker Thread. */ public void createCheckerThread() { - checker = new Thread(() -> { + ThreadUtil.createNewThread(x -> { while (BotWorker.getState() != BotState.STOPPED) { if (!lastDay.equalsIgnoreCase(new SimpleDateFormat("dd").format(new Date()))) { @@ -334,15 +324,8 @@ public void createCheckerThread() { getLogger().error("Error accessing the AudioPlayer.", ex); } } - - try { - Thread.sleep((10 * (60000L))); - } catch (InterruptedException exception) { - getInstance().getLogger().error("Checker Thread interrupted", exception); - } } - }); - checker.start(); + }, null, Duration.ofMinutes(1), true, false); } /** @@ -436,15 +419,6 @@ public Logger getAnalyticsLogger() { return analyticsLogger; } - /** - * Retrieve the Instance of the Checker-Thread. - * - * @return {@link Thread} Instance of the Checker-Thread. - */ - public Thread getChecker() { - return checker; - } - /** * Retrieve the Instance of the Config. * diff --git a/src/main/java/de/presti/ree6/sql/SQLConnector.java b/src/main/java/de/presti/ree6/sql/SQLConnector.java index 5e5785bdc..dd65c507c 100644 --- a/src/main/java/de/presti/ree6/sql/SQLConnector.java +++ b/src/main/java/de/presti/ree6/sql/SQLConnector.java @@ -12,6 +12,7 @@ import java.sql.*; import java.util.Base64; +import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -192,6 +193,8 @@ public StoredResultSet querySQL(String sqlQuery, Object... objcObjects) { preparedStatement.setObject(index++, SQLUtil.convertJSONToBlob(jsonElement), Types.BLOB); } else if (obj instanceof byte[] byteArray) { preparedStatement.setObject(index++, Base64.getEncoder().encodeToString(byteArray), Types.VARCHAR); + } else if (obj instanceof Date date) { + preparedStatement.setObject(index++, date.getTime(), Types.BIGINT); } } diff --git a/src/main/java/de/presti/ree6/sql/SQLWorker.java b/src/main/java/de/presti/ree6/sql/SQLWorker.java index 587f52b68..4a2ae1758 100644 --- a/src/main/java/de/presti/ree6/sql/SQLWorker.java +++ b/src/main/java/de/presti/ree6/sql/SQLWorker.java @@ -1816,7 +1816,7 @@ public void addBirthday(String guildId, String channelId, String userId, String */ public void removeBirthday(String guildId, String userId) { if (isBirthdaySaved(guildId, userId)) { - sqlConnector.querySQL("DELETE FROM Birthday WHERE GID=? AND UID=?", guildId, userId); + sqlConnector.querySQL("DELETE FROM BirthdayWish WHERE GID=? AND UID=?", guildId, userId); } } @@ -1827,7 +1827,7 @@ public void removeBirthday(String guildId, String userId) { * @return {@link Boolean} as result. If true, there is data saved in the Database | If false, there is no data saved. */ public boolean isBirthdaySaved(String guildId, String userId) { - return sqlConnector.querySQL("SELECT * FROM Birthday WHERE GID=? AND UID=?", guildId, userId).hasResults(); + return sqlConnector.querySQL("SELECT * FROM BirthdayWish WHERE GID=? AND UID=?", guildId, userId).hasResults(); } /** @@ -1837,7 +1837,7 @@ public boolean isBirthdaySaved(String guildId, String userId) { * @return {@link BirthdayWish} as result. If true, there is data saved in the Database | If false, there is no data saved. */ public BirthdayWish getBirthday(String guildId, String userId) { - return (BirthdayWish) getEntity(BirthdayWish.class, "SELECT * FROM Birthday WHERE GID=? AND UID=?", guildId, userId).getEntity(); + return (BirthdayWish) getEntity(BirthdayWish.class, "SELECT * FROM BirthdayWish WHERE GID=? AND UID=?", guildId, userId).getEntity(); } /** @@ -1846,7 +1846,7 @@ public BirthdayWish getBirthday(String guildId, String userId) { * @return {@link List} of {@link BirthdayWish} as result. If true, there is data saved in the Database | If false, there is no data saved. */ public List getBirthdays(String guildId) { - return getEntity(BirthdayWish.class, "SELECT * FROM Birthday WHERE GID=?", guildId).getEntities().stream().map(BirthdayWish.class::cast).toList(); + return getEntity(BirthdayWish.class, "SELECT * FROM BirthdayWish WHERE GID=?", guildId).getEntities().stream().map(BirthdayWish.class::cast).toList(); } /** @@ -1854,7 +1854,7 @@ public List getBirthdays(String guildId) { * @return {@link List} of {@link BirthdayWish} as result. If true, there is data saved in the Database | If false, there is no data saved. */ public List getBirthdays() { - return getEntity(BirthdayWish.class, "SELECT * FROM Birthday").getEntities().stream().map(BirthdayWish.class::cast).toList(); + return getEntity(BirthdayWish.class, "SELECT * FROM BirthdayWish").getEntities().stream().map(BirthdayWish.class::cast).toList(); } //endregion diff --git a/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java b/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java index 71ba3be72..baea63fd2 100644 --- a/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java +++ b/src/main/java/de/presti/ree6/sql/base/data/SQLUtil.java @@ -67,7 +67,7 @@ public static String mapJavaToSQL(Class javaObjectClass) { javaObjectClass.isAssignableFrom(char.class)) { return "CHAR(1)"; } else if (javaObjectClass.isAssignableFrom(java.util.Date.class)) { - return "DATETIME"; + return "BIGINT"; } else if (javaObjectClass.isAssignableFrom(java.sql.Date.class)) { return "DATE"; } else if (javaObjectClass.isAssignableFrom(java.sql.Time.class)) { @@ -309,7 +309,7 @@ public static Object mapCustomField(Field field, Object currentValue) { } else if (currentValue instanceof Blob blob) { currentValue = SQLUtil.convertBlobToJSON(blob); } else if (currentValue instanceof Long longValue && - field.getType().isInstance(Date.class)) { + field.getType().isAssignableFrom(Date.class)) { currentValue = new Date(longValue); } diff --git a/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java b/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java index 52a681d75..25e9a1b53 100644 --- a/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java +++ b/src/main/java/de/presti/ree6/sql/entities/BirthdayWish.java @@ -33,7 +33,7 @@ public class BirthdayWish extends SQLEntity { /** * The Birthday. */ - @Property(name = "birthday") + @Property(name = "birthday", keepOriginalValue = false) Date birthdate; /** From a6e46cc27612c1701f7934a843de8568fc0c7919 Mon Sep 17 00:00:00 2001 From: DxsSucuk Date: Thu, 28 Jul 2022 17:24:52 +0200 Subject: [PATCH 6/7] * Cleanup. --- src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java b/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java index 1715578f6..68ef88cbe 100644 --- a/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java +++ b/src/main/java/de/presti/ree6/sql/mapper/EntityMapper.java @@ -8,11 +8,7 @@ import de.presti.ree6.sql.base.data.StoredResultSet; import java.lang.reflect.Field; -import java.sql.Blob; -import java.time.LocalDateTime; import java.util.ArrayList; -import java.util.Base64; -import java.util.Date; /** * This class is used to map an SQL Result into the right Class-Entity. From 2c9ff48defd3468acd830c88fedb06499b609e36 Mon Sep 17 00:00:00 2001 From: DxsSucuk Date: Thu, 28 Jul 2022 17:29:19 +0200 Subject: [PATCH 7/7] * Cleanup. --- src/main/java/de/presti/ree6/main/Main.java | 2 -- src/main/java/de/presti/ree6/sql/SQLWorker.java | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/de/presti/ree6/main/Main.java b/src/main/java/de/presti/ree6/main/Main.java index a4bca3035..7e012b80e 100644 --- a/src/main/java/de/presti/ree6/main/Main.java +++ b/src/main/java/de/presti/ree6/main/Main.java @@ -14,7 +14,6 @@ import de.presti.ree6.events.OtherEvents; import de.presti.ree6.logger.events.LoggerQueue; import de.presti.ree6.sql.SQLConnector; -import de.presti.ree6.sql.entities.BirthdayWish; import de.presti.ree6.utils.apis.Notifier; import de.presti.ree6.utils.data.ArrayUtil; import de.presti.ree6.utils.data.Config; @@ -31,7 +30,6 @@ import java.time.Duration; import java.util.Calendar; import java.util.Date; -import java.util.List; /** * Main Application class, used to store Instances of System Relevant classes. diff --git a/src/main/java/de/presti/ree6/sql/SQLWorker.java b/src/main/java/de/presti/ree6/sql/SQLWorker.java index 4a2ae1758..67fa8cecb 100644 --- a/src/main/java/de/presti/ree6/sql/SQLWorker.java +++ b/src/main/java/de/presti/ree6/sql/SQLWorker.java @@ -1804,9 +1804,7 @@ public void addBirthday(String guildId, String channelId, String userId, String } else { saveEntity(new BirthdayWish(guildId, channelId, userId, new SimpleDateFormat("dd.MM.yyyy").parse(birthday))); } - } catch (ParseException e) { - e.printStackTrace(); - } + } catch (ParseException ignore) {} } /**