From a94562c40b53fb9b0811a9d85e8f880be69dddce Mon Sep 17 00:00:00 2001 From: admin_rb Date: Fri, 12 Jun 2026 21:44:35 +0200 Subject: [PATCH] added sanity check for forfeit --- commands/slash/applications/wge.js | 84 +++++++++++++++++++----------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/commands/slash/applications/wge.js b/commands/slash/applications/wge.js index bbc1aa4..84b0687 100644 --- a/commands/slash/applications/wge.js +++ b/commands/slash/applications/wge.js @@ -172,7 +172,7 @@ async function wgeResume(interaction) { if (!timer) { return interaction.reply({ content: 'Timer not found.', - ephemeral: true + flags: MessageFlags.Ephemeral }); } @@ -196,45 +196,65 @@ async function wgeResume(interaction) { } async function wgeForfeit(interaction) { - // Reset Timer, Reassign Randomly - const db = mClient.db('neo-db') - const wgeColl = db.collection('timer_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 + const db = mClient.db('neo-db'); + const wgeColl = db.collection('timer_wge'); + const participateColl = db.collection('items_wge'); + // Get active WGE const wge = await wgeColl.findOne({ - $and: [ - { debug: false }, - { state: 'running' } - ] - }) + debug: false, + state: 'running' + }); - await wgeColl.deleteOne({ - _id: wge._id - }) + if (!wge) { + 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({ - content: `${interaction.user} has forfeited! <@${wgeAssignedUser.id}> is the next expert! Their topic is **${wge.topic}**` - }) + // Pick a new participant, excluding current assignee + 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({ - assignedUser: wgeAssignedUser.id, + assignedUser: nextUser.id, assigningUser: wge.assigningUser, startedAt: Date.now(), - timeRemaining: wgeDefaultAssignedTime, + timeRemaining: WGE_DURATION, state: 'running', @@ -249,6 +269,10 @@ async function wgeForfeit(interaction) { 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) {