Compare commits

..

2 Commits

Author SHA1 Message Date
admin_rb 8d7e47af3f gave birthday a notified status to only notify once a day 2026-06-12 21:44:52 +02:00
admin_rb a94562c40b added sanity check for forfeit 2026-06-12 21:44:35 +02:00
3 changed files with 92 additions and 34 deletions
+54 -30
View File
@@ -172,7 +172,7 @@ async function wgeResume(interaction) {
if (!timer) { if (!timer) {
return interaction.reply({ return interaction.reply({
content: 'Timer not found.', content: 'Timer not found.',
ephemeral: true flags: MessageFlags.Ephemeral
}); });
} }
@@ -196,45 +196,65 @@ async function wgeResume(interaction) {
} }
async function wgeForfeit(interaction) { async function wgeForfeit(interaction) {
// Reset Timer, Reassign Randomly const db = mClient.db('neo-db');
const db = mClient.db('neo-db') const wgeColl = db.collection('timer_wge');
const wgeColl = db.collection('timer_wge') const participateColl = db.collection('items_wge');
// Randomizer Function
const participateColl = db.collection('items_wge')
const randomParticipant = await participateColl.aggregate([
{ $match: { status: true } },
{ $sample: { size: 1 } }
]).toArray()
if (!randomParticipant.length) { return interaction.reply({ content: 'No one else is participating!' }) }
random = await interaction.guild.members.fetch(randomParticipant[0].userID)
wgeAssignedUser = random.user
// Get active WGE
const wge = await wgeColl.findOne({ const wge = await wgeColl.findOne({
$and: [ debug: false,
{ debug: false }, state: 'running'
{ state: 'running' } });
]
})
await wgeColl.deleteOne({ if (!wge) {
_id: wge._id return interaction.reply({
}) content: 'No active WGE is currently running.',
flags: MessageFlags.Ephemeral
});
}
const wgeDefaultAssignedTime = 7 * 24 * 60 * 60 * 1000; // 7 days in ms // Sanity check: only assigned user can forfeit
if (wge.assignedUser !== interaction.user.id) {
return interaction.reply({
content: `Only <@${wge.assignedUser}> can forfeit the current WGE.`,
flags: MessageFlags.Ephemeral
});
}
interaction.reply({ // Pick a new participant, excluding current assignee
content: `${interaction.user} has forfeited! <@${wgeAssignedUser.id}> is the next expert! Their topic is **${wge.topic}**` const [randomParticipant] = await participateColl.aggregate([
}) {
$match: {
status: true,
userID: { $ne: interaction.user.id }
}
},
{ $sample: { size: 1 } }
]).toArray();
if (!randomParticipant) {
return interaction.reply({
content: 'No one else is participating!',
flags: MessageFlags.Ephemeral
});
}
const member = await interaction.guild.members.fetch(
randomParticipant.userID
);
const nextUser = member.user;
const WGE_DURATION = 7 * 24 * 60 * 60 * 1000; // 7 days
await wgeColl.deleteOne({ _id: wge._id });
await wgeColl.insertOne({ await wgeColl.insertOne({
assignedUser: wgeAssignedUser.id, assignedUser: nextUser.id,
assigningUser: wge.assigningUser, assigningUser: wge.assigningUser,
startedAt: Date.now(), startedAt: Date.now(),
timeRemaining: wgeDefaultAssignedTime, timeRemaining: WGE_DURATION,
state: 'running', state: 'running',
@@ -249,6 +269,10 @@ async function wgeForfeit(interaction) {
createdAt: new Date() createdAt: new Date()
}); });
return interaction.reply({
content: `${interaction.user} has forfeited! <@${nextUser.id}> is the next expert! Their topic is **${wge.topic}**`
});
} }
async function wgeHistory(interaction) { async function wgeHistory(interaction) {
+13
View File
@@ -101,5 +101,18 @@ module.exports = {
description: 'a birthday banner' description: 'a birthday banner'
}] }]
}); });
// mark as notified
const bdayColl = db.collection('items_birthdays')
await bdayColl.findOneAndUpdate({
userID: member.user.id
}, {
$set: {
notified: true
}
},{
upsert: true
})
} }
}; };
+25 -4
View File
@@ -48,21 +48,42 @@ module.exports = {
for (const member of members.values()) { for (const member of members.values()) {
const dbEntry = bdayMap.get(member.id); const dbEntry = bdayMap.get(member.id);
if (dbEntry && isToday(dbEntry.day, dbEntry.month)) { if (!dbEntry) continue;
// Today is their birthday
// skip if already notified
if (dbEntry.notified) {
console.log(`${member.user.username} was already notified today, skipping...`);
continue;
}
if (isToday(dbEntry.day, dbEntry.month)) {
await member.roles.add(birthdayRoleID).catch(err => await member.roles.add(birthdayRoleID).catch(err =>
console.error(`Failed to add birthday role to ${member.user.tag}:`, err) console.error(`Failed to add birthday role to ${member.user.tag}:`, err)
); );
console.log(`Added birthday role to ${member.user.tag}`); console.log(`Added birthday role to ${member.user.tag}`);
client.emit('Birthday', member); client.emit('Birthday', member);
// mark as notified
await bdayColl.updateOne(
{ userID: member.id },
{ $set: { notified: true } }
);
} else { } else {
// Default fallback: remove birthday role if present
if (member.roles.cache.has(birthdayRoleID)) { if (member.roles.cache.has(birthdayRoleID)) {
await member.roles.remove(birthdayRoleID).catch(err => await member.roles.remove(birthdayRoleID).catch(err =>
console.error(`Failed to remove birthday role from ${member.user.tag}:`, err) console.error(`Failed to remove birthday role from ${member.user.tag}:`, err)
); );
console.log(`Removed birthday role from ${member.user.tag} (not today or not in DB)`);
console.log(`Removed birthday role from ${member.user.tag}`);
} }
// reset notification flag if not birthday anymore
await bdayColl.updateOne(
{ userID: member.id },
{ $set: { notified: false } }
);
} }
} }
}; };