Add a /bday command where members save their birthday, and wish a happy birthday in #general every morning to whoever it is that day
Members can save their birthday with /bday, and the bot automatically posts a happy birthday message every morning at 9 AM UTC to whoever has a birthday that day.
Setup: Run /setup-bday once to activate the daily check.
Settings: Configure which channel birthday wishes should be posted to.
Usage: Members use /bday date:MM/DD (e.g., /bday date:03/15) to save their birthday.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
✅ Daily birthday check scheduled for 9 AM UTC every day.
🎂 Birthday saved! I'll wish you a happy birthday on 03/15.
🎂 Birthday saved! I'll wish you a happy birthday on 09/14.
🎂 Birthday saved! I'll wish you a happy birthday on 07/04.
Wishing a very happy birthday to @Jordan! Have a wonderful day!
// Birthday Wishes - members save their birthday and get daily wishes
mee6.settings({
channel: {
type: 'channel',
label: 'Birthday wishes channel',
required: true,
sample: 'general',
},
});
// Each member's birthday is kept twice in their storage: 'birthday' ("03/15")
// for reading, and a key named after the date ('bday_03_15') that lets the
// daily check find the day's celebrants in one call. The first version kept
// a list of every member instead ('birthday_members'): the daily check moves
// them over a few at a time.
// Members of the old list moved over per run, and celebrants looked at per
// day: a run makes 50 calls at most.
const MOVE_BATCH = 8;
const MAX_CELEBRANTS = 12;
/** Whether a call was refused because the member has left the server. */
function hasLeft(err: unknown): boolean {
return String(err).includes('is not in this server');
}
/** The key that marks a member's birthday: 'bday_03_15' for "03/15". */
function dayKey(date: string): string {
return `bday_${date.replace('/', '_')}`;
}
/** Today in MM/DD, UTC. */
function todayDate(): string {
const now = new Date();
return `${(now.getUTCMonth() + 1).toString().padStart(2, '0')}/${now.getUTCDate().toString().padStart(2, '0')}`;
}
/**
* Move a few members of the first version's list to their date key, and say
* which of them were born today. Those who have left the server are dropped.
* Schedules another round while the list is not empty.
*/
async function moveOldList(today: string): Promise<string[]> {
const oldList = await mee6.store.get<string[]>('birthday_members', []);
if (oldList.length === 0) return [];
const born: string[] = [];
for (const userId of oldList.slice(0, MOVE_BATCH)) {
const m = mee6.store.member(userId);
const birthday = await m.get<string>('birthday');
if (!birthday) continue;
try {
await m.set(dayKey(birthday), 1);
if (birthday === today) born.push(userId);
} catch (err) {
if (!hasLeft(err)) throw err;
}
}
const rest = oldList.slice(MOVE_BATCH);
if (rest.length) {
await mee6.store.set('birthday_members', rest);
await mee6.schedule.once('birthday_move', 30, {});
} else {
await mee6.store.delete('birthday_members');
}
return born;
}
/** Wish the celebrants a happy birthday. */
async function wish(ctx: Context, channelId: string, celebrants: string[]) {
if (celebrants.length === 0) return;
const mentions = celebrants.map((id) => `<@${id}>`).join(', ');
await ctx.send(channelId, {
embed: {
title: '🎉 Happy Birthday! 🎂',
description: `Wishing a very happy birthday to ${mentions}! Have a wonderful day!`,
color: '#FF69B4',
},
});
}
// Command for members to save their birthday
mee6.command(
'bday',
{
description: 'Save your birthday (MM/DD format)',
options: [
{
name: 'date',
type: 'string',
description: 'Your birthday (MM/DD, e.g., 03/15)',
required: true,
},
],
},
async (ctx) => {
const date = ctx.option<string>('date');
// Validate format MM/DD
const match = date.match(/^(\d{1,2})\/(\d{1,2})$/);
if (!match) {
await ctx.reply('❌ Please use MM/DD format (e.g., 03/15 for March 15th).');
return;
}
const month = parseInt(match[1], 10);
const day = parseInt(match[2], 10);
// Validate ranges
if (month < 1 || month > 12) {
await ctx.reply('❌ Month must be between 1 and 12.');
return;
}
const daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (day < 1 || day > daysInMonth[month - 1]) {
await ctx.reply(`❌ Day must be between 1 and ${daysInMonth[month - 1]} for month ${month}.`);
return;
}
// Store as MM/DD format (normalized with leading zeros)
const normalized = `${month.toString().padStart(2, '0')}/${day.toString().padStart(2, '0')}`;
const m = mee6.store.member(ctx.author.id);
const previous = await m.get<string>('birthday');
if (previous && previous !== normalized) await m.delete(dayKey(previous));
await m.set('birthday', normalized);
await m.set(dayKey(normalized), 1);
await ctx.reply(`🎂 Birthday saved! I'll wish you a happy birthday on ${normalized}.`);
}
);
// Setup command to initialize the daily schedule
mee6.command(
'setup-bday',
{
description: '[Admin] Set up the daily birthday check',
},
async (ctx) => {
// Schedule daily check at 9 AM UTC (cron: minute hour day month weekday)
await mee6.schedule.cron('daily_birthday_check', '0 9 * * *');
await ctx.reply('✅ Daily birthday check scheduled for 9 AM UTC every day.');
}
);
// Daily check for birthdays - runs every morning at 9 AM UTC
mee6.on('scheduled', async (ctx) => {
if (ctx.scheduleKey !== 'daily_birthday_check') return;
const channelId = ctx.settings.channel;
if (!channelId) {
mee6.log('No birthday channel configured');
return;
}
const today = todayDate();
// Today's celebrants still in the server; the date key of one who has left
// goes with them
const celebrants: string[] = [];
for (const { userId } of await mee6.store.memberTop(dayKey(today), MAX_CELEBRANTS)) {
if (await mee6.getMember(userId)) celebrants.push(userId);
else await mee6.store.member(userId).delete(dayKey(today));
}
celebrants.push(...(await moveOldList(today)));
await wish(ctx, channelId, [...new Set(celebrants)]);
});
// The first version's list, moved over a few members at a time
mee6.on('scheduled', async (ctx) => {
if (ctx.scheduleKey !== 'birthday_move') return;
const born = await moveOldList(todayDate());
if (ctx.settings.channel) await wish(ctx, ctx.settings.channel, born);
});