Décrivez ici votre problème ou ce que vous cherchez à faire.

const { Client, GatewayIntentBits, MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');
const { token, logChannelId } = require('./config');

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });

let serviceRecords = {};

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

function createServiceEmbed(user, startTime, endTime) {
    return new MessageEmbed()
        .setColor('#3498db')
        .setTitle('Service Information')
        .setDescription(`Service details for ${user.username}`)
        .addField('Start Time', startTime.toLocaleString(), true)
        .addField('End Time', endTime ? endTime.toLocaleString() : 'Not ended yet', true);
}

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isButton()) return;

    const { customId } = interaction;

    if (customId === 'start') {
        if (!serviceRecords[interaction.user.id]) {
            serviceRecords[interaction.user.id] = { startTime: new Date(), endTime: null };
            const startEmbed = createServiceEmbed(interaction.user, serviceRecords[interaction.user.id].startTime, null);
            await interaction.reply({ content: 'Votre service a commencé!', embeds: [startEmbed] });
        } else {
            await interaction.reply('Vous avez déjà un service en cours.');
        }
    }

    if (customId === 'end') {
        if (serviceRecords[interaction.user.id] && !serviceRecords[interaction.user.id].endTime) {
            const endTime = new Date();
            serviceRecords[interaction.user.id].endTime = endTime;
            const startEmbed = createServiceEmbed(interaction.user, serviceRecords[interaction.user.id].startTime, endTime);
            await interaction.reply({ content: 'Service terminé !', embeds: [startEmbed] });

            if (logChannelId) {
                const logChannel = client.channels.cache.get(logChannelId);
                if (logChannel) {
                    const currentTime = new Date().toLocaleString();
                    const totalElapsedTime = (endTime - serviceRecords[interaction.user.id].startTime) / 1000;
                    const totalHours = Math.floor(totalElapsedTime / 3600);
                    const totalMinutes = Math.floor((totalElapsedTime % 3600) / 60);
                    const totalSeconds = Math.floor(totalElapsedTime % 60);
                    const userDisplayName = interaction.member.nickname || interaction.user.username;
                    logChannel.send(`**[${currentTime}]** ${userDisplayName} : Service terminé ! Total : ${totalHours}h ${totalMinutes}m ${totalSeconds}s.`);
                }
            }

            delete serviceRecords[interaction.user.id];
        } else {
            await interaction.reply('Le timer ne fonctionne pas.');
        }
    }
});

client.on('messageCreate', async (message) => {
    if (message.content === '!total') {
        const playersTotalTime = calculateTotalService();
        let response = 'Temps de service total depuis le lundi :\n';

        playersTotalTime.forEach((data, playerId) => {
            const totalElapsedTime = data.total;
            const totalHours = Math.floor(totalElapsedTime / 3600);
            const totalMinutes = Math.floor((totalElapsedTime % 3600) / 60);
            const totalSeconds = Math.floor(totalElapsedTime % 60);
            const user = client.users.cache.get(playerId);
            const userDisplayName = user ? (user.username || user.tag) : "Utilisateur introuvable";

            response += `${userDisplayName} : ${totalHours}h ${totalMinutes}m ${totalSeconds}s\n`;
        });

        await message.channel.send(response);
    } else if (message.content === '!start' || message.content === '!end') {
        const startButton = new MessageButton()
            .setCustomId('start')
            .setLabel('Start')
            .setStyle('PRIMARY');

        const endButton = new MessageButton()
            .setCustomId('end')
            .setLabel('End')
            .setStyle('DANGER');

        const row = new MessageActionRow().addComponents(startButton, endButton);

        await message.channel.send({ content: 'Choisissez une action :', components: [row] });
    } else if (message.content === '!showembed') {
        const exampleUser = message.author;
        const exampleEmbed = createServiceEmbed(exampleUser, new Date(), null);
        await message.channel.send({ embeds: [exampleEmbed] });
    }
});

function calculateTotalService() {
    const playersTotalTime = new Map();

    const currentTime = new Date();
    const currentDay = currentTime.getDay(); // 0: Dimanche, 1: Lundi, ..., 6: Samedi

    for (const playerId in serviceRecords) {
        const playerData = serviceRecords[playerId];
        if (playerData.startTime) {
            const elapsedTime = (currentTime - playerData.startTime) / 1000;
            if (!playersTotalTime.has(playerId)) {
                playersTotalTime.set(playerId, { total: elapsedTime });
            } else {
                playersTotalTime.get(playerId).total += elapsedTime;
            }
        }
    }

    return playersTotalTime;
}

client.login(token);

Ce que je veux

faire en sorte que quand je lance le bot un embed apparaisse avec LE BOUTON démarer son service et un autre finir son service qui fais que quand le bot est lancé et que on clique sur le bouton démarer sa fasse !start et pareil pour finir

Ce que j'obtiens

Décrivez vos éventuelles erreurs ou ce que vous obtenez à la place de ce que vous attendez :(

Aucune réponse