Files
Bat/src/main/java/cc/fascinated/bat/features/reminder/ReminderProfile.java
2024-07-03 00:10:02 +01:00

144 lines
4.3 KiB
Java

package cc.fascinated.bat.features.reminder;
import cc.fascinated.bat.common.Serializable;
import cc.fascinated.bat.service.DiscordService;
import com.google.gson.Gson;
import lombok.Getter;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.bson.Document;
import java.util.*;
/**
* @author Fascinated (fascinated7)
*/
@Getter
public class ReminderProfile extends Serializable {
/**
* The reminders in the guild
*/
private final Map<User, List<Reminder>> reminders = new HashMap<>();
/*
* Get the amount of reminders a user has
*
* @param user The user to get the reminders for
* @return The amount of reminders the user has
*/
public int getReminderCount(User user) {
List<Reminder> reminderList = reminders.get(user);
return reminderList == null ? 0 : reminderList.size();
}
/*
* Remove all reminders for a user
*
* @param user The user to remove the reminders for
*/
public void removeReminders(User user) {
reminders.remove(user);
}
/*
* Check if a user has reminders
*
* @param user The user to check for
* @return If the user has reminders
*/
public boolean hasReminders(User user) {
return reminders.containsKey(user);
}
/*
* Get the reminders for a user
*
* @param user The user to get the reminders for
* @return The reminders for the user
*/
public List<Reminder> getReminders(User user) {
return reminders.get(user);
}
/*
* Add a reminder for a user
*
* @param user The user to add the reminder for
* @param reminder The reminder to add
*/
public Reminder addReminder(User user, TextChannel channel, String reason, Date endDate) {
List<Reminder> reminderList = reminders.get(user);
if (reminderList == null) {
reminderList = new ArrayList<>();
}
Reminder reminder = new Reminder(reason, channel.getId(), endDate);
reminderList.add(reminder);
reminders.put(user, reminderList);
return reminder;
}
/*
* Remove a reminder for a user
*
* @param user The user to remove the reminder for
* @param reminder The reminder to remove
*/
public void removeReminder(User user, Reminder reminder) {
List<Reminder> reminderList = reminders.get(user);
if (reminderList == null) {
return;
}
reminderList.remove(reminder);
reminders.put(user, reminderList);
if (reminderList.isEmpty()) {
reminders.remove(user);
}
}
@Override
public void load(Document document, Gson gson) {
for (String key : document.keySet()) {
User user = DiscordService.JDA.getUserById(key);
if (user == null) {
continue;
}
List<Reminder> reminderList = new ArrayList<>();
for (Document reminderDocument : document.getList(key, Document.class)) {
reminderList.add(new Reminder(
reminderDocument.getString("reminder"),
reminderDocument.getString("channelId"),
reminderDocument.getDate("endDate")
));
}
reminders.put(user, reminderList);
}
}
@Override
public Document serialize(Gson gson) {
Document document = new Document();
for (Map.Entry<User, List<Reminder>> entry : reminders.entrySet()) {
List<Document> reminderDocuments = new ArrayList<>();
List<Reminder> value = entry.getValue();
if (value == null || value.isEmpty()) {
continue;
}
for (Reminder reminder : value) {
Document reminderDocument = new Document();
reminderDocument.append("reminder", reminder.getReminder());
reminderDocument.append("channelId", reminder.getChannelId());
reminderDocument.append("endDate", reminder.getEndDate());
reminderDocuments.add(reminderDocument);
}
document.append(entry.getKey().getId(), reminderDocuments);
}
return document;
}
@Override
public void reset() {
reminders.clear();
}
}