Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 5x 5x 5x 5x 36x 36x 36x 36x 36x 36x 1x 36x 8x 8x 8x 1x 7x 7x 1x 6x 3x 3x 1x 5x 1x 5x 5x 5x 4x 4x 3x 3x 5x 5x 36x 36x 1x 1x 1x 1x 1x 1x 4x 4x 3x 2x 2x 1x 2x 3x 1x 1x 5x 5x 4x 4x 5x 5x 4x 4x 4x 3x 4x 4x 1x 1x 5x | const EventStorage = require('../storage/EventStorage');
const EventValidator = require('../validators/EventValidator');
const EventNotificationManager = require('./EventNotificationManager');
const EventReminderManager = require('./EventReminderManager');
/**
* EventManager - Community event coordination and management system
*
* Orchestrates a comprehensive event management system that enables community members
* to create, manage, and track local and regional events.
*
* System design rationale:
* - Role-based organization (region/location) enables targeted notifications
* - Automated reminder system ensures events don't get forgotten
* - Validation prevents invalid or poorly formatted events
* - Persistent storage enables event history and analytics
*
* Features:
* - Event creation with role-based targeting
* - Automated reminder notifications (week/day/soon)
* - Event removal and cancellation management
* - Regional and location-based filtering
* - Integration with Discord role system for notifications
*/
class EventManager {
constructor(bot) {
this.bot = bot;
this.storage = new EventStorage(process.env.EVENTS_TABLE || this.bot.getEventsTable());
// Initialize specialized components
this.validator = new EventValidator(bot);
this.notificationManager = new EventNotificationManager(bot);
this.reminderManager = new EventReminderManager(bot, this.storage);
// Start reminder checker (runs every hour) - skip only during Jest testing
if (!process.env.JEST_WORKER_ID) {
this.reminderManager.startReminderChecker();
}
console.log(`EventManager initialized with specialized components`);
}
/**
* Create a new community event with comprehensive validation
*
* Validates event data, stores in database, and sends notifications to appropriate
* regional and location-based channels.
*
* @param {string} guildId - Discord guild ID
* @param {Object} eventData - Event details (name, region, location, date, link)
* @param {Object} createdBy - Discord user who created the event
* @param {Object} regionRole - Discord role for regional notifications
* @param {Object} locationRole - Discord role for location-specific notifications
* @returns {Object} - Created event with database ID
*/
async createEvent(guildId, eventData, createdBy, regionRole = null, locationRole = null, quiet = false) {
try {
// Validate event data
const validation = this.validator.validateEventData(eventData);
if (!validation.valid) {
throw new Error(validation.error);
}
// Get guild
const guild = this.bot.client.guilds.cache.get(guildId);
if (!guild) {
throw new Error('Guild not found');
}
// If roles weren't passed, try to find them by name (fallback for backward compatibility)
if (!regionRole) {
regionRole = this.validator.findRoleByName(guild, eventData.region);
if (!regionRole) {
throw new Error(`Region role "${eventData.region}" not found`);
}
}
if (!locationRole && eventData.location) {
locationRole = this.validator.findRoleByName(guild, eventData.location);
}
// Convert date to ISO string for consistent storage (assuming British time)
const eventDate = new Date(eventData.eventDate);
const eventDataWithISODate = {
...eventData,
eventDate: eventDate.toISOString(),
createdBy: createdBy.id
};
// Create event in database
const event = await this.storage.createEvent(guildId, eventDataWithISODate);
// Send notification to region and location channels (unless quiet mode)
Eif (!quiet) {
await this.notificationManager.sendEventNotification(guild, event, regionRole, locationRole);
}
// Reschedule reminders to include this new event
this.reminderManager.rescheduleReminders();
return event;
} catch (error) {
console.error('Error creating event:', error);
throw error;
}
}
/**
* Cleanup timers - call this during shutdown or in tests
*/
cleanup() {
this.reminderManager.cleanup();
console.log('EventManager cleaned up');
}
/**
* Get events for a region
*/
async getEventsByRegion(guildId, region) {
return await this.storage.getEventsByRegion(guildId, region);
}
/**
* Get next 3 upcoming events for a region
*/
async getUpcomingEventsByRegion(guildId, region) {
return await this.storage.getUpcomingEventsByRegion(guildId, region, 3);
}
/**
* Get next 3 upcoming events for a location
*/
async getUpcomingEventsByLocation(guildId, location) {
return await this.storage.getUpcomingEventsByLocation(guildId, location, 3);
}
/**
* Delete an event
*/
async deleteEvent(guildId, eventId) {
return await this.storage.deleteEvent(guildId, eventId);
}
/**
* Get all upcoming events for a guild
*/
async getUpcomingEvents(guildId) {
return await this.storage.getUpcomingEvents(guildId);
}
async getAllUpcomingEvents(guildId, limit = 50) {
return await this.storage.getAllUpcomingEvents(guildId, limit);
}
/**
* Remove an event by criteria
*/
async removeEvent(guildId, eventCriteria, removedBy) {
try {
const result = await this.storage.removeEventByCriteria(guildId, eventCriteria);
if (result.success && result.event) {
// Get guild for notifications
const guild = this.bot.client.guilds.cache.get(guildId);
if (guild) {
// Send cancellation notification
await this.notificationManager.sendCancellationNotification(guild, result.event);
}
// Reschedule reminders
this.reminderManager.rescheduleReminders();
}
return result;
} catch (error) {
console.error('Error removing event:', error);
return { success: false, error: error.message };
}
}
/**
* Clear all events for a guild
*/
async clearAllEvents(guildId, removedBy) {
try {
const events = await this.storage.getUpcomingEvents(guildId);
let clearedCount = 0;
for (const event of events) {
const result = await this.storage.deleteEvent(guildId, event.event_id);
if (result) {
clearedCount++;
// Get guild for notifications
const guild = this.bot.client.guilds.cache.get(guildId);
if (guild) {
// Send cancellation notification
await this.notificationManager.sendCancellationNotification(guild, event);
}
}
}
// Reschedule reminders
this.reminderManager.rescheduleReminders();
return clearedCount;
} catch (error) {
console.error('Error clearing events:', error);
return 0;
}
}
}
module.exports = EventManager; |