no longer reassigns the same user

This commit is contained in:
2026-08-16 13:01:32 +02:00
parent c99a96c201
commit 71e53c8014
+92 -46
View File
@@ -2,12 +2,15 @@ const { mClient } = require("..");
async function getMember(client, userId) { async function getMember(client, userId) {
const guild = client.guilds.cache.get(process.env.D_GuildID); 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); let member = guild.members.cache.get(userId);
// fallback (safe path) // Fallback to API fetch
if (!member) { if (!member) {
member = await guild.members.fetch(userId).catch(() => null); member = await guild.members.fetch(userId).catch(() => null);
} }
@@ -19,60 +22,97 @@ module.exports = {
name: 'timerComplete', name: 'timerComplete',
async execute(timer, client) { async execute(timer, client) {
const member = await getMember(client, timer.assignedUser); const db = mClient.db('neo-db');
if (!member) return;
await member.send( const wgeColl = db.collection('timer_wge');
`❌ Timer ran out: **${timer.topic}** ...Re-assigning randomly...` const participateColl = db.collection('items_wge');
).catch(() => { }); const channelColl = db.collection('config_channels');
// Reset Timer, Reassign Randomly const guild = client.guilds.cache.get(process.env.D_GuildID);
const db = mClient.db('neo-db')
const wgeColl = db.collection('timer_wge')
// Randomizer Function if (!guild) {
const participateColl = db.collection('items_wge') 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([ const randomParticipant = await participateColl.aggregate([
{ $match: { status: true } }, {
{ $sample: { size: 1 } } $match: {
]).toArray() 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({ return;
$and: [ }
{ debug: false },
{ state: 'running' }
]
})
const channelColl = db.collection('config_channels') const newMember = await guild.members
const guild = await client.guilds.cache.get(process.env.D_GuildID); .fetch(randomParticipant[0].userID)
const wgeChannel = await channelColl.findOne({ .catch(() => null);
$and: [
{ purpose: 'wge' },
{ guildID: process.env.D_GuildID }
]
})
const channel = await client.channels.cache.get(wgeChannel.channelID)
random = await guild.members.fetch(randomParticipant[0].userID) if (!newMember) {
wgeAssignedUser = random.user 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({ await wgeColl.deleteOne({
_id: wge._id _id: timer._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}**`
})
// Create the new timer
await wgeColl.insertOne({ await wgeColl.insertOne({
assignedUser: wgeAssignedUser.id, assignedUser: newAssignedUser.id,
assigningUser: wge.assigningUser, assigningUser: timer.assigningUser,
startedAt: Date.now(), startedAt: Date.now(),
timeRemaining: wgeDefaultAssignedTime, timeRemaining: wgeDefaultAssignedTime,
@@ -85,11 +125,17 @@ module.exports = {
complete: false complete: false
}, },
topic: wge.topic, topic: timer.topic,
debug: false, debug: false,
createdAt: new Date() 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}**`
});
} }
}; };