-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
206ddf2
767ee85
401f1e6
8192fdf
6aee3ba
dd479c7
c1f7448
4259568
ee4d908
c418ba6
20f224a
dc3382a
2f06eb4
72156b8
47b17b8
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) { | ||
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
|
||
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); | ||
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) { | ||
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); | ||
} | ||
} |
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
@see
in 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
.