Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,20 @@ public List<QOTWAccount> getTopAccounts(int amount, int page) {
* @return The total points after the update.
*/
public long increment(long userId) {
return increment(userId, 1);
}

/**
* Increments a single user's QOTW-Points.
*
* @param userId The ID of the user whose points shall be incremented
* @param incrementCount the number of points to increment
* @return The total points after the update.
*/
public long increment(long userId, int incrementCount) {
try {
LocalDate date=LocalDate.now();
int points = pointsRepository.getPointsAtDate(userId, date)+1;
int points = pointsRepository.getPointsAtDate(userId, date) + incrementCount;
pointsRepository.setPointsAtDate(userId, date, points);
LocalDate month = getCurrentMonth();
long newScore = pointsRepository.getByUserId(userId, month).map(QOTWAccount::getPoints).orElse(0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData;
import net.javadiscord.javabot.systems.qotw.commands.qotw_points.DecrementPointsSubcommand;
import net.javadiscord.javabot.systems.qotw.commands.qotw_points.IncrementPointsSubcommand;
import net.javadiscord.javabot.systems.qotw.commands.questions_queue.AddQuestionSubcommand;
import net.javadiscord.javabot.systems.qotw.commands.questions_queue.ListQuestionsSubcommand;
Expand All @@ -22,17 +23,18 @@ public class QOTWAdminCommand extends SlashCommand {
* @param addQuestionSubcommand /qotw-admin questions-queue add
* @param removeQuestionSubcommand /qotw-admin questions-queue remove
* @param incrementPointsSubcommand /qotw-admin account increment
* @param decrementPointsSubcommand /qotw-admin account decrement
* @param reviewSubcommand /qotw-admin submissions review
*/
public QOTWAdminCommand(ListQuestionsSubcommand listQuestionsSubcommand, AddQuestionSubcommand addQuestionSubcommand, RemoveQuestionSubcommand removeQuestionSubcommand, IncrementPointsSubcommand incrementPointsSubcommand, QOTWReviewSubcommand reviewSubcommand) {
public QOTWAdminCommand(ListQuestionsSubcommand listQuestionsSubcommand, AddQuestionSubcommand addQuestionSubcommand, RemoveQuestionSubcommand removeQuestionSubcommand, IncrementPointsSubcommand incrementPointsSubcommand, DecrementPointsSubcommand decrementPointsSubcommand, QOTWReviewSubcommand reviewSubcommand) {
setCommandData(Commands.slash("qotw-admin", "Administrative tools for managing the Question of the Week.")
.setDefaultPermissions(DefaultMemberPermissions.DISABLED)
.setGuildOnly(true)
);
addSubcommands(reviewSubcommand);
addSubcommandGroups(
SubcommandGroup.of(new SubcommandGroupData("questions-queue", "Commands for interacting with the set of QOTW questions that are in queue."), listQuestionsSubcommand, addQuestionSubcommand, removeQuestionSubcommand),
SubcommandGroup.of(new SubcommandGroupData("account", "Commands for interaction with Users Question of the Week points."), incrementPointsSubcommand),
SubcommandGroup.of(new SubcommandGroupData("account", "Commands for interaction with Users Question of the Week points."), incrementPointsSubcommand, decrementPointsSubcommand),
SubcommandGroup.of(new SubcommandGroupData("submissions", "Commands for managing QOTW Submissions."), reviewSubcommand)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package net.javadiscord.javabot.systems.qotw.commands.qotw_points;

import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.javadiscord.javabot.systems.notification.NotificationService;
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
import net.javadiscord.javabot.util.Responses;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;

/**
* This is an abstract class for QOTW subcommands allowing staff-members to change the QOTW-points of any user.
*/
public abstract class ChangePointsSubcommand extends SlashCommand.Subcommand {

private final QOTWPointsService pointsService;
private final NotificationService notificationService;

/**
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
* @param pointsService The {@link QOTWPointsService}
* @param notificationService The {@link NotificationService}
* @param commandName The name of the command
* @param commandDescription The description of the command
*/
public ChangePointsSubcommand(QOTWPointsService pointsService, NotificationService notificationService, String commandName, String commandDescription) {
this.pointsService = pointsService;
this.notificationService = notificationService;
setCommandData(new SubcommandData(commandName, commandDescription)
.addOption(OptionType.USER, "user", "The user whose points should be changed.", true)
.addOption(OptionType.BOOLEAN, "quiet", "Whether or not the user should be notified", false)
);
}

@Override
public void execute(@NotNull SlashCommandInteractionEvent event) {
OptionMapping userOption = event.getOption("user");
if (userOption == null) {
Responses.replyMissingArguments(event).queue();
return;
}
Member member = userOption.getAsMember();
if (member == null) {
Responses.error(event, "User must be a part of this server.").queue();
return;
}
boolean quiet = event.getOption("quiet", () -> false, OptionMapping::getAsBoolean);
event.deferReply().queue();
long points = changePoints(event);
sendNotifications(event, member, points, quiet);
}

private long changePoints(SlashCommandInteractionEvent event) {
return pointsService.increment(event.getMember().getIdLong(), getIncrementCount(event));
}

protected abstract int getIncrementCount(SlashCommandInteractionEvent event);

private void sendNotifications(SlashCommandInteractionEvent event, Member member, long points, boolean quiet) {
MessageEmbed embed = buildIncrementEmbed(member.getUser(), points);
notificationService.withGuild(event.getGuild()).sendToModerationLog(c -> c.sendMessageEmbeds(embed));
if (!quiet) {
notificationService.withQOTW(event.getGuild(), member.getUser()).sendAccountIncrementedNotification();
}
event.getHook().sendMessageEmbeds(embed).queue();
}

protected @NotNull MessageEmbed buildIncrementEmbed(@NotNull User user, long points) {
return createIncrementEmbedBuilder(user, points)
.build();
}

/**
* Creates an {@link EmbedBuilder} for the notification embed.
* @param user The user whose account is incremented
* @param points The new total number of points of the user
* @return The created {@link EmbedBuilder} for creating the noticiation embed
*/
protected @NotNull EmbedBuilder createIncrementEmbedBuilder(User user, long points) {
return new EmbedBuilder()
.setAuthor(user.getAsTag(), null, user.getEffectiveAvatarUrl())
.setTitle("QOTW Account changed")
.setColor(Responses.Type.SUCCESS.getColor())
.addField("Total QOTW-Points", "```" + points + "```", true)
.addField("Rank", "```#" + pointsService.getQOTWRank(user.getIdLong()) + "```", true)
.setFooter("ID: " + user.getId())
.setTimestamp(Instant.now());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.javadiscord.javabot.systems.qotw.commands.qotw_points;

import org.jetbrains.annotations.NotNull;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.javadiscord.javabot.systems.notification.NotificationService;
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;

/**
* <h3>This class represents the /qotw account increment command.</h3>
* This Subcommand allows staff-members to increment the QOTW-points of any user.
*/
public class DecrementPointsSubcommand extends ChangePointsSubcommand {

public DecrementPointsSubcommand(QOTWPointsService pointsService, NotificationService notificationService) {
super(pointsService, notificationService, "decrement", "Removes one point to the user's QOTW-Account");
}

@Override
protected int getIncrementCount(SlashCommandInteractionEvent event) {
return -1;
}

@Override
protected @NotNull EmbedBuilder createIncrementEmbedBuilder(User user, long points) {
return super.createIncrementEmbedBuilder(user, points)
.setTitle("QOTW Account decremented");
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,32 @@
package net.javadiscord.javabot.systems.qotw.commands.qotw_points;

import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
import org.jetbrains.annotations.NotNull;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.javadiscord.javabot.systems.notification.NotificationService;
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
import net.javadiscord.javabot.util.Responses;
import org.jetbrains.annotations.NotNull;

import java.time.Instant;

/**
* <h3>This class represents the /qotw account increment command.</h3>
* This Subcommand allows staff-members to increment the QOTW-Account of any user.
* This Subcommand allows staff-members to increment the QOTW-points of any user.
*/
public class IncrementPointsSubcommand extends SlashCommand.Subcommand {
public class IncrementPointsSubcommand extends ChangePointsSubcommand {

private final QOTWPointsService pointsService;
private final NotificationService notificationService;

/**
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
* @param pointsService The {@link QOTWPointsService}
* @param notificationService The {@link NotificationService}
*/
public IncrementPointsSubcommand(QOTWPointsService pointsService, NotificationService notificationService) {
this.pointsService = pointsService;
this.notificationService = notificationService;
setCommandData(new SubcommandData("increment", "Adds one point to the user's QOTW-Account")
.addOption(OptionType.USER, "user", "The user whose points should be incremented.", true)
);
super(pointsService, notificationService, "increment", "Adds one point to the user's QOTW-Account");
}

@Override
public void execute(@NotNull SlashCommandInteractionEvent event) {
OptionMapping userOption = event.getOption("user");
if (userOption == null) {
Responses.replyMissingArguments(event).queue();
return;
}
Member member = userOption.getAsMember();
if (member == null) {
Responses.error(event, "User must be a part of this server.").queue();
return;
}
event.deferReply().queue();
long points = pointsService.increment(member.getIdLong());
MessageEmbed embed = buildIncrementEmbed(member.getUser(), points);
notificationService.withGuild(event.getGuild()).sendToModerationLog(c -> c.sendMessageEmbeds(embed));
notificationService.withQOTW(event.getGuild(), member.getUser()).sendAccountIncrementedNotification();
event.getHook().sendMessageEmbeds(embed).queue();
protected int getIncrementCount(SlashCommandInteractionEvent event) {
return 1;
}

private @NotNull MessageEmbed buildIncrementEmbed(@NotNull User user, long points) {
return new EmbedBuilder()
.setAuthor(user.getAsTag(), null, user.getEffectiveAvatarUrl())
.setTitle("QOTW Account Incremented")
.setColor(Responses.Type.SUCCESS.getColor())
.addField("Total QOTW-Points", "```" + points + "```", true)
.addField("Rank", "```#" + pointsService.getQOTWRank(user.getIdLong()) + "```", true)
.setFooter("ID: " + user.getId())
.setTimestamp(Instant.now())
.build();
@Override
protected @NotNull EmbedBuilder createIncrementEmbedBuilder(User user, long points) {
return super.createIncrementEmbedBuilder(user, points)
.setTitle("QOTW Account incremented");
}

}