Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-129 Send level-up on same channel and remove after 5 seconds. #134

Merged
merged 11 commits into from
Oct 25, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
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;

public class LevelController implements Observer<ExperienceChangeEvent> {

private final LevelConfig levelConfig;
Expand Down Expand Up @@ -39,8 +46,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();
Expand Down Expand Up @@ -73,9 +80,21 @@ 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 {
Optional<MessageChannel> textChannelOptional = Optional.ofNullable(this.jda.getTextChannelById(event.channelId()));

MessageChannel channel = textChannelOptional.orElse(this.jda.getPrivateChannelById(event.channelId()));
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.discordapp.leveling.experience;

public record ExperienceChangeEvent(Experience experience) {
public record ExperienceChangeEvent(Experience experience, long channelId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ public ExperienceService(DatabaseManager databaseManager, ObserverRegistry obser
this.observerRegistry = observerRegistry;
}

public CompletableFuture<Experience> saveExperience(Experience experience) {
public CompletableFuture<Experience> 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<Experience> modifyPoints(long id, double points, boolean add) {
return this.experienceRepository.modifyPoints(id, points, add).whenComplete((experience, throwable) -> {
public CompletableFuture<Experience> 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));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}


this.givePoints(event);
}

Expand All @@ -36,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).whenComplete((experience, throwable) -> {
long idLong = event.getChannel().getIdLong();
this.experienceService.modifyPoints(userId, points, true, idLong).whenComplete((experience, throwable) -> {
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
if (throwable != null) {
throwable.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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).whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
}
if (event.getUser().isBot()) {
return;
}

event.getUser().openPrivateChannel().queue(channel -> {
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()).whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
}
});
});
}

Expand All @@ -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).whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
}
if (event.getUser().isBot()) {
return;
}

event.getUser().openPrivateChannel().queue(channel -> {
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
this.experienceService.modifyPoints(userId, points, true, channel.getIdLong()).whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).whenComplete((experience, throwable) -> {

long idLong = event.getChannelLeft().getIdLong();
this.experienceService.modifyPoints(userId, this.calculatePoints(event), true, idLong).whenComplete((experience, throwable) -> {
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
if (throwable != null) {
throwable.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
.whenComplete((experience, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
Expand Down