Skip to content

Commit be750c5

Browse files
committed
change QOTW Champion role monthly
1 parent b1139fa commit be750c5

File tree

8 files changed

+84
-30
lines changed

8 files changed

+84
-30
lines changed

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
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;
86
import net.javadiscord.javabot.systems.qotw.dao.QuestionPointsRepository;
97
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
108
import net.javadiscord.javabot.util.ExceptionLogger;
@@ -26,7 +24,6 @@
2624
@Service
2725
public class QOTWPointsService {
2826
private final QuestionPointsRepository pointsRepository;
29-
private final BotConfig botConfig;
3027

3128
/**
3229
* Creates a new QOTW Account if none exists.
@@ -124,33 +121,16 @@ public List<QOTWAccount> getTopAccounts(int amount, int page) {
124121
/**
125122
* Increments a single user's QOTW-Points.
126123
*
127-
* @param member The {@link Member} whose points shall be incremented
124+
* @param userId The ID of the user whose points shall be incremented
128125
* @return The total points after the update.
129126
*/
130-
public long increment(Member member) {
131-
long userId = member.getIdLong();
127+
public long increment(long userId) {
132128
try {
133129
LocalDate date=LocalDate.now();
134130
int points = pointsRepository.getPointsAtDate(userId, date)+1;
135131
pointsRepository.setPointsAtDate(userId, date, points);
136-
Role qotwChampionRole = botConfig.get(member.getGuild()).getQotwConfig().getQOTWChampionRole();
137132
LocalDate month = getCurrentMonth();
138133
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-
}
154134
return newScore;
155135
} catch (DataAccessException e) {
156136
ExceptionLogger.capture(e, getClass().getSimpleName());

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);
54+
long points = pointsService.increment(member.getIdLong());
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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public boolean setPointsAtDate(long userId, LocalDate date, long points) {
8383
* @throws DataAccessException If an error occurs.
8484
*/
8585
public List<QOTWAccount> sortByPoints(LocalDate startDate) throws DataAccessException {
86-
return jdbcTemplate.query("SELECT user_id, SUM(points) FROM qotw_points WHERE obtained_at >= ? GROUP BY user_id ORDER BY SUM(points) DESC", (rs, row)->this.read(rs), startDate);
86+
return jdbcTemplate.query("SELECT user_id, SUM(points) FROM qotw_points WHERE obtained_at >= ? GROUP BY user_id ORDER BY SUM(points) DESC",
87+
(rs, row)->this.read(rs),
88+
startDate);
8789
}
8890

8991
/**
@@ -96,10 +98,23 @@ public List<QOTWAccount> sortByPoints(LocalDate startDate) throws DataAccessExce
9698
* @throws DataAccessException If an error occurs.
9799
*/
98100
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 obtained_at >= ? AND points > 0 GROUP BY user_id ORDER BY SUM(points) DESC LIMIT ? OFFSET ?", (rs,row)->this.read(rs),
101+
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 ?",
102+
(rs,row)->this.read(rs),
100103
startDate, size, Math.max(0, (page * size) - size));
101104
}
102105

106+
/**
107+
* Gets all users with a specific score starting from a specific date.
108+
* @param startDate the minimum date points are considered
109+
* @param score the score users should have in order to be considered
110+
* @return A {@link List} of all users with a specific total QOTW score
111+
*/
112+
public List<Long> getUsersWithSpecificScore(LocalDate startDate, long score){
113+
return jdbcTemplate.query("SELECT user_id FROM qotw_points WHERE obtained_at >= ? GROUP BY user_id HAVING SUM(points)=?",
114+
(rs,row)->rs.getLong(1),
115+
startDate, score);
116+
}
117+
103118
/**
104119
* Reads a {@link ResultSet} and returns a new {@link QOTWAccount} object.
105120
*
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package net.javadiscord.javabot.systems.qotw.jobs;
2+
3+
import java.time.LocalDate;
4+
5+
import org.springframework.scheduling.annotation.Scheduled;
6+
import org.springframework.stereotype.Service;
7+
8+
import lombok.RequiredArgsConstructor;
9+
import net.dv8tion.jda.api.JDA;
10+
import net.dv8tion.jda.api.entities.Guild;
11+
import net.dv8tion.jda.api.entities.Member;
12+
import net.dv8tion.jda.api.entities.Role;
13+
import net.javadiscord.javabot.data.config.BotConfig;
14+
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
15+
import net.javadiscord.javabot.systems.qotw.dao.QuestionPointsRepository;
16+
17+
/**
18+
* Jobs which gives the QOTW Champion role to the users with the highest monthly QOTW score.
19+
*/
20+
@RequiredArgsConstructor
21+
@Service
22+
public class QOTWChampionJob {
23+
24+
private final BotConfig botConfig;
25+
private final QOTWPointsService pointsService;
26+
private final QuestionPointsRepository pointsRepository;
27+
private final JDA jda;
28+
29+
/**
30+
* Gives the QOTW Champion role to the users with the highest monthly QOTW score.
31+
*/
32+
@Scheduled(cron = "0 0 0 1 * *") //start of month
33+
public void execute() {
34+
for (Guild guild : jda.getGuilds()) {
35+
LocalDate month=LocalDate.now().minusMonths(1);
36+
Role qotwChampionRole = botConfig.get(guild).getQotwConfig().getQOTWChampionRole();
37+
if (qotwChampionRole != null) {
38+
pointsRepository.getTopAccounts(month, 0, 1)
39+
.stream()
40+
.findFirst()
41+
.ifPresent(best -> {
42+
for (Member member : guild.getMembersWithRoles(qotwChampionRole)) {
43+
if (pointsService.getOrCreateAccount(member.getIdLong()).getPoints() < best.getPoints()) {
44+
member.getGuild().removeRoleFromMember(member, qotwChampionRole).queue();
45+
}
46+
}
47+
for (Long userId : pointsRepository.getUsersWithSpecificScore(month, best.getPoints())) {
48+
Member member = guild.getMemberById(userId);
49+
if(member != null) {
50+
guild.addRoleToMember(member, qotwChampionRole).queue();
51+
}
52+
}
53+
});
54+
}
55+
}
56+
}
57+
58+
}

src/main/java/net/javadiscord/javabot/systems/qotw/QOTWCloseSubmissionsJob.java renamed to src/main/java/net/javadiscord/javabot/systems/qotw/jobs/QOTWCloseSubmissionsJob.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.javadiscord.javabot.systems.qotw;
1+
package net.javadiscord.javabot.systems.qotw.jobs;
22

33
import lombok.RequiredArgsConstructor;
44
import net.dv8tion.jda.api.EmbedBuilder;

src/main/java/net/javadiscord/javabot/systems/qotw/QOTWReminderJob.java renamed to src/main/java/net/javadiscord/javabot/systems/qotw/jobs/QOTWReminderJob.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.javadiscord.javabot.systems.qotw;
1+
package net.javadiscord.javabot.systems.qotw.jobs;
22

33
import net.dv8tion.jda.api.JDA;
44
import net.dv8tion.jda.api.entities.Guild;

src/main/java/net/javadiscord/javabot/systems/qotw/QOTWUserReminderJob.java renamed to src/main/java/net/javadiscord/javabot/systems/qotw/jobs/QOTWUserReminderJob.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package net.javadiscord.javabot.systems.qotw;
1+
package net.javadiscord.javabot.systems.qotw.jobs;
22

33
import net.dv8tion.jda.api.JDA;
44
import net.dv8tion.jda.api.entities.Guild;
55
import net.javadiscord.javabot.data.config.BotConfig;
66
import net.javadiscord.javabot.data.config.guild.QOTWConfig;
77
import net.javadiscord.javabot.systems.notification.NotificationService;
8+
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
89
import net.javadiscord.javabot.systems.qotw.dao.QuestionQueueRepository;
910
import net.javadiscord.javabot.systems.qotw.model.QOTWSubmission;
1011
import net.javadiscord.javabot.systems.qotw.submissions.SubmissionManager;

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(thread.getGuild().getMember(author));
135+
pointsService.increment(author.getIdLong());
136136
notificationService.withQOTW(thread.getGuild(), author).sendAccountIncrementedNotification();
137137
if (bestAnswer) {
138-
pointsService.increment(thread.getGuild().getMember(author));
138+
pointsService.increment(author.getIdLong());
139139
notificationService.withQOTW(thread.getGuild(), author).sendBestAnswerNotification();
140140
}
141141
Responses.success(hook, "Submission Accepted",

0 commit comments

Comments
 (0)