diff --git a/events/timerComplete.js b/events/timerComplete.js index f8c1101..af01ddc 100644 --- a/events/timerComplete.js +++ b/events/timerComplete.js @@ -2,12 +2,15 @@ const { mClient } = require(".."); async function getMember(client, userId) { const guild = client.guilds.cache.get(process.env.D_GuildID); - if (!guild) return null; - // cache first (fast path) + if (!guild) { + return null; + } + + // Cache first let member = guild.members.cache.get(userId); - // fallback (safe path) + // Fallback to API fetch if (!member) { member = await guild.members.fetch(userId).catch(() => null); } @@ -19,60 +22,97 @@ module.exports = { name: 'timerComplete', async execute(timer, client) { - const member = await getMember(client, timer.assignedUser); - if (!member) return; + const db = mClient.db('neo-db'); - await member.send( - `❌ Timer ran out: **${timer.topic}** ...Re-assigning randomly...` - ).catch(() => { }); + const wgeColl = db.collection('timer_wge'); + const participateColl = db.collection('items_wge'); + const channelColl = db.collection('config_channels'); - // Reset Timer, Reassign Randomly - const db = mClient.db('neo-db') - const wgeColl = db.collection('timer_wge') + const guild = client.guilds.cache.get(process.env.D_GuildID); - // Randomizer Function - const participateColl = db.collection('items_wge') + if (!guild) { + console.error('timerComplete: Guild not found.'); + return; + } + + // Notify previous assignee via DM + const previousMember = await getMember(client, timer.assignedUser); + + if (previousMember) { + await previousMember.send( + `❌ Timer ran out: **${timer.topic}** ...Re-assigning randomly...` + ).catch(() => {}); + } + + // Find the configured WGE channel + const wgeChannelConfig = await channelColl.findOne({ + purpose: 'wge', + guildID: process.env.D_GuildID + }); + + if (!wgeChannelConfig) { + console.error('timerComplete: WGE channel configuration not found.'); + return; + } + + const channel = client.channels.cache.get(wgeChannelConfig.channelID); + + if (!channel) { + console.error('timerComplete: WGE channel not found.'); + return; + } + + // Pick a random participant, excluding the previous assignee const randomParticipant = await participateColl.aggregate([ - { $match: { status: true } }, - { $sample: { size: 1 } } - ]).toArray() + { + $match: { + status: true, + userID: { $ne: timer.assignedUser } + } + }, + { + $sample: { + size: 1 + } + } + ]).toArray(); - if (!randomParticipant.length) { return interaction.reply({ content: 'No one else is participating!' }) } + // Nobody else is available + if (!randomParticipant.length) { + await channel.send({ + content: + `The timer for <@${timer.assignedUser}> has run out, ` + + `but there are currently no other participating users available for reassignment.` + }); - const wge = await wgeColl.findOne({ - $and: [ - { debug: false }, - { state: 'running' } - ] - }) + return; + } - const channelColl = db.collection('config_channels') - const guild = await client.guilds.cache.get(process.env.D_GuildID); - const wgeChannel = await channelColl.findOne({ - $and: [ - { purpose: 'wge' }, - { guildID: process.env.D_GuildID } - ] - }) - - const channel = await client.channels.cache.get(wgeChannel.channelID) + const newMember = await guild.members + .fetch(randomParticipant[0].userID) + .catch(() => null); - random = await guild.members.fetch(randomParticipant[0].userID) - wgeAssignedUser = random.user + if (!newMember) { + console.error( + `timerComplete: Could not fetch selected user ${randomParticipant[0].userID}.` + ); + return; + } + const newAssignedUser = newMember.user; + + const wgeDefaultAssignedTime = + 7 * 24 * 60 * 60 * 1000; // 7 days + + // Remove the expired timer specifically await wgeColl.deleteOne({ - _id: wge._id - }) - - const wgeDefaultAssignedTime = 7 * 24 * 60 * 60 * 1000; // 7 days in ms - - channel.send({ - content: `The Timer for <@${wge.assignedUser}> has run out! Assigning randomly... \r\n<@${wgeAssignedUser.id}> is the next expert! Their topic still is **${wge.topic}**` - }) + _id: timer._id + }); + // Create the new timer await wgeColl.insertOne({ - assignedUser: wgeAssignedUser.id, - assigningUser: wge.assigningUser, + assignedUser: newAssignedUser.id, + assigningUser: timer.assigningUser, startedAt: Date.now(), timeRemaining: wgeDefaultAssignedTime, @@ -85,11 +125,17 @@ module.exports = { complete: false }, - topic: wge.topic, + topic: timer.topic, debug: false, createdAt: new Date() }); + // Announce reassignment + await channel.send({ + content: + `The timer for <@${timer.assignedUser}> has run out! Assigning randomly...\n` + + `<@${newAssignedUser.id}> is the next expert! Their topic is still **${timer.topic}**` + }); } }; \ No newline at end of file