EZD Logik

This commit is contained in:
2026-07-02 18:37:22 +02:00
parent 8d7e47af3f
commit 6b2fab8b95
5 changed files with 374 additions and 63 deletions
+163 -48
View File
@@ -1,6 +1,8 @@
const { ModalBuilder, UserSelectMenuBuilder, TextInputBuilder, EmbedBuilder, TimestampStyles, time, StringSelectMenuBuilder } = require("@discordjs/builders");
const { ModalBuilder, UserSelectMenuBuilder, TextInputBuilder, EmbedBuilder, TimestampStyles, time, StringSelectMenuBuilder, messageLink } = require("@discordjs/builders");
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require('discord.js')
const { SlashCommandBuilder, LabelBuilder, TextInputStyle, MessageFlags } = require("discord.js");
const { mClient } = require("../../..");
const db = mClient.db('neo-db')
require('dotenv').config()
function getRemaining(timer) {
@@ -22,7 +24,7 @@ function ordinal(n) {
}
async function wgeStatus(interaction) {
const db = mClient.db('neo-db')
const wgeColl = db.collection('timer_wge')
const historyColl = db.collection('history_wge');
@@ -62,7 +64,7 @@ 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
@@ -128,7 +130,7 @@ async function wgeStart(interaction) {
}
async function wgeStop(interaction) {
const db = mClient.db('neo-db');
;
const wgeColl = db.collection('timer_wge');
const timer = await wgeColl.findOne({
@@ -162,7 +164,7 @@ async function wgeStop(interaction) {
}
async function wgeResume(interaction) {
const db = mClient.db('neo-db');
;
const wgeColl = db.collection('timer_wge');
const timer = await wgeColl.findOne({
@@ -196,7 +198,7 @@ async function wgeResume(interaction) {
}
async function wgeForfeit(interaction) {
const db = mClient.db('neo-db');
;
const wgeColl = db.collection('timer_wge');
const participateColl = db.collection('items_wge');
@@ -275,14 +277,10 @@ async function wgeForfeit(interaction) {
});
}
async function wgeHistory(interaction) {
//FIXME!
}
async function wgeParticipate(interaction) {
const status = interaction.options.getBoolean('status')
const user = interaction.user
const db = mClient.db('neo-db')
const coll = db.collection('items_wge')
// Choice was false, ergo not participating
@@ -303,50 +301,167 @@ async function wgeParticipate(interaction) {
})
}
async function listParticipants(interaction) {
;
const participantColl = db.collection('items_wge');
const participants = await participantColl.find({}).toArray();
const participating = participants
.filter(p => p.status)
.map(p => `<@${p.userID}>`);
const notParticipating = participants
.filter(p => !p.status)
.map(p => `<@${p.userID}>`);
const embed = new EmbedBuilder()
.setTitle('WGE Participation')
.setColor(0x5865F2)
.addFields(
{
name: `Participating (${participating.length})`,
value: participating.join('\n') || 'No participants found.',
inline: true
},
{
name: `Not Participating (${notParticipating.length})`,
value: notParticipating.join('\n') || 'No entries found.',
inline: true
}
)
.setFooter({
text: `Total entries: ${participants.length}`
})
await interaction.reply({
embeds: [embed],
flags: MessageFlags.Ephemeral
});
}
function formatDuration(ms) {
if (ms == null) return "unknown"
const totalSeconds = Math.floor(ms / 1000)
const days = Math.floor(totalSeconds / 86400)
const hours = Math.floor((totalSeconds % 86400) / 3600)
const minutes = Math.floor((totalSeconds % 3600) / 60)
const seconds = totalSeconds % 60
const parts = []
if (days) parts.push(`${days}d`)
if (hours) parts.push(`${hours}h`)
if (minutes) parts.push(`${minutes}m`)
if (seconds && !days) parts.push(`${seconds}s`) // only show seconds for short durations
return parts.length ? parts.join(" ") : "0s"
}
async function listHistory(interaction) {
const historyColl = db.collection('history_wge')
const wgeHistory = await historyColl
.find({ debug: false })
.sort({ createdAt: -1 })
.toArray()
if (!wgeHistory.length) {
return interaction.reply({
content: "No WGE history found.",
flags: MessageFlags.Ephemeral
})
}
const buildEmbed = (entry, index) => {
const createdTs = entry.createdAt
? Math.floor(new Date(entry.createdAt).getTime() / 1000)
: null
return new EmbedBuilder()
.setTitle(`WGE History (${index + 1}/${wgeHistory.length})`)
.setDescription(
`<@${entry.assignedUser}> was the **World's Greatest Expert** of...\n\n` +
`**${entry.topic}**\n\n` +
`Assigned by: <@${entry.assigningUser}>\n` +
(createdTs ? `Created: <t:${createdTs}:R>` : "")
)
.setColor(0x2b2d31)
}
let page = 0
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('prev')
.setLabel('◀')
.setStyle(ButtonStyle.Secondary)
.setDisabled(true),
new ButtonBuilder()
.setCustomId('next')
.setLabel('▶')
.setStyle(ButtonStyle.Secondary)
.setDisabled(wgeHistory.length <= 1)
)
const msg = await interaction.reply({
embeds: [buildEmbed(wgeHistory[page], page)],
components: [row],
fetchReply: true
})
const collector = msg.createMessageComponentCollector({
componentType: ComponentType.Button,
time: 120000
})
collector.on('collect', async i => {
if (i.user.id !== interaction.user.id) {
return i.reply({
content: "You can't control this pagination.",
flags: MessageFlags.Ephemeral
})
}
if (i.customId === 'prev') {
page--
} else if (i.customId === 'next') {
page++
}
page = Math.max(0, Math.min(page, wgeHistory.length - 1))
row.components[0].setDisabled(page === 0)
row.components[1].setDisabled(page === wgeHistory.length - 1)
await i.update({
embeds: [buildEmbed(wgeHistory[page], page)],
components: [row]
})
})
collector.on('end', async () => {
const disabledRow = new ActionRowBuilder().addComponents(
row.components.map(btn => ButtonBuilder.from(btn).setDisabled(true))
)
await msg.edit({ components: [disabledRow] }).catch(() => { })
})
}
async function wgeList(interaction) {
const thing = interaction.options.getString('thing');
switch (thing) {
case 'participants': {
const db = mClient.db('neo-db');
const participantColl = db.collection('items_wge');
const participants = await participantColl.find({}).toArray();
const participating = participants
.filter(p => p.status)
.map(p => `<@${p.userID}>`);
const notParticipating = participants
.filter(p => !p.status)
.map(p => `<@${p.userID}>`);
const embed = new EmbedBuilder()
.setTitle('WGE Participation')
.setColor(0x5865F2)
.addFields(
{
name: `Participating (${participating.length})`,
value: participating.join('\n') || 'No participants found.',
inline: true
},
{
name: `Not Participating (${notParticipating.length})`,
value: notParticipating.join('\n') || 'No entries found.',
inline: true
}
)
.setFooter({
text: `Total entries: ${participants.length}`
})
await interaction.reply({
embeds: [embed],
flags: MessageFlags.Ephemeral
});
listParticipants(interaction)
break;
}
case 'history':
listHistory(interaction)
break;
default:
break;
}