-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
base: develop
Are you sure you want to change the base?
Feature: Cake Day #1042
Changes from all commits
4e6fa50
affca19
e709cbf
35806ee
d9afbb2
7a7cb94
caddb0c
56e5e1d
a186427
2c67ed5
fe711e7
ab83944
a7f45b6
e12f8d9
8b38313
42ffceb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| } | ||
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment seems pretty useless. |
||
| 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) { | ||
tj-wazei marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should update our table on user join/leave events instead of onMessage, like i mentioned earlier onMessage only triggers for active users which means after few weeks. It's being triggered for 1000 messages from same users without adding any value(updating our table with joined dates). User join event will add new entry to our table, user leaves will remove that entry. |
||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use |
||
| } | ||
|
|
||
| /** | ||
| * 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); | ||
| } | ||
| } | ||
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment seems pretty useless. |
||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the routine should start a seperate thread that will collect two different list of users
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment really needed? It would maybe make sense if it links to the corresponding classes. Though even that should be done as some
@seein the records comment instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible that we require javadoc on this one (public class). But yeah, we can add some details or a
@see.