MCPcopy Create free account
hub / github.com/codebymitch/TitanBot / checkBirthdays

Function checkBirthdays

src/services/birthdayService.js:278–358  ·  view source on GitHub ↗
(client)

Source from the content-addressed store, hash-verified

276}
277
278export async function checkBirthdays(client) {
279 const today = new Date();
280 const currentMonth = today.getUTCMonth() + 1;
281 const currentDay = today.getUTCDate();
282
283 if (process.env.NODE_ENV !== 'production') {
284 logger.debug(`🎂 Running daily birthday check for UTC: ${currentMonth}/${currentDay}.`);
285 }
286
287 for (const [guildId, guild] of client.guilds.cache) {
288 try {
289 const config = await getGuildConfig(client, guildId);
290 const { birthdayChannelId, birthdayRoleId } = config;
291
292 if (!birthdayChannelId || !birthdayRoleId) {
293 if (process.env.NODE_ENV !== 'production') {
294 logger.debug(`Skipping birthday check for ${guild.name}: Missing channel or role config.`);
295 }
296 continue;
297 }
298
299 const channel = await guild.channels.fetch(birthdayChannelId).catch(() => null);
300 if (!channel) continue;
301
302 const trackingKey = `bday-role-tracking-${guildId}`;
303 const trackingData = (await client.db.get(trackingKey)) || {};
304 const updatedTrackingData = { ...trackingData };
305
306 for (const userId of Object.keys(trackingData)) {
307 try {
308 const member = await guild.members.fetch(userId).catch(() => null);
309 if (member && member.roles.cache.has(birthdayRoleId)) {
310 await member.roles.remove(birthdayRoleId, "Birthday role expired");
311 }
312 delete updatedTrackingData[userId];
313 } catch (error) {
314 logger.error(`Error removing birthday role from ${userId}:`, error);
315 }
316 }
317
318 if (Object.keys(updatedTrackingData).length !== Object.keys(trackingData).length) {
319 await client.db.set(trackingKey, updatedTrackingData);
320 }
321
322 const birthdaysKey = `birthdays:${guildId}`;
323 const birthdays = (await client.db.get(birthdaysKey)) || {};
324 const birthdayMembers = [];
325 for (const [userId, userData] of Object.entries(birthdays)) {
326 if (userData.month === currentMonth && userData.day === currentDay) {
327 const member = await guild.members.fetch(userId).catch(() => null);
328 if (member) {
329 birthdayMembers.push(member);
330 try {
331 await member.roles.add(birthdayRoleId, "Happy Birthday! 🎉");
332 updatedTrackingData[userId] = true;
333 } catch (error) {
334 logger.error(`Error adding birthday role to ${member.user.tag}:`, error);
335 }

Callers 1

setupCronJobsMethod · 0.90

Calls 3

getGuildConfigFunction · 0.90
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected