package cc.fascinated.bat.reminder; import cc.fascinated.bat.common.feature.Feature; import cc.fascinated.bat.common.feature.FeatureProfile; import cc.fascinated.bat.reminder.command.ReminderCommand; import cc.fascinated.bat.common.model.BatGuild; import cc.fascinated.bat.service.OldCommandService; import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.GuildService; import lombok.NonNull; import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.DependsOn; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** * @author Fascinated (fascinated7) */ @Component @Log4j2(topic = "Reminder Feature") @DependsOn("discordService") public class ReminderFeature extends Feature { public static final int MAX_REMINDERS = 5; // 5 reminders public static final long MAX_REMINDER_LENGTH = TimeUnit.DAYS.toMillis(30); // 1 month private final GuildService guildService; @Autowired public ReminderFeature(@NonNull ApplicationContext context, @NonNull OldCommandService commandService, @NonNull GuildService guildService) { super("Reminder", FeatureProfile.FeatureState.DISABLED, true); this.guildService = guildService; super.registerCommand(commandService, context.getBean(ReminderCommand.class)); } @Scheduled(cron = "*/30 * * * * *") public void checkReminders() { for (Guild guild : DiscordService.JDA.getGuilds()) { BatGuild batGuild = guildService.getGuild(guild.getId()); if (batGuild == null) { continue; } ReminderProfile reminderProfile = batGuild.getProfile(ReminderProfile.class); if (reminderProfile == null) { continue; } for (Map.Entry> entry : reminderProfile.getReminders().entrySet()) { User user = entry.getKey(); List toRemove = new ArrayList<>(); List reminders = entry.getValue(); for (Reminder reminder : reminders) { if (!reminder.isExpired()) { continue; } toRemove.add(reminder); TextChannel channel = reminder.getChannel(); if (channel != null) { channel.sendMessage("Hey %s! ⏰ It's time for your reminder: `%s`".formatted( user.getAsMention(), reminder.getReminder() )).queue(); } } toRemove.forEach(reminder -> reminderProfile.removeReminder(user, reminder)); } } } }