-
-
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
2e4b90a
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,21 @@ | ||
| package org.togetherjava.tjbot.config; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Configuration record for the Cake Day feature. | ||
| * | ||
| * @see org.togetherjava.tjbot.features.cakeday.CakeDayService | ||
| */ | ||
| 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,79 @@ | ||
| 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; | ||
|
|
||
| public CakeDayListener(CakeDayService cakeDayService) { | ||
christolis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. 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. I would like to work on this |
||
| 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
Member
Author
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. I don't think we can do it in a single statement. The only alternative to Let me know if you were thinking of something else other than |
||
| } | ||
|
|
||
| /** | ||
| * 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.removeUserCakeDay(user, guild); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 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; | ||
|
|
||
| public CakeDayRoutine(CakeDayService cakeDayService) { | ||
christolis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. I would like to work on this. 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. By 2 threads, do you mean 2 different functions or literal java threads?
Member
Author
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.
I think he means literally a new Java thread. |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.