Skip to content

Commit 0502f83

Browse files
committed
fix(CakeDayService): consider month and day for cake day role
This commit aims to fix a bug where the CakeDayService#addTodayMembersCakeDayRole() method would add the cake day role to all members who have been at least one year into the server, disregarding the month and date in which they joined. The documentation has also been made more clean and concise, while the CakeDayService#addCakeDayRole() which required a UserSnowflake as one of its inputs has been removed and now the other version of this function is used which only requires a Member instance. Passing the Guild would be unnecessary as it could be easily acquired from the Member instance, and additionally it helps make sure that the right Member and Guild are used to call this method. Finally, this commit adds an extra condition in the select-from query found in CakeDayService#findCakeDaysTodayFromDatabase() which makes sure that we get all the cake days for the right guild, instead of getting them all unconditionally.
1 parent 8ae61d2 commit 0502f83

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

application/src/main/java/org/togetherjava/tjbot/features/cakeday/CakeDayService.java

+18-26
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,30 @@ private void refreshMembersCakeDayRoles(Role cakeDayRole, Guild guild) {
9898
}
9999

100100
/**
101-
* Asynchronously adds the specified cake day role to guild members who are celebrating their
102-
* cake day today.
101+
* Assigns a special role to members whose cake day (anniversary of joining) is today, but only
102+
* if they have been a member for at least one year.
103103
* <p>
104-
* The cake day role is added to members who have been in the guild for at least one year.
104+
* This method checks the current date against the cake day records in the database for each
105+
* member of the given guild. If the member's cake day is today, and they have been a member for
106+
* at least one year, the method assigns them a special role.
105107
*
106-
* @param guild the guild in which to add the cake day role to members
108+
* @param guild the guild to check for members celebrating their cake day today
107109
*/
108110
private void addTodayMembersCakeDayRole(Guild guild) {
109-
findCakeDaysTodayFromDatabase().forEach(cakeDayRecord -> {
110-
UserSnowflake userSnowflake = UserSnowflake.fromId(cakeDayRecord.getUserId());
111+
findCakeDaysTodayFromDatabase(guild).forEach(cakeDayRecord -> {
112+
Member member = guild.getMemberById(cakeDayRecord.getUserId());
111113

112-
int anniversary = OffsetDateTime.now().getYear() - cakeDayRecord.getJoinedYear();
113-
if (anniversary > 0) {
114-
addCakeDayRole(userSnowflake, guild);
114+
if (member == null) {
115+
return;
115116
}
116-
});
117-
}
118-
119-
120-
/**
121-
* Adds the cake day role to the specified user in the given guild, if available.
122-
*
123-
* @param snowflake the snowflake ID of the user to whom the cake day role will be added
124-
* @param guild the guild in which the cake day role will be added to the user
125-
*/
126-
protected void addCakeDayRole(UserSnowflake snowflake, Guild guild) {
127-
Role cakeDayRole = getCakeDayRole(guild).orElse(null);
128117

129-
if (cakeDayRole == null) {
130-
return;
131-
}
118+
boolean isAnniversaryDay = hasMemberCakeDayToday(member);
119+
int yearsSinceJoin = OffsetDateTime.now().getYear() - cakeDayRecord.getJoinedYear();
132120

133-
guild.addRoleToMember(snowflake, cakeDayRole).complete();
121+
if (yearsSinceJoin > 0 && isAnniversaryDay) {
122+
addCakeDayRole(member);
123+
}
124+
});
134125
}
135126

136127
/**
@@ -254,12 +245,13 @@ protected void handleUserLeft(User user, Guild guild) {
254245
*
255246
* @return a list of {@link CakeDaysRecord} objects representing cake days for today
256247
*/
257-
private List<CakeDaysRecord> findCakeDaysTodayFromDatabase() {
248+
private List<CakeDaysRecord> findCakeDaysTodayFromDatabase(Guild guild) {
258249
String todayMonthDay = OffsetDateTime.now().format(MONTH_DAY_FORMATTER);
259250

260251
return database
261252
.read(context -> context.selectFrom(CAKE_DAYS)
262253
.where(CAKE_DAYS.JOINED_MONTH_DAY.eq(todayMonthDay))
254+
.and(CAKE_DAYS.GUILD_ID.eq(guild.getIdLong()))
263255
.fetch())
264256
.collect(Collectors.toList());
265257
}

0 commit comments

Comments
 (0)