From f269e15a76492ffc04ef4a73082bd6bcd7f260c8 Mon Sep 17 00:00:00 2001 From: admin_rb Date: Wed, 10 Jun 2026 17:26:45 +0200 Subject: [PATCH] rewrote wgeStart from userselect to stringselect with user prefill --- commands/slash/applications/wge.js | 70 ++++++++++++++++++++++-------- events/InteractionCreate.js | 54 +++++++++++++++-------- 2 files changed, 89 insertions(+), 35 deletions(-) diff --git a/commands/slash/applications/wge.js b/commands/slash/applications/wge.js index dc3da3d..bbc1aa4 100644 --- a/commands/slash/applications/wge.js +++ b/commands/slash/applications/wge.js @@ -1,4 +1,4 @@ -const { ModalBuilder, UserSelectMenuBuilder, TextInputBuilder, EmbedBuilder, TimestampStyles, time } = require("@discordjs/builders"); +const { ModalBuilder, UserSelectMenuBuilder, TextInputBuilder, EmbedBuilder, TimestampStyles, time, StringSelectMenuBuilder } = require("@discordjs/builders"); const { SlashCommandBuilder, LabelBuilder, TextInputStyle, MessageFlags } = require("discord.js"); const { mClient } = require("../../.."); require('dotenv').config() @@ -59,34 +59,70 @@ async function wgeStatus(interaction) { } async function wgeStart(interaction) { + const defaultUsers = [] + defaultUsers.push(process.env.D_ID) + + const db = mClient.db('neo-db') + const participantColl = db.collection('items_wge') + const participants = await participantColl.find({ + status: true + }).toArray() + + const wgeBaseModal = new ModalBuilder() .setCustomId('wgeBaseModal') .setTitle("World's Greatest Expert") - const wgeUserInput = new UserSelectMenuBuilder() - .setCustomId('wgeUserInput') + const options = [ + { + label: 'RANDOM', + value: process.env.D_ID + } + ]; + + const participantOptions = await Promise.all( + participants.map(async (participant) => { + try { + const member = await interaction.guild.members.fetch(participant.userID); + return { + label: `${member.displayName} (${member.user.username})`, + value: participant.userID + }; + } catch { + return null; + } + }) + ); + + options.push( + ...participantOptions.filter(option => option !== null) + ); + + // Using StringSelectMenuBuilder because UserSelectMenuBuilder cannot be filtered for now + const wgeUserCustInput = new StringSelectMenuBuilder() + .setCustomId('wgeCustUserInput') .setMaxValues(1) + .setMinValues(1) .setRequired(true) - .addDefaultUsers(process.env.D_ID) - // DefaultUser is randomizer + .addOptions(options) const wgeTopicInput = new TextInputBuilder() .setCustomId('wgeTopicInput') .setStyle(TextInputStyle.Short) .setRequired(true) - const wgeUserLabel = new LabelBuilder() - .setLabel("Who is YOUR World's Greatest Expert?") - .setDescription('Select them!') - .setUserSelectMenuComponent(wgeUserInput) - const wgeTopicLabel = new LabelBuilder() .setLabel("And WHAT are they an expert in?") .setDescription('Tell me!') .setTextInputComponent(wgeTopicInput) + const wgeCustUserLabel = new LabelBuilder() + .setLabel('WHO is your greatest expert?') + .setDescription('Select them') + .setStringSelectMenuComponent(wgeUserCustInput) + wgeBaseModal - .addLabelComponents(wgeUserLabel, wgeTopicLabel) + .addLabelComponents(wgeCustUserLabel, wgeTopicLabel) await interaction.showModal(wgeBaseModal) } @@ -179,7 +215,7 @@ async function wgeForfeit(interaction) { const wge = await wgeColl.findOne({ $and: [ { debug: false }, - { state: 'running'} + { state: 'running' } ] }) @@ -357,11 +393,11 @@ module.exports = { .setDescription('list things') .addStringOption(o => o.setName('thing') - .setDescription('what would you like to be listed') - .addChoices( - { name: 'participants', value: 'participants'}, - { name: 'history', value: 'history'} - ) + .setDescription('what would you like to be listed') + .addChoices( + { name: 'participants', value: 'participants' }, + { name: 'history', value: 'history' } + ) ) ) , diff --git a/events/InteractionCreate.js b/events/InteractionCreate.js index 6aaf47d..28d2496 100644 --- a/events/InteractionCreate.js +++ b/events/InteractionCreate.js @@ -1,4 +1,4 @@ -const { Events, MessageFlags } = require('discord.js') +const { Events, MessageFlags, Message } = require('discord.js') const { mClient } = require('../index') require('dotenv').config() @@ -63,6 +63,40 @@ async function modalSubmitHandler(interaction) { if (interaction.customId === 'wgeBaseModal') { const db = mClient.db('neo-db') const wgeColl = db.collection('timer_wge') + const participateColl = db.collection('items_wge') + + // Get the data entered by the user + + var wgeAssignedUser = {} + wgeAssignedUser.id = interaction.components[0].component.values[0] + + const wgeAssigningUser = interaction.user + if (wgeAssignedUser.id === process.env.D_ID) { + // Randomizer Function + const randomParticipant = await participateColl.aggregate([ + { $match: { status: true } }, + { $sample: { size: 1 } } + ]).toArray() + + if (!randomParticipant) { return interaction.followUp({ content: 'No User is participating!' }) } + + random = await interaction.guild.members.fetch(randomParticipant[0].userID) + wgeAssignedUser = random.user + } else { + // validate non-random user + const foundParticipant = await participateColl.findOne({ + $and: [ + { assignedUser: wgeAssignedUser.id }, + { status: true} + ] + }) + + if(!foundParticipant || foundParticipant == null){ return interaction.reply({ + content: `<@${wgeAssignedUser.id}> is not participating!`, + flags: MessageFlags.Ephemeral + })} + } + const prevWge = await wgeColl.findOne({ $and: [ @@ -70,7 +104,7 @@ async function modalSubmitHandler(interaction) { { state: 'running' } ] }) - + if (prevWge != null) { // Save previous run in history_wge const historyWgeColl = db.collection('history_wge') @@ -93,22 +127,6 @@ async function modalSubmitHandler(interaction) { }) } - // Get the data entered by the user - var wgeAssignedUser = interaction.components[0].component.users.first(); - const wgeAssigningUser = interaction.user - if (wgeAssignedUser.id === process.env.D_ID) { - // Randomizer Function - const participateColl = db.collection('items_wge') - const randomParticipant = await participateColl.aggregate([ - { $match: { status: true } }, - { $sample: { size: 1 } } - ]).toArray() - - if (!randomParticipant) { return interaction.followUp({ content: 'No User is participating!' }) } - - random = await interaction.guild.members.fetch(randomParticipant[0].userID) - wgeAssignedUser = random.user - } const wgeAssignedTopic = interaction.fields.getTextInputValue('wgeTopicInput'); const wgeDefaultAssignedTime = 7 * 24 * 60 * 60 * 1000; // 7 days in ms