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 @@ -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<ExperienceChangeEvent> {

private final LevelConfig levelConfig;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.LongSupplier;

public class ExperienceService {

Expand All @@ -16,18 +17,18 @@ public ExperienceService(DatabaseManager databaseManager, ObserverRegistry obser
this.observerRegistry = observerRegistry;
}

public CompletableFuture<Experience> saveExperience(Experience experience) {
public CompletableFuture<Experience> 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<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, 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));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +23,6 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}


this.givePoints(event);
}

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,23 +24,38 @@ 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
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();
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.time.Duration;
import java.time.Instant;
import java.util.function.LongSupplier;

public class ExperienceVoiceListener extends ListenerAdapter {

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down