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..b6edeb01 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/LevelController.java @@ -5,9 +5,12 @@ import com.eternalcode.discordapp.observer.Observer; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.User; -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.concurrent.TimeUnit; +import java.util.function.LongSupplier; + 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,12 +76,29 @@ 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 { + LongSupplier channelId = event.channelId(); + + MessageChannel channel = this.getChannelById(channelId); + + if (channel == null) { + return null; + } + + channel.sendMessage(messageContent).queue(message -> message.delete().queueAfter(5, TimeUnit.SECONDS)); + } + catch (Exception ignored) { + } return userLevel; }); } + + private MessageChannel getChannelById(LongSupplier channelId) { + MessageChannel channel = this.jda.getPrivateChannelById(channelId.getAsLong()); + + return channel != null ? channel : this.jda.getTextChannelById(channelId.getAsLong()); + } } 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..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) { +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 2028a354..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,18 +17,18 @@ public ExperienceService(DatabaseManager databaseManager, ObserverRegistry obser this.observerRegistry = observerRegistry; } - public CompletableFuture saveExperience(Experience experience) { + public CompletableFuture saveExperience(Experience experience, LongSupplier 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, LongSupplier 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..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; @@ -21,7 +23,6 @@ public void onMessageReceived(MessageReceivedEvent event) { return; } - this.givePoints(event); } @@ -36,7 +37,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).whenComplete((experience, throwable) -> { + 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 80f0ed9a..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 @@ -2,10 +2,13 @@ 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.events.message.react.MessageReactionAddEvent; 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; @@ -21,11 +24,13 @@ 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) -> { - if (throwable != null) { - throwable.printStackTrace(); - } - }); + User user = event.getUser(); + + if (user.isBot()) { + return; + } + + this.modifyPoints(user, userId, points); } @Override @@ -33,11 +38,24 @@ 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) -> { - if (throwable != null) { - throwable.printStackTrace(); - } - }); + User user = event.getUser(); + + if (user.isBot()) { + return; + } + + this.modifyPoints(user, userId, points); + } + + private void modifyPoints(User event, long userId, double points) { + LongSupplier channel = () -> event.openPrivateChannel().complete().getIdLong(); + + 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 d0d34db8..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 { @@ -51,7 +52,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).whenComplete((experience, throwable) -> { + + 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 fbd28bdf..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,7 +35,9 @@ public void onMessageReceived(MessageReceivedEvent event) { return; } - if (event.getChannel().getIdLong() != this.codeGameConfiguration.channelId) { + LongSupplier channelId = () -> event.getChannel().getIdLong(); + + if (channelId.getAsLong() != this.codeGameConfiguration.channelId) { return; } @@ -66,7 +69,8 @@ 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, channelId) .whenComplete((experience, throwable) -> { if (throwable != null) { throwable.printStackTrace();