From afbebbdb46786958b31131602cb5336d24d0b1d5 Mon Sep 17 00:00:00 2001 From: vluckyyy Date: Wed, 25 Oct 2023 15:32:04 +0200 Subject: [PATCH 01/11] Start: Send levelup message at the same channel. --- .../discordapp/leveling/LevelConfig.java | 3 --- .../discordapp/leveling/LevelController.java | 20 ++++++++++++++----- .../experience/ExperienceChangeEvent.java | 2 +- .../experience/ExperienceService.java | 10 +++++----- .../listener/ExperienceMessageListener.java | 3 +-- .../listener/ExperienceReactionListener.java | 4 ++-- .../listener/ExperienceVoiceListener.java | 2 +- .../games/CodeGameAnswerController.java | 2 +- 8 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelConfig.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelConfig.java index 13ee3603..2cecf2f6 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelConfig.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelConfig.java @@ -13,9 +13,6 @@ public class LevelConfig implements CdnConfig { @Description("# The count of points that will be added to the user's level") public int points = 100; - @Description("# Channel where the message will be sent") - public long channel = 0L; - public Message message = new Message(); @Contextual diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java index ad437be8..17cac3c3 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -3,11 +3,14 @@ import com.eternalcode.discordapp.leveling.experience.Experience; import com.eternalcode.discordapp.leveling.experience.ExperienceChangeEvent; import com.eternalcode.discordapp.observer.Observer; +import io.sentry.Sentry; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import panda.utilities.text.Formatter; +import java.util.concurrent.TimeUnit; + public class LevelController implements Observer { private final LevelConfig levelConfig; @@ -39,8 +42,8 @@ public LevelController(LevelConfig levelConfig, LevelService levelService, JDA j */ @Override - public void update(ExperienceChangeEvent experienceChangeEvent) { - Experience experience = experienceChangeEvent.experience(); + public void update(ExperienceChangeEvent event) { + Experience experience = event.experience(); long userId = experience.getUserId(); double experiencePoints = experience.getPoints(); @@ -73,11 +76,18 @@ public void update(ExperienceChangeEvent experienceChangeEvent) { .register("{level}", String.valueOf(newLevel)) .format(this.levelConfig.message.description); - TextChannel levelChannel = this.jda.getTextChannelById(this.levelConfig.channel); - if (levelChannel != null) { - levelChannel.sendMessage(messageContent).queue(); + + try { + TextChannel channel = this.jda.getTextChannelById(event.channelId()); + channel.sendMessage(messageContent).queue(message -> { + message.delete().queueAfter(5, TimeUnit.SECONDS); + }); + } catch (Exception exception) { + Sentry.captureException(exception); + exception.printStackTrace(); } + return userLevel; }); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java index ed57ea65..e6b55474 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java @@ -1,4 +1,4 @@ package com.eternalcode.discordapp.leveling.experience; -public record ExperienceChangeEvent(Experience experience) { +public record ExperienceChangeEvent(Experience experience, long channelId) { } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java index 2028a354..abab68c6 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java @@ -16,18 +16,18 @@ public ExperienceService(DatabaseManager databaseManager, ObserverRegistry obser this.observerRegistry = observerRegistry; } - public CompletableFuture saveExperience(Experience experience) { + public CompletableFuture saveExperience(Experience experience, long channelId) { return this.experienceRepository.saveExperience(experience).whenComplete((experience1, throwable) -> { if (throwable == null) { - this.observerRegistry.publish(new ExperienceChangeEvent(experience1)); + this.observerRegistry.publish(new ExperienceChangeEvent(experience1, channelId)); } }); } - public CompletableFuture modifyPoints(long id, double points, boolean add) { - return this.experienceRepository.modifyPoints(id, points, add).whenComplete((experience, throwable) -> { + public CompletableFuture modifyPoints(long userId, double points, boolean add, long channelId) { + return this.experienceRepository.modifyPoints(userId, points, add).whenComplete((experience, throwable) -> { if (throwable == null) { - this.observerRegistry.publish(new ExperienceChangeEvent(experience)); + this.observerRegistry.publish(new ExperienceChangeEvent(experience, channelId)); } }); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java index 5c507113..feb84a1f 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java @@ -21,7 +21,6 @@ public void onMessageReceived(MessageReceivedEvent event) { return; } - this.givePoints(event); } @@ -36,7 +35,7 @@ private void givePoints(MessageReceivedEvent event) { double points = (double) message.length / this.experienceConfig.messageExperience.howManyWords * basePoints; long userId = event.getAuthor().getIdLong(); - this.experienceService.modifyPoints(userId, points, true).whenComplete((experience, throwable) -> { + this.experienceService.modifyPoints(userId, points, true, event.getChannel().getIdLong()).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java index 80f0ed9a..d24d6f86 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java @@ -21,7 +21,7 @@ public void onMessageReactionAdd(MessageReactionAddEvent event) { long userId = event.getUserIdLong(); double points = this.experienceConfig.basePoints * this.experienceConfig.reactionExperience.multiplier; - this.experienceService.modifyPoints(userId, points, true).whenComplete((experience, throwable) -> { + this.experienceService.modifyPoints(userId, points, true, event.getChannel().getIdLong()).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } @@ -33,7 +33,7 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) { long userId = event.getUserIdLong(); double points = this.experienceConfig.basePoints * this.experienceConfig.reactionExperience.multiplier; - this.experienceService.modifyPoints(userId, points, true).whenComplete((experience, throwable) -> { + this.experienceService.modifyPoints(userId, points, true, event.getChannel().getIdLong()).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java index d0d34db8..1b3ac041 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java @@ -51,7 +51,7 @@ private void leaveVoiceChannel(GuildVoiceUpdateEvent event) { long userId = event.getMember().getIdLong(); this.usersVoiceActivityData.usersOnVoiceChannel.remove(event.getMember().getIdLong()); - this.experienceService.modifyPoints(userId, this.calculatePoints(event), true).whenComplete((experience, throwable) -> { + this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, event.getChannelJoined().getIdLong()).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java index fbd28bdf..5648d9e5 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java @@ -66,7 +66,7 @@ public void onMessageReceived(MessageReceivedEvent event) { .setDescription(formatter.format(this.codeGameConfiguration.embedSettings.description)) .setFooter(this.codeGameConfiguration.embedSettings.footer); - this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true) + this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true, event.getChannel().getIdLong()) .whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); From 43014636d57bda84337a7d2edaf15c62d903c3a9 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 19:40:47 +0200 Subject: [PATCH 02/11] Add option to send level-up message at the same channel. --- .../discordapp/leveling/LevelController.java | 21 +++++++++---- .../listener/ExperienceMessageListener.java | 3 +- .../listener/ExperienceReactionListener.java | 30 ++++++++++++++----- .../listener/ExperienceVoiceListener.java | 4 ++- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java index 17cac3c3..07fed06b 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -6,9 +6,13 @@ import io.sentry.Sentry; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.Channel; +import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import panda.utilities.text.Formatter; +import java.util.Optional; import java.util.concurrent.TimeUnit; public class LevelController implements Observer { @@ -78,16 +82,21 @@ public void update(ExperienceChangeEvent event) { try { - TextChannel channel = this.jda.getTextChannelById(event.channelId()); - channel.sendMessage(messageContent).queue(message -> { - message.delete().queueAfter(5, TimeUnit.SECONDS); - }); - } catch (Exception exception) { + Optional textChannelOptional = Optional.ofNullable(this.jda.getTextChannelById(event.channelId())); + + MessageChannel channel = textChannelOptional.orElse(this.jda.getPrivateChannelById(event.channelId())); + + if (channel == null) { + return null; + } + + channel.sendMessage(messageContent).queue(message -> message.delete().queueAfter(5, TimeUnit.SECONDS)); + } + catch (Exception exception) { Sentry.captureException(exception); exception.printStackTrace(); } - return userLevel; }); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java index feb84a1f..87020c01 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java @@ -35,7 +35,8 @@ private void givePoints(MessageReceivedEvent event) { double points = (double) message.length / this.experienceConfig.messageExperience.howManyWords * basePoints; long userId = event.getAuthor().getIdLong(); - this.experienceService.modifyPoints(userId, points, true, event.getChannel().getIdLong()).whenComplete((experience, throwable) -> { + long idLong = event.getChannel().getIdLong(); + this.experienceService.modifyPoints(userId, points, true, idLong).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java index d24d6f86..d8b7b8fe 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java @@ -2,6 +2,8 @@ import com.eternalcode.discordapp.leveling.experience.ExperienceConfig; import com.eternalcode.discordapp.leveling.experience.ExperienceService; +import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -21,10 +23,16 @@ public void onMessageReactionAdd(MessageReactionAddEvent event) { long userId = event.getUserIdLong(); double points = this.experienceConfig.basePoints * this.experienceConfig.reactionExperience.multiplier; - this.experienceService.modifyPoints(userId, points, true, event.getChannel().getIdLong()).whenComplete((experience, throwable) -> { - if (throwable != null) { - throwable.printStackTrace(); - } + if (event.getUser().isBot()) { + return; + } + + event.getUser().openPrivateChannel().queue(channel -> { + this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()).whenComplete((experience, throwable) -> { + if (throwable != null) { + throwable.printStackTrace(); + } + }); }); } @@ -33,10 +41,16 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) { long userId = event.getUserIdLong(); double points = this.experienceConfig.basePoints * this.experienceConfig.reactionExperience.multiplier; - this.experienceService.modifyPoints(userId, points, true, event.getChannel().getIdLong()).whenComplete((experience, throwable) -> { - if (throwable != null) { - throwable.printStackTrace(); - } + if (event.getUser().isBot()) { + return; + } + + event.getUser().openPrivateChannel().queue(channel -> { + this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()).whenComplete((experience, throwable) -> { + if (throwable != null) { + throwable.printStackTrace(); + } + }); }); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java index 1b3ac041..4baed6f0 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java @@ -51,7 +51,9 @@ private void leaveVoiceChannel(GuildVoiceUpdateEvent event) { long userId = event.getMember().getIdLong(); this.usersVoiceActivityData.usersOnVoiceChannel.remove(event.getMember().getIdLong()); - this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, event.getChannelJoined().getIdLong()).whenComplete((experience, throwable) -> { + + long idLong = event.getChannelLeft().getIdLong(); + this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, idLong).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } From ad1ea4b04683b68b50e1b9bccc5a8e58bb392034 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 19:51:05 +0200 Subject: [PATCH 03/11] Refactor private channel handling. --- .../listener/ExperienceReactionListener.java | 27 +++++++++---------- .../games/CodeGameAnswerController.java | 6 +++-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java index d8b7b8fe..32dc97df 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java @@ -2,6 +2,7 @@ import com.eternalcode.discordapp.leveling.experience.ExperienceConfig; import com.eternalcode.discordapp.leveling.experience.ExperienceService; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; @@ -27,13 +28,7 @@ public void onMessageReactionAdd(MessageReactionAddEvent event) { return; } - event.getUser().openPrivateChannel().queue(channel -> { - this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()).whenComplete((experience, throwable) -> { - if (throwable != null) { - throwable.printStackTrace(); - } - }); - }); + this.modifyPoints(event.getUser(), userId, points); } @Override @@ -45,13 +40,17 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) { return; } - event.getUser().openPrivateChannel().queue(channel -> { - this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()).whenComplete((experience, throwable) -> { - if (throwable != null) { - throwable.printStackTrace(); - } - }); - }); + this.modifyPoints(event.getUser(), userId, points); + } + + private void modifyPoints(User event, long userId, double points) { + event.openPrivateChannel().queue(channel -> this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()) + .whenComplete((experience, throwable) -> { + + if (throwable != null) { + throwable.printStackTrace(); + } + })); } } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java index 5648d9e5..1512f0db 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java @@ -34,7 +34,9 @@ public void onMessageReceived(MessageReceivedEvent event) { return; } - if (event.getChannel().getIdLong() != this.codeGameConfiguration.channelId) { + long idLong = event.getChannel().getIdLong(); + + if (idLong != this.codeGameConfiguration.channelId) { return; } @@ -66,7 +68,7 @@ public void onMessageReceived(MessageReceivedEvent event) { .setDescription(formatter.format(this.codeGameConfiguration.embedSettings.description)) .setFooter(this.codeGameConfiguration.embedSettings.footer); - this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true, event.getChannel().getIdLong()) + this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true, idLong) .whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); From a76a3bdbc1bf07ec769915d8581031da18962413 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 20:01:26 +0200 Subject: [PATCH 04/11] Follow dmk suggestions. --- .../experience/listener/ExperienceMessageListener.java | 4 ++-- .../discordapp/leveling/games/CodeGameAnswerController.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java index 87020c01..9d11c13e 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java @@ -35,8 +35,8 @@ private void givePoints(MessageReceivedEvent event) { double points = (double) message.length / this.experienceConfig.messageExperience.howManyWords * basePoints; long userId = event.getAuthor().getIdLong(); - long idLong = event.getChannel().getIdLong(); - this.experienceService.modifyPoints(userId, points, true, idLong).whenComplete((experience, throwable) -> { + long channelId = event.getChannel().getIdLong(); + this.experienceService.modifyPoints(userId, points, true, channelId).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java index 1512f0db..4ac24ccc 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java @@ -34,9 +34,9 @@ public void onMessageReceived(MessageReceivedEvent event) { return; } - long idLong = event.getChannel().getIdLong(); + long channelId = event.getChannel().getIdLong(); - if (idLong != this.codeGameConfiguration.channelId) { + if (channelId != this.codeGameConfiguration.channelId) { return; } @@ -68,7 +68,7 @@ public void onMessageReceived(MessageReceivedEvent event) { .setDescription(formatter.format(this.codeGameConfiguration.embedSettings.description)) .setFooter(this.codeGameConfiguration.embedSettings.footer); - this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true, idLong) + this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true, channelId) .whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); From 2d6d6f494ffd7e734168184f60d216dc5f439997 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 20:03:11 +0200 Subject: [PATCH 05/11] Follow dmk suggestions. --- .../listener/ExperienceReactionListener.java | 12 ++++++++---- .../experience/listener/ExperienceVoiceListener.java | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java index 32dc97df..8f3ae20b 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java @@ -24,11 +24,13 @@ public void onMessageReactionAdd(MessageReactionAddEvent event) { long userId = event.getUserIdLong(); double points = this.experienceConfig.basePoints * this.experienceConfig.reactionExperience.multiplier; - if (event.getUser().isBot()) { + User user = event.getUser(); + + if (user.isBot()) { return; } - this.modifyPoints(event.getUser(), userId, points); + this.modifyPoints(user, userId, points); } @Override @@ -36,11 +38,13 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) { long userId = event.getUserIdLong(); double points = this.experienceConfig.basePoints * this.experienceConfig.reactionExperience.multiplier; - if (event.getUser().isBot()) { + User user = event.getUser(); + + if (user.isBot()) { return; } - this.modifyPoints(event.getUser(), userId, points); + this.modifyPoints(user, userId, points); } private void modifyPoints(User event, long userId, double points) { diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java index 4baed6f0..0d5f2f51 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java @@ -52,8 +52,8 @@ private void leaveVoiceChannel(GuildVoiceUpdateEvent event) { long userId = event.getMember().getIdLong(); this.usersVoiceActivityData.usersOnVoiceChannel.remove(event.getMember().getIdLong()); - long idLong = event.getChannelLeft().getIdLong(); - this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, idLong).whenComplete((experience, throwable) -> { + long voiceLeftChannelId = event.getChannelLeft().getIdLong(); + this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, voiceLeftChannelId).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); } From 1c099e0b1db03d9ed00e9bf82cefcf03f6644d05 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 20:13:13 +0200 Subject: [PATCH 06/11] Follow dmk suggestions. --- .../eternalcode/discordapp/leveling/LevelController.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java index 07fed06b..2d29bd56 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -82,9 +82,10 @@ public void update(ExperienceChangeEvent event) { try { - Optional textChannelOptional = Optional.ofNullable(this.jda.getTextChannelById(event.channelId())); + long channelId = event.channelId(); - MessageChannel channel = textChannelOptional.orElse(this.jda.getPrivateChannelById(event.channelId())); + MessageChannel channel = this.isPrivateChannel(channelId) ? this.jda.getPrivateChannelById(channelId) + : this.jda.getTextChannelById(channelId); if (channel == null) { return null; @@ -100,4 +101,8 @@ public void update(ExperienceChangeEvent event) { return userLevel; }); } + + public boolean isPrivateChannel(long channelId) { + return this.jda.getPrivateChannelById(channelId) != null; + } } From ca663e0212ade481075234c7c7a90f9d262f0bbe Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 20:18:37 +0200 Subject: [PATCH 07/11] Follow rollczi suggestions. --- .../eternalcode/discordapp/leveling/LevelController.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java index 2d29bd56..0ac37654 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -84,8 +84,7 @@ public void update(ExperienceChangeEvent event) { try { long channelId = event.channelId(); - MessageChannel channel = this.isPrivateChannel(channelId) ? this.jda.getPrivateChannelById(channelId) - : this.jda.getTextChannelById(channelId); + MessageChannel channel = this.getChannelById(channelId); if (channel == null) { return null; @@ -102,7 +101,9 @@ public void update(ExperienceChangeEvent event) { }); } - public boolean isPrivateChannel(long channelId) { - return this.jda.getPrivateChannelById(channelId) != null; + private MessageChannel getChannelById(long channelId) { + MessageChannel channel = this.jda.getPrivateChannelById(channelId); + + return channel != null ? channel : this.jda.getTextChannelById(channelId); } } From f8bd9f2bc98c18d2772e1c6675f44b3ca83c025f Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 20:49:02 +0200 Subject: [PATCH 08/11] Follow rollczi suggestions. --- .../discordapp/leveling/LevelController.java | 19 ++++++------------- .../listener/ExperienceReactionListener.java | 15 +++++++-------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java index 0ac37654..5b7c0b2e 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -3,17 +3,13 @@ import com.eternalcode.discordapp.leveling.experience.Experience; import com.eternalcode.discordapp.leveling.experience.ExperienceChangeEvent; import com.eternalcode.discordapp.observer.Observer; -import io.sentry.Sentry; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.entities.channel.Channel; -import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; -import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import panda.utilities.text.Formatter; -import java.util.Optional; import java.util.concurrent.TimeUnit; +import java.util.function.LongSupplier; public class LevelController implements Observer { @@ -84,7 +80,7 @@ public void update(ExperienceChangeEvent event) { try { long channelId = event.channelId(); - MessageChannel channel = this.getChannelById(channelId); + MessageChannel channel = this.getChannelById(() -> channelId); if (channel == null) { return null; @@ -92,18 +88,15 @@ public void update(ExperienceChangeEvent event) { channel.sendMessage(messageContent).queue(message -> message.delete().queueAfter(5, TimeUnit.SECONDS)); } - catch (Exception exception) { - Sentry.captureException(exception); - exception.printStackTrace(); - } + catch (Exception ignored) {} return userLevel; }); } - private MessageChannel getChannelById(long channelId) { - MessageChannel channel = this.jda.getPrivateChannelById(channelId); + private MessageChannel getChannelById(LongSupplier channelId) { + MessageChannel channel = this.jda.getPrivateChannelById(channelId.getAsLong()); - return channel != null ? channel : this.jda.getTextChannelById(channelId); + return channel != null ? channel : this.jda.getTextChannelById(channelId.getAsLong()); } } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java index 8f3ae20b..4b5d4905 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java @@ -3,8 +3,6 @@ import com.eternalcode.discordapp.leveling.experience.ExperienceConfig; import com.eternalcode.discordapp.leveling.experience.ExperienceService; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; -import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -48,13 +46,14 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) { } private void modifyPoints(User event, long userId, double points) { - event.openPrivateChannel().queue(channel -> this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()) - .whenComplete((experience, throwable) -> { + long channel = event.openPrivateChannel().complete().getIdLong(); - if (throwable != null) { - throwable.printStackTrace(); - } - })); + this.experienceService.modifyPoints(userId, points, true, channel) + .whenComplete((experience, throwable) -> { + if (throwable != null) { + throwable.printStackTrace(); + } + }); } } From 9d509c991cdc5fd936c406a0dce5822a24d43a24 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 20:55:19 +0200 Subject: [PATCH 09/11] Fix checkstyle violations. --- .../com/eternalcode/discordapp/leveling/LevelController.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java index 5b7c0b2e..2ce6e04a 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -88,7 +88,9 @@ public void update(ExperienceChangeEvent event) { channel.sendMessage(messageContent).queue(message -> message.delete().queueAfter(5, TimeUnit.SECONDS)); } - catch (Exception ignored) {} + catch (Exception ignored) { + + } return userLevel; }); From cccb2265138c14ca5cc0e584f4a35556652d1d2e Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 21:04:03 +0200 Subject: [PATCH 10/11] Replace long to long suppiler. --- .../eternalcode/discordapp/leveling/LevelController.java | 4 ++-- .../leveling/experience/ExperienceChangeEvent.java | 4 +++- .../discordapp/leveling/experience/ExperienceService.java | 5 +++-- .../experience/listener/ExperienceMessageListener.java | 4 +++- .../experience/listener/ExperienceReactionListener.java | 2 +- .../experience/listener/ExperienceVoiceListener.java | 3 ++- .../discordapp/leveling/games/CodeGameAnswerController.java | 6 ++++-- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java index 2ce6e04a..b6edeb01 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -78,9 +78,9 @@ public void update(ExperienceChangeEvent event) { try { - long channelId = event.channelId(); + LongSupplier channelId = event.channelId(); - MessageChannel channel = this.getChannelById(() -> channelId); + MessageChannel channel = this.getChannelById(channelId); if (channel == null) { return null; diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java index e6b55474..5ccfa9b1 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceChangeEvent.java @@ -1,4 +1,6 @@ package com.eternalcode.discordapp.leveling.experience; -public record ExperienceChangeEvent(Experience experience, long channelId) { +import java.util.function.LongSupplier; + +public record ExperienceChangeEvent(Experience experience, LongSupplier channelId) { } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java index abab68c6..8ed69637 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/ExperienceService.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.function.LongSupplier; public class ExperienceService { @@ -16,7 +17,7 @@ public ExperienceService(DatabaseManager databaseManager, ObserverRegistry obser this.observerRegistry = observerRegistry; } - public CompletableFuture saveExperience(Experience experience, long channelId) { + public CompletableFuture saveExperience(Experience experience, LongSupplier channelId) { return this.experienceRepository.saveExperience(experience).whenComplete((experience1, throwable) -> { if (throwable == null) { this.observerRegistry.publish(new ExperienceChangeEvent(experience1, channelId)); @@ -24,7 +25,7 @@ public CompletableFuture saveExperience(Experience experience, long }); } - public CompletableFuture modifyPoints(long userId, double points, boolean add, long channelId) { + public CompletableFuture modifyPoints(long userId, double points, boolean add, LongSupplier channelId) { return this.experienceRepository.modifyPoints(userId, points, add).whenComplete((experience, throwable) -> { if (throwable == null) { this.observerRegistry.publish(new ExperienceChangeEvent(experience, channelId)); diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java index 9d11c13e..6d7cb8f4 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceMessageListener.java @@ -5,6 +5,8 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import java.util.function.LongSupplier; + public class ExperienceMessageListener extends ListenerAdapter { private final ExperienceConfig experienceConfig; @@ -35,7 +37,7 @@ private void givePoints(MessageReceivedEvent event) { double points = (double) message.length / this.experienceConfig.messageExperience.howManyWords * basePoints; long userId = event.getAuthor().getIdLong(); - long channelId = event.getChannel().getIdLong(); + LongSupplier channelId = () -> event.getChannel().getIdLong(); this.experienceService.modifyPoints(userId, points, true, channelId).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java index 4b5d4905..6169686e 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java @@ -48,7 +48,7 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) { private void modifyPoints(User event, long userId, double points) { long channel = event.openPrivateChannel().complete().getIdLong(); - this.experienceService.modifyPoints(userId, points, true, channel) + this.experienceService.modifyPoints(userId, points, true, () -> channel) .whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java index 0d5f2f51..32aeb64a 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceVoiceListener.java @@ -11,6 +11,7 @@ import java.time.Duration; import java.time.Instant; +import java.util.function.LongSupplier; public class ExperienceVoiceListener extends ListenerAdapter { @@ -52,7 +53,7 @@ private void leaveVoiceChannel(GuildVoiceUpdateEvent event) { long userId = event.getMember().getIdLong(); this.usersVoiceActivityData.usersOnVoiceChannel.remove(event.getMember().getIdLong()); - long voiceLeftChannelId = event.getChannelLeft().getIdLong(); + LongSupplier voiceLeftChannelId = () -> event.getChannelLeft().getIdLong(); this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, voiceLeftChannelId).whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace(); diff --git a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java index 4ac24ccc..b83e87dd 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/games/CodeGameAnswerController.java @@ -11,6 +11,7 @@ import java.time.Duration; import java.time.Instant; import java.util.Random; +import java.util.function.LongSupplier; public class CodeGameAnswerController extends ListenerAdapter { @@ -34,9 +35,9 @@ public void onMessageReceived(MessageReceivedEvent event) { return; } - long channelId = event.getChannel().getIdLong(); + LongSupplier channelId = () -> event.getChannel().getIdLong(); - if (channelId != this.codeGameConfiguration.channelId) { + if (channelId.getAsLong() != this.codeGameConfiguration.channelId) { return; } @@ -68,6 +69,7 @@ public void onMessageReceived(MessageReceivedEvent event) { .setDescription(formatter.format(this.codeGameConfiguration.embedSettings.description)) .setFooter(this.codeGameConfiguration.embedSettings.footer); + this.experienceService.modifyPoints(event.getAuthor().getIdLong(), points, true, channelId) .whenComplete((experience, throwable) -> { if (throwable != null) { From bc9d7dabde7a893728e6578bf2e7eaa8b1d8bba7 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Wed, 25 Oct 2023 21:07:39 +0200 Subject: [PATCH 11/11] Follow rollczi suggestion. --- .../experience/listener/ExperienceReactionListener.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java index 6169686e..3809d18c 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/experience/listener/ExperienceReactionListener.java @@ -7,6 +7,8 @@ import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import java.util.function.LongSupplier; + public class ExperienceReactionListener extends ListenerAdapter { private final ExperienceConfig experienceConfig; @@ -46,9 +48,9 @@ public void onMessageReactionRemove(MessageReactionRemoveEvent event) { } private void modifyPoints(User event, long userId, double points) { - long channel = event.openPrivateChannel().complete().getIdLong(); + LongSupplier channel = () -> event.openPrivateChannel().complete().getIdLong(); - this.experienceService.modifyPoints(userId, points, true, () -> channel) + this.experienceService.modifyPoints(userId, points, true, channel) .whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace();