Skip to content

Commit

Permalink
fix(CakeDayService): consider month and day for cake day role
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
christolis committed Mar 6, 2024
1 parent cf5efde commit c8d92cb
Showing 1 changed file with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,30 @@ private void refreshMembersCakeDayRoles(Role cakeDayRole, Guild guild) {
}

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

int anniversary = OffsetDateTime.now().getYear() - cakeDayRecord.getJoinedYear();
if (anniversary > 0) {
addCakeDayRole(userSnowflake, guild);
if (member == null) {
return;
}
});
}


/**
* Adds the cake day role to the specified user in the given guild, if available.
*
* @param snowflake the snowflake ID of the user to whom the cake day role will be added
* @param guild the guild in which the cake day role will be added to the user
*/
protected void addCakeDayRole(UserSnowflake snowflake, Guild guild) {
Role cakeDayRole = getCakeDayRole(guild).orElse(null);

if (cakeDayRole == null) {
return;
}
boolean isAnniversaryDay = hasMemberCakeDayToday(member);
int yearsSinceJoin = OffsetDateTime.now().getYear() - cakeDayRecord.getJoinedYear();

guild.addRoleToMember(snowflake, cakeDayRole).complete();
if (yearsSinceJoin > 0 && isAnniversaryDay) {
addCakeDayRole(member);
}
});
}

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

return database
.read(context -> context.selectFrom(CAKE_DAYS)
.where(CAKE_DAYS.JOINED_MONTH_DAY.eq(todayMonthDay))
.and(CAKE_DAYS.GUILD_ID.eq(guild.getIdLong()))
.fetch())
.collect(Collectors.toList());
}
Expand Down

0 comments on commit c8d92cb

Please sign in to comment.