-
-
Notifications
You must be signed in to change notification settings - Fork 89
Feature: Cake Day #1042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
christolis
wants to merge
15
commits into
Together-Java:develop
Choose a base branch
from
christolis:feature/cake-day
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature: Cake Day #1042
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
206ddf2
feat(cake-day): implement batch insert cake days routine
christolis 767ee85
feat(cake-day): implement actual routine logic
christolis 401f1e6
docs: add JavaDocs
christolis 8192fdf
refactor: move cake day feature to its own directory
christolis 6aee3ba
fix: improve role refresh handling and make it work
christolis dd479c7
refactor: remove unused method
christolis c1f7448
docs: add JavaDocs
christolis 4259568
fix: include anniversary logic for cake day
christolis ee4d908
fix: add Objects#requireNonNull() on cakeDayConfig
christolis c418ba6
feat(config): add proper default value for key
christolis 20f224a
feat(cake-day): improve removeRoleFromMembers() method
christolis dc3382a
feat: add CakeDayConfig record constructor
christolis 2f06eb4
feat: use Optional properly
christolis 72156b8
fix(CakeDayService): consider month and day for cake day role
christolis 47b17b8
refactor: better use of Optional
christolis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
application/src/main/java/org/togetherjava/tjbot/config/CakeDayConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.togetherjava.tjbot.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Configuration record for the Cake Day feature. | ||
*/ | ||
public record CakeDayConfig( | ||
@JsonProperty(value = "rolePattern", required = true) String rolePattern) { | ||
|
||
/** | ||
* Configuration constructor for the Cake Day feature. | ||
*/ | ||
public CakeDayConfig { | ||
Objects.requireNonNull(rolePattern); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
application/src/main/java/org/togetherjava/tjbot/features/cakeday/CakeDayListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.togetherjava.tjbot.features.cakeday; | ||
|
||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.dv8tion.jda.api.entities.Member; | ||
import net.dv8tion.jda.api.entities.User; | ||
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import org.togetherjava.tjbot.db.generated.tables.records.CakeDaysRecord; | ||
import org.togetherjava.tjbot.features.EventReceiver; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* A listener class responsible for handling cake day related events. | ||
*/ | ||
public class CakeDayListener extends ListenerAdapter implements EventReceiver { | ||
|
||
private final CakeDayService cakeDayService; | ||
|
||
/** | ||
* Constructs a new CakeDayListener with the given {@link CakeDayService}. | ||
* | ||
* @param cakeDayService the {@link CakeDayService} to be used by this listener | ||
*/ | ||
public CakeDayListener(CakeDayService cakeDayService) { | ||
this.cakeDayService = cakeDayService; | ||
} | ||
|
||
/** | ||
* Handles the event of a message being received in a guild. | ||
* <p> | ||
* It caches the user's cake day and inserts the member's cake day into the database if not | ||
* already present. | ||
* | ||
* @param event the {@link MessageReceivedEvent} representing the message received | ||
*/ | ||
@Override | ||
public void onMessageReceived(@NotNull MessageReceivedEvent event) { | ||
User author = event.getAuthor(); | ||
Member member = event.getMember(); | ||
long authorId = author.getIdLong(); | ||
long guildId = event.getGuild().getIdLong(); | ||
|
||
if (member == null || author.isBot() || author.isSystem()) { | ||
return; | ||
} | ||
|
||
|
||
if (cakeDayService.hasMemberCakeDayToday(member)) { | ||
cakeDayService.addCakeDayRole(member); | ||
return; | ||
} | ||
|
||
if (cakeDayService.isUserCached(author)) { | ||
return; | ||
} | ||
|
||
cakeDayService.addToCache(author); | ||
Optional<CakeDaysRecord> cakeDaysRecord = | ||
cakeDayService.findUserCakeDayFromDatabase(authorId); | ||
if (cakeDaysRecord.isPresent()) { | ||
return; | ||
} | ||
|
||
cakeDayService.insertMemberCakeDayToDatabase(member, guildId); | ||
} | ||
|
||
/** | ||
* Handles the event of a guild member being removed from the guild. It removes the user's cake | ||
* day information from the database if present. | ||
* | ||
* @param event the {@link GuildMemberRemoveEvent} representing the member removal event | ||
*/ | ||
@Override | ||
public void onGuildMemberRemove(GuildMemberRemoveEvent event) { | ||
User user = event.getUser(); | ||
Guild guild = event.getGuild(); | ||
|
||
cakeDayService.handleUserLeft(user, guild); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
application/src/main/java/org/togetherjava/tjbot/features/cakeday/CakeDayRoutine.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.togetherjava.tjbot.features.cakeday; | ||
|
||
import net.dv8tion.jda.api.JDA; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import org.togetherjava.tjbot.features.Routine; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Represents a routine for managing cake day celebrations. | ||
* <p> | ||
* This routine handles the assignment and removal of a designated cake day role to guild members | ||
* based on their anniversary of joining the guild. | ||
*/ | ||
public class CakeDayRoutine implements Routine { | ||
|
||
private final CakeDayService cakeDayService; | ||
|
||
/** | ||
* Constructs a new {@link CakeDayRoutine} instance. | ||
* | ||
* @param cakeDayService an instance of the cake day service | ||
*/ | ||
public CakeDayRoutine(CakeDayService cakeDayService) { | ||
this.cakeDayService = cakeDayService; | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public Schedule createSchedule() { | ||
return new Schedule(ScheduleMode.FIXED_RATE, 0, 1, TimeUnit.DAYS); | ||
} | ||
|
||
@Override | ||
public void runRoutine(@NotNull JDA jda) { | ||
jda.getGuilds().forEach(cakeDayService::reassignCakeDayRole); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.