AltitudeBot/src/main/java/com/alttd/database/queries/events/Event.java
Teriuihi 27f1920081 Create event management system, added event notifier for community events
Added functionalities to create and manage events, including the creation of necessary database tables, context menus, modals, and scheduling tasks. Introduced `UserToMessageTracker` utility for tracking user messages within modals.
2024-08-07 00:18:40 +02:00

80 lines
2.5 KiB
Java

package com.alttd.database.queries.events;
import com.alttd.AltitudeBot;
import com.alttd.util.Logger;
import lombok.Getter;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import java.time.Instant;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Optional;
public final class Event {
private static final HashMap<Long, Event> eventMap = new HashMap<>();
@Getter
private final long messageId, guildId, channelId, roleId;
@Getter
private final Instant startTime;
@Getter
private Role role = null;
@Getter
private final String title;
public static Event getEvent(long messageId) {
return eventMap.get(messageId);
}
private Message message = null;
public Event(long messageId, long guildId, long channelId, Instant startTime, long roleId, String title) {
this.messageId = messageId;
this.guildId = guildId;
this.channelId = channelId;
this.startTime = startTime;
this.roleId = roleId;
this.title = title;
eventMap.put(messageId, this);
loadMessageAndRole();
Logger.altitudeLogs.info(String.format("Loaded event with title [%s] and message id [%s]", title, messageId));
}
public static Optional<Event> getNextEvent() {
return eventMap.values().stream().min(Comparator.comparing(Event::getStartTime));
}
public static void removeEvent(Event event) {
eventMap.remove(event.getMessageId());
}
private void loadMessageAndRole() {
Guild guild = AltitudeBot.getInstance().getJDA().getGuildById(guildId);
if (guild == null) {
Logger.altitudeLogs.error(String.format("Unable to find guild %s when creating event", guildId));
return;
}
TextChannel textChannel = guild.getTextChannelById(channelId);
if (textChannel == null) {
Logger.altitudeLogs.error(String.format("Unable to find text channel %s when creating event", channelId));
return;
}
textChannel.retrieveMessageById(messageId).queue(message -> {
this.message = message;
Logger.altitudeLogs.debug(String.format("Loaded message for event with title [%s]", title));
});
role = guild.getRoleById(roleId);
}
public Optional<Message> getMessage() {
return message == null ? Optional.empty() : Optional.of(message);
}
};