rewrote wgeStart from userselect to stringselect with user prefill

This commit is contained in:
2026-06-10 17:26:45 +02:00
parent 0f5ada6c0f
commit f269e15a76
2 changed files with 89 additions and 35 deletions
+53 -17
View File
@@ -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' }
)
)
)
,
+35 -17
View File
@@ -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: [
@@ -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