|
| 1 | +package net.javadiscord.javabot.systems.qotw.commands.qotw_points; |
| 2 | + |
| 3 | +import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand; |
| 4 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 5 | +import net.dv8tion.jda.api.entities.Member; |
| 6 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 7 | +import net.dv8tion.jda.api.entities.User; |
| 8 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 10 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 11 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
| 12 | +import net.javadiscord.javabot.systems.notification.NotificationService; |
| 13 | +import net.javadiscord.javabot.systems.qotw.QOTWPointsService; |
| 14 | +import net.javadiscord.javabot.util.Responses; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import java.time.Instant; |
| 18 | + |
| 19 | +/** |
| 20 | + * This is an abstract class for QOTW subcommands allowing staff-members to change the QOTW-points of any user. |
| 21 | + */ |
| 22 | +public abstract class ChangePointsSubcommand extends SlashCommand.Subcommand { |
| 23 | + |
| 24 | + private final QOTWPointsService pointsService; |
| 25 | + private final NotificationService notificationService; |
| 26 | + |
| 27 | + /** |
| 28 | + * The constructor of this class, which sets the corresponding {@link SubcommandData}. |
| 29 | + * @param pointsService The {@link QOTWPointsService} |
| 30 | + * @param notificationService The {@link NotificationService} |
| 31 | + * @param commandName The name of the command |
| 32 | + * @param commandDescription The description of the command |
| 33 | + */ |
| 34 | + public ChangePointsSubcommand(QOTWPointsService pointsService, NotificationService notificationService, String commandName, String commandDescription) { |
| 35 | + this.pointsService = pointsService; |
| 36 | + this.notificationService = notificationService; |
| 37 | + setCommandData(new SubcommandData(commandName, commandDescription) |
| 38 | + .addOption(OptionType.USER, "user", "The user whose points should be changed.", true) |
| 39 | + .addOption(OptionType.BOOLEAN, "quiet", "Whether or not the user should be notified", false) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void execute(@NotNull SlashCommandInteractionEvent event) { |
| 45 | + OptionMapping userOption = event.getOption("user"); |
| 46 | + if (userOption == null) { |
| 47 | + Responses.replyMissingArguments(event).queue(); |
| 48 | + return; |
| 49 | + } |
| 50 | + Member member = userOption.getAsMember(); |
| 51 | + if (member == null) { |
| 52 | + Responses.error(event, "User must be a part of this server.").queue(); |
| 53 | + return; |
| 54 | + } |
| 55 | + boolean quiet = event.getOption("quiet", () -> false, OptionMapping::getAsBoolean); |
| 56 | + event.deferReply().queue(); |
| 57 | + long points = changePoints(event); |
| 58 | + sendNotifications(event, member, points, quiet); |
| 59 | + } |
| 60 | + |
| 61 | + private long changePoints(SlashCommandInteractionEvent event) { |
| 62 | + return pointsService.increment(event.getMember().getIdLong(), getIncrementCount(event)); |
| 63 | + } |
| 64 | + |
| 65 | + protected abstract int getIncrementCount(SlashCommandInteractionEvent event); |
| 66 | + |
| 67 | + private void sendNotifications(SlashCommandInteractionEvent event, Member member, long points, boolean quiet) { |
| 68 | + MessageEmbed embed = buildIncrementEmbed(member.getUser(), points); |
| 69 | + notificationService.withGuild(event.getGuild()).sendToModerationLog(c -> c.sendMessageEmbeds(embed)); |
| 70 | + if (!quiet) { |
| 71 | + notificationService.withQOTW(event.getGuild(), member.getUser()).sendAccountIncrementedNotification(); |
| 72 | + } |
| 73 | + event.getHook().sendMessageEmbeds(embed).queue(); |
| 74 | + } |
| 75 | + |
| 76 | + protected @NotNull MessageEmbed buildIncrementEmbed(@NotNull User user, long points) { |
| 77 | + return createIncrementEmbedBuilder(user, points) |
| 78 | + .build(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates an {@link EmbedBuilder} for the notification embed. |
| 83 | + * @param user The user whose account is incremented |
| 84 | + * @param points The new total number of points of the user |
| 85 | + * @return The created {@link EmbedBuilder} for creating the noticiation embed |
| 86 | + */ |
| 87 | + protected @NotNull EmbedBuilder createIncrementEmbedBuilder(User user, long points) { |
| 88 | + return new EmbedBuilder() |
| 89 | + .setAuthor(user.getAsTag(), null, user.getEffectiveAvatarUrl()) |
| 90 | + .setTitle("QOTW Account changed") |
| 91 | + .setColor(Responses.Type.SUCCESS.getColor()) |
| 92 | + .addField("Total QOTW-Points", "```" + points + "```", true) |
| 93 | + .addField("Rank", "```#" + pointsService.getQOTWRank(user.getIdLong()) + "```", true) |
| 94 | + .setFooter("ID: " + user.getId()) |
| 95 | + .setTimestamp(Instant.now()); |
| 96 | + } |
| 97 | +} |
0 commit comments