Skip to content

Commit

Permalink
feat(command): Added level up message toggle (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb committed Apr 26, 2022
1 parent e7b2394 commit 3ac06e5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import main.java.de.voidtech.gerald.service.ServerService;
import main.java.de.voidtech.gerald.util.ParsingUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
Expand Down Expand Up @@ -57,6 +58,9 @@ public void executeInternal(CommandContext context, List<String> args) {
case "lb":
showServerLeaderboard(context, server);
break;
case "togglemsg":
toggleLevelUpMessages(context, server);
break;
default:
context.reply("**That's not a valid subcommand!**\n" + this.getUsage());
break;
Expand All @@ -65,6 +69,15 @@ public void executeInternal(CommandContext context, List<String> args) {
}
}

private void toggleLevelUpMessages(CommandContext context, Server server) {
boolean userCanToggle = context.getMember().hasPermission(Permission.MANAGE_SERVER);
if (!userCanToggle) context.reply("**You need the Manage Server permission to toggle level up messages!**");
else {
boolean enabled = xpService.toggleLevelUpMessages(server.getId());
context.reply("**Level up messages are now " + (enabled ? "enabled**" : "disabled**"));
}
}

private void showServerLeaderboard(CommandContext context, Server server) {
List<Experience> topTenMembers = xpService.getServerLeaderboardChunk(server.getId(), 5, 0);
int userPosition = xpService.getUserLeaderboardPosition(server.getId(), context.getAuthor().getId());
Expand Down Expand Up @@ -141,31 +154,31 @@ private String numberToEmoji(int number) {

private String convertSingleDigitToEmoji(String digit) {
switch (digit) {
case "0":
return ":zero:";
case "1":
return ":one:";
case "2":
return ":two:";
case "3":
return ":three:";
case "4":
return ":four:";
case "5":
return ":five:";
case "6":
return ":six:";
case "7":
return ":seven:";
case "8":
return ":eight:";
case "9":
return ":nine:";
case "10":
return ":ten:";
default:
return ":zero:";
}
case "0":
return ":zero:";
case "1":
return ":one:";
case "2":
return ":two:";
case "3":
return ":three:";
case "4":
return ":four:";
case "5":
return ":five:";
case "6":
return ":six:";
case "7":
return ":seven:";
case "8":
return ":eight:";
case "9":
return ":nine:";
case "10":
return ":ten:";
default:
return ":zero:";
}
}

private void removeLevelUpRole(CommandContext context, List<String> args, Server server) {
Expand Down Expand Up @@ -239,7 +252,8 @@ public String getDescription() {
+ "You can gain up to 15 experience per minute.\n"
+ "Server admins can configure roles that are given to you when you reach a certain level.\n"
+ "To stop people from checking their XP, you can disable the XP command.\n"
+ "If you want to stop people from gaining XP, disable the r-xp routine.";
+ "If you want to stop people from gaining XP, disable the r-xp routine.\n"
+ "To disable the level up messages, use the togglemsg subcommand.";
}

@Override
Expand All @@ -248,6 +262,7 @@ public String getUsage() {
+ "xp levels\n"
+ "xp addrole [level] [role]\n"
+ "xp removerole [level]\n"
+ "xp togglemsg\n"
+ "xp leaderboard";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,12 @@ private void sendLevelUpMessage(LevelUpRole role, Member member, Role roleToBeGi
.build();
member.getGuild().getTextChannelById(channelID).sendMessageEmbeds(levelUpEmbed).queue();
}

public boolean toggleLevelUpMessages(long id) {
ServerExperienceConfig config = getServerExperienceConfig(id);
boolean nowEnabled = !config.levelUpMessagesEnabled();
config.setLevelUpMessagesEnabled(nowEnabled);
saveServerExperienceConfig(config);
return nowEnabled;
}
}

0 comments on commit 3ac06e5

Please sign in to comment.