Skip to content

Commit b1139fa

Browse files
committed
automatically assign QOTW-Champion role
1 parent 87a97d4 commit b1139fa

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

src/main/java/net/javadiscord/javabot/data/config/guild/QOTWConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class QOTWConfig extends GuildConfigItem {
2222
private long submissionsForumChannelId;
2323
private long questionRoleId;
2424
private long qotwReviewRoleId;
25+
private long qotwChampionRoleId;
2526
private String submissionForumOngoingReviewTagName = "";
2627

2728
public NewsChannel getQuestionChannel() {
@@ -44,6 +45,10 @@ public Role getQOTWReviewRole() {
4445
return this.getGuild().getRoleById(this.qotwReviewRoleId);
4546
}
4647

48+
public Role getQOTWChampionRole() {
49+
return this.getGuild().getRoleById(this.qotwChampionRoleId);
50+
}
51+
4752
/**
4853
* Gets a {@link ForumTag} based on the specified name.
4954
*

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import lombok.RequiredArgsConstructor;
44
import net.dv8tion.jda.api.entities.Guild;
55
import net.dv8tion.jda.api.entities.Member;
6+
import net.dv8tion.jda.api.entities.Role;
7+
import net.javadiscord.javabot.data.config.BotConfig;
68
import net.javadiscord.javabot.systems.qotw.dao.QuestionPointsRepository;
79
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
810
import net.javadiscord.javabot.util.ExceptionLogger;
@@ -24,6 +26,7 @@
2426
@Service
2527
public class QOTWPointsService {
2628
private final QuestionPointsRepository pointsRepository;
29+
private final BotConfig botConfig;
2730

2831
/**
2932
* Creates a new QOTW Account if none exists.
@@ -121,15 +124,34 @@ public List<QOTWAccount> getTopAccounts(int amount, int page) {
121124
/**
122125
* Increments a single user's QOTW-Points.
123126
*
124-
* @param userId The discord Id of the user.
127+
* @param member The {@link Member} whose points shall be incremented
125128
* @return The total points after the update.
126129
*/
127-
public long increment(long userId) {
130+
public long increment(Member member) {
131+
long userId = member.getIdLong();
128132
try {
129133
LocalDate date=LocalDate.now();
130134
int points = pointsRepository.getPointsAtDate(userId, date)+1;
131135
pointsRepository.setPointsAtDate(userId, date, points);
132-
return pointsRepository.getByUserId(userId, getCurrentMonth()).map(QOTWAccount::getPoints).orElse(0L);
136+
Role qotwChampionRole = botConfig.get(member.getGuild()).getQotwConfig().getQOTWChampionRole();
137+
LocalDate month = getCurrentMonth();
138+
long newScore = pointsRepository.getByUserId(userId, month).map(QOTWAccount::getPoints).orElse(0L);
139+
if (qotwChampionRole != null) {
140+
pointsRepository.getTopAccounts(month, 0, 1)
141+
.stream()
142+
.findFirst()
143+
.ifPresent(best -> {
144+
if (newScore >= best.getPoints()) {
145+
member.getGuild().addRoleToMember(member, qotwChampionRole).queue();
146+
for (Member m : member.getGuild().getMembersWithRoles(qotwChampionRole)) {
147+
if (getOrCreateAccount(m.getIdLong()).getPoints() < best.getPoints()) {
148+
m.getGuild().removeRoleFromMember(m, qotwChampionRole).queue();
149+
}
150+
}
151+
}
152+
});
153+
}
154+
return newScore;
133155
} catch (DataAccessException e) {
134156
ExceptionLogger.capture(e, getClass().getSimpleName());
135157
return 0;

src/main/java/net/javadiscord/javabot/systems/qotw/commands/qotw_points/IncrementPointsSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
5151
return;
5252
}
5353
event.deferReply().queue();
54-
long points = pointsService.increment(member.getIdLong());
54+
long points = pointsService.increment(member);
5555
MessageEmbed embed = buildIncrementEmbed(member.getUser(), points);
5656
notificationService.withGuild(event.getGuild()).sendToModerationLog(c -> c.sendMessageEmbeds(embed));
5757
notificationService.withQOTW(event.getGuild(), member.getUser()).sendAccountIncrementedNotification();

src/main/java/net/javadiscord/javabot/systems/qotw/dao/QuestionPointsRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public List<QOTWAccount> sortByPoints(LocalDate startDate) throws DataAccessExce
9696
* @throws DataAccessException If an error occurs.
9797
*/
9898
public List<QOTWAccount> getTopAccounts(LocalDate startDate, int page, int size) throws DataAccessException {
99-
return jdbcTemplate.query("SELECT user_id, SUM(points) FROM qotw_points WHERE startDate >= ? AND points > 0 GROUP BY user_id ORDER BY SUM(points) DESC LIMIT ? OFFSET ?", (rs,row)->this.read(rs),
99+
return jdbcTemplate.query("SELECT user_id, SUM(points) FROM qotw_points WHERE obtained_at >= ? AND points > 0 GROUP BY user_id ORDER BY SUM(points) DESC LIMIT ? OFFSET ?", (rs,row)->this.read(rs),
100100
startDate, size, Math.max(0, (page * size) - size));
101101
}
102102

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/SubmissionManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ private boolean canCreateSubmissions(Member member) {
132132
*/
133133
public void acceptSubmission(InteractionHook hook, @NotNull ThreadChannel thread, @NotNull User author, boolean bestAnswer) {
134134
thread.getManager().setName(SUBMISSION_ACCEPTED + thread.getName().substring(1)).queue();
135-
pointsService.increment(author.getIdLong());
135+
pointsService.increment(thread.getGuild().getMember(author));
136136
notificationService.withQOTW(thread.getGuild(), author).sendAccountIncrementedNotification();
137137
if (bestAnswer) {
138-
pointsService.increment(author.getIdLong());
138+
pointsService.increment(thread.getGuild().getMember(author));
139139
notificationService.withQOTW(thread.getGuild(), author).sendBestAnswerNotification();
140140
}
141141
Responses.success(hook, "Submission Accepted",

0 commit comments

Comments
 (0)