Skip to content

Commit

Permalink
Add option to send level-up message at the same channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Oct 25, 2023
1 parent afbebbd commit 4301463
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExperienceChangeEvent> {
Expand Down Expand Up @@ -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<MessageChannel> 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;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
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, 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();
}
});
});
}

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, 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();
}
});
});
}

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, 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();
}
Expand Down

0 comments on commit 4301463

Please sign in to comment.