Skip to content

Commit d07ffde

Browse files
committed
allow decrementing QOTW points
1 parent f6aca25 commit d07ffde

File tree

5 files changed

+156
-55
lines changed

5 files changed

+156
-55
lines changed

src/main/java/net/javadiscord/javabot/systems/qotw/QOTWPointsService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,20 @@ public List<QOTWAccount> getTopAccounts(int amount, int page) {
125125
* @return The total points after the update.
126126
*/
127127
public long increment(long userId) {
128+
return increment(userId, 1);
129+
}
130+
131+
/**
132+
* Increments a single user's QOTW-Points.
133+
*
134+
* @param userId The ID of the user whose points shall be incremented
135+
* @param incrementCount the number of points to increment
136+
* @return The total points after the update.
137+
*/
138+
public long increment(long userId, int incrementCount) {
128139
try {
129140
LocalDate date=LocalDate.now();
130-
int points = pointsRepository.getPointsAtDate(userId, date)+1;
141+
int points = pointsRepository.getPointsAtDate(userId, date) + incrementCount;
131142
pointsRepository.setPointsAtDate(userId, date, points);
132143
LocalDate month = getCurrentMonth();
133144
long newScore = pointsRepository.getByUserId(userId, month).map(QOTWAccount::getPoints).orElse(0L);

src/main/java/net/javadiscord/javabot/systems/qotw/commands/QOTWAdminCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
44
import net.dv8tion.jda.api.interactions.commands.build.Commands;
55
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
6+
import net.javadiscord.javabot.systems.qotw.commands.qotw_points.DecrementPointsSubcommand;
67
import net.javadiscord.javabot.systems.qotw.commands.qotw_points.IncrementPointsSubcommand;
78
import net.javadiscord.javabot.systems.qotw.commands.questions_queue.AddQuestionSubcommand;
89
import net.javadiscord.javabot.systems.qotw.commands.questions_queue.ListQuestionsSubcommand;
@@ -22,17 +23,18 @@ public class QOTWAdminCommand extends SlashCommand {
2223
* @param addQuestionSubcommand /qotw-admin questions-queue add
2324
* @param removeQuestionSubcommand /qotw-admin questions-queue remove
2425
* @param incrementPointsSubcommand /qotw-admin account increment
26+
* @param decrementPointsSubcommand /qotw-admin account decrement
2527
* @param reviewSubcommand /qotw-admin submissions review
2628
*/
27-
public QOTWAdminCommand(ListQuestionsSubcommand listQuestionsSubcommand, AddQuestionSubcommand addQuestionSubcommand, RemoveQuestionSubcommand removeQuestionSubcommand, IncrementPointsSubcommand incrementPointsSubcommand, QOTWReviewSubcommand reviewSubcommand) {
29+
public QOTWAdminCommand(ListQuestionsSubcommand listQuestionsSubcommand, AddQuestionSubcommand addQuestionSubcommand, RemoveQuestionSubcommand removeQuestionSubcommand, IncrementPointsSubcommand incrementPointsSubcommand, DecrementPointsSubcommand decrementPointsSubcommand, QOTWReviewSubcommand reviewSubcommand) {
2830
setCommandData(Commands.slash("qotw-admin", "Administrative tools for managing the Question of the Week.")
2931
.setDefaultPermissions(DefaultMemberPermissions.DISABLED)
3032
.setGuildOnly(true)
3133
);
3234
addSubcommands(reviewSubcommand);
3335
addSubcommandGroups(
3436
SubcommandGroup.of(new SubcommandGroupData("questions-queue", "Commands for interacting with the set of QOTW questions that are in queue."), listQuestionsSubcommand, addQuestionSubcommand, removeQuestionSubcommand),
35-
SubcommandGroup.of(new SubcommandGroupData("account", "Commands for interaction with Users Question of the Week points."), incrementPointsSubcommand),
37+
SubcommandGroup.of(new SubcommandGroupData("account", "Commands for interaction with Users Question of the Week points."), incrementPointsSubcommand, decrementPointsSubcommand),
3638
SubcommandGroup.of(new SubcommandGroupData("submissions", "Commands for managing QOTW Submissions."), reviewSubcommand)
3739
);
3840
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.javadiscord.javabot.systems.qotw.commands.qotw_points;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import net.dv8tion.jda.api.EmbedBuilder;
6+
import net.dv8tion.jda.api.entities.User;
7+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
8+
import net.javadiscord.javabot.systems.notification.NotificationService;
9+
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
10+
11+
/**
12+
* <h3>This class represents the /qotw account increment command.</h3>
13+
* This Subcommand allows staff-members to increment the QOTW-points of any user.
14+
*/
15+
public class DecrementPointsSubcommand extends ChangePointsSubcommand {
16+
17+
public DecrementPointsSubcommand(QOTWPointsService pointsService, NotificationService notificationService) {
18+
super(pointsService, notificationService, "decrement", "Removes one point to the user's QOTW-Account");
19+
}
20+
21+
@Override
22+
protected int getIncrementCount(SlashCommandInteractionEvent event) {
23+
return -1;
24+
}
25+
26+
@Override
27+
protected @NotNull EmbedBuilder createIncrementEmbedBuilder(User user, long points) {
28+
return super.createIncrementEmbedBuilder(user, points)
29+
.setTitle("QOTW Account decremented");
30+
}
31+
}
Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,32 @@
11
package net.javadiscord.javabot.systems.qotw.commands.qotw_points;
22

3-
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
3+
import org.jetbrains.annotations.NotNull;
4+
45
import net.dv8tion.jda.api.EmbedBuilder;
5-
import net.dv8tion.jda.api.entities.Member;
6-
import net.dv8tion.jda.api.entities.MessageEmbed;
76
import net.dv8tion.jda.api.entities.User;
87
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;
128
import net.javadiscord.javabot.systems.notification.NotificationService;
139
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;
1810

1911
/**
2012
* <h3>This class represents the /qotw account increment command.</h3>
21-
* This Subcommand allows staff-members to increment the QOTW-Account of any user.
13+
* This Subcommand allows staff-members to increment the QOTW-points of any user.
2214
*/
23-
public class IncrementPointsSubcommand extends SlashCommand.Subcommand {
15+
public class IncrementPointsSubcommand extends ChangePointsSubcommand {
2416

25-
private final QOTWPointsService pointsService;
26-
private final NotificationService notificationService;
27-
28-
/**
29-
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
30-
* @param pointsService The {@link QOTWPointsService}
31-
* @param notificationService The {@link NotificationService}
32-
*/
3317
public IncrementPointsSubcommand(QOTWPointsService pointsService, NotificationService notificationService) {
34-
this.pointsService = pointsService;
35-
this.notificationService = notificationService;
36-
setCommandData(new SubcommandData("increment", "Adds one point to the user's QOTW-Account")
37-
.addOption(OptionType.USER, "user", "The user whose points should be incremented.", true)
38-
);
18+
super(pointsService, notificationService, "increment", "Adds one point to the user's QOTW-Account");
3919
}
4020

4121
@Override
42-
public void execute(@NotNull SlashCommandInteractionEvent event) {
43-
OptionMapping userOption = event.getOption("user");
44-
if (userOption == null) {
45-
Responses.replyMissingArguments(event).queue();
46-
return;
47-
}
48-
Member member = userOption.getAsMember();
49-
if (member == null) {
50-
Responses.error(event, "User must be a part of this server.").queue();
51-
return;
52-
}
53-
event.deferReply().queue();
54-
long points = pointsService.increment(member.getIdLong());
55-
MessageEmbed embed = buildIncrementEmbed(member.getUser(), points);
56-
notificationService.withGuild(event.getGuild()).sendToModerationLog(c -> c.sendMessageEmbeds(embed));
57-
notificationService.withQOTW(event.getGuild(), member.getUser()).sendAccountIncrementedNotification();
58-
event.getHook().sendMessageEmbeds(embed).queue();
22+
protected int getIncrementCount(SlashCommandInteractionEvent event) {
23+
return 1;
5924
}
6025

61-
private @NotNull MessageEmbed buildIncrementEmbed(@NotNull User user, long points) {
62-
return new EmbedBuilder()
63-
.setAuthor(user.getAsTag(), null, user.getEffectiveAvatarUrl())
64-
.setTitle("QOTW Account Incremented")
65-
.setColor(Responses.Type.SUCCESS.getColor())
66-
.addField("Total QOTW-Points", "```" + points + "```", true)
67-
.addField("Rank", "```#" + pointsService.getQOTWRank(user.getIdLong()) + "```", true)
68-
.setFooter("ID: " + user.getId())
69-
.setTimestamp(Instant.now())
70-
.build();
26+
@Override
27+
protected @NotNull EmbedBuilder createIncrementEmbedBuilder(User user, long points) {
28+
return super.createIncrementEmbedBuilder(user, points)
29+
.setTitle("QOTW Account incremented");
7130
}
31+
7232
}

0 commit comments

Comments
 (0)