Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
9.0 kB
7
Indexable
Never
client.on('messageCreate', async (message) => {
    if (message.author.bot || !message.content.startsWith('.p')) return;

    const args = message.content.slice(3).trim().split(' ');
    const command = args.shift().toLowerCase();

    if (!message.member.voice.channel) {
        return client.errNormal({
            error: `You're not in a voice channel!`,
            type: 'reply'
        }, message);
    }

    const channel = message.member.voice.channel;
    let player = client.player.players.get(message.guild.id);

    if (player && (channel.id !== player?.voiceChannel)) {
        return client.errNormal({
            error: `You are not in the same voice channel!`,
            type: 'reply'
        }, message);
    }

    if (!player) {
        player = client.player.create({
            guild: message.guild.id,
            voiceChannel: channel.id,
            textChannel: message.channel.id,
            selfDeafen: true
        });

        if (!channel.joinable) {
            return client.errNormal({
                error: `That channel isn't joinable`,
                type: 'reply'
            }, message);
        }
        
        player.connect();

        setTimeout(() => {
            if (channel.type == Discord.ChannelType.GuildStageVoice) {
                message.guild.members.me.voice.setSuppressed(false);
            }
        }, 500);
    }

    player = client.player.players.get(message.guild.id);
    if (player.state !== "CONNECTED") player.connect();

    const query = args.join(' ');

    client.simpleEmbed({
        desc: `🔎┆Searching...`,
        type: 'reply'
    }, message);

    const res = await player.search(command);

    if (res.loadType === 'LOAD_FAILED') {
        if (!player.queue.current) player.destroy();
        return client.errNormal({
            error: `Error getting music. Please try again in a few minutes`,
            type: 'reply'
        }, message);
    }

    console.log(res);

    switch (res.loadType) {
        case 'NO_MATCHES': {
            if (!player.queue.current) player.destroy();
            await client.errNormal({
                error: `No music was found`,
                type: 'reply'
            }, message);
            break;
        }

        case 'TRACK_LOADED': {
            const track = res.tracks[0];
            await player.queue.add(track);
            if (!player.playing && !player.paused) {
                player.play();
            } else {
                client.embed({
                    title: `${client.emotes.normal.music}・${track.title}`,
                    url: track.uri,
                    desc: `The song has been added to the queue!`,
                    thumbnail: track.thumbnail,
                    fields: [
                        {
                            name: `👤┆Requested By`,
                            value: `${track.requester}`,
                            inline: true
                        },
                        {
                            name: `${client.emotes.normal.clock}┆Ends at`,
                            value: `<t:${((Date.now() / 1000) + (track.duration / 1000)).toFixed(0)}:f>`,
                            inline: true
                        },
                        {
                            name: `🎬┆Author`,
                            value: `${track.author}`,
                            inline: true
                        }
                    ],
                    type: 'reply'
                }, message);
            }
            break;
        }

        case 'PLAYLIST_LOADED': {
            await player.queue.add(res.tracks);
            if (!player.playing && !player.paused) player.play();
            else {
                // Hier kannst du Code hinzufügen, wenn die Wiedergabe bereits läuft
            }
            break;
        }

        case 'SEARCH_RESULT': {
            let max = 5;
            let collected;
            let filter = (i) => i.user.id === message.author.id;
            if (res.tracks.length < max) max = res.tracks.length;

            let row = new Discord.ActionRowBuilder()
                .addComponents(
                    new Discord.ButtonBuilder()
                        .setEmoji("1️⃣")
                        .setCustomId("1")
                        .setStyle(Discord.ButtonStyle.Secondary),

                    new Discord.ButtonBuilder()
                        .setEmoji("2️⃣")
                        .setCustomId("2")
                        .setStyle(Discord.ButtonStyle.Secondary),

                    new Discord.ButtonBuilder()
                        .setEmoji("3️⃣")
                        .setCustomId("3")
                        .setStyle(Discord.ButtonStyle.Secondary),

                    new Discord.ButtonBuilder()
                        .setEmoji("4️⃣")
                        .setCustomId("4")
                        .setStyle(Discord.ButtonStyle.Secondary),

                    new Discord.ButtonBuilder()
                        .setEmoji("5️⃣")
                        .setCustomId("5")
                        .setStyle(Discord.ButtonStyle.Secondary),
                );

            let row2 = new Discord.ActionRowBuilder()
                .addComponents(
                    new Discord.ButtonBuilder()
                        .setEmoji("🛑")
                        .setLabel("Cancel")
                        .setCustomId("cancel")
                        .setStyle(Discord.ButtonStyle.Danger),
                );

            const results = res.tracks
                .slice(0, max)
                .map((track, index) => `**[#${++index}]**┆${track.title.length >= 45 ? `${track.title.slice(0, 45)}...` : track.title}`)
                .join('\n');

            client.embed({
                title: `🔍・Search Results`,
                desc: results,
                fields: [
                    {
                        name: `❓┆Cancel search?`,
                        value: `Press \`cancel\` to stop the search`,
                        inline: true
                    }
                ],
                components: [row, row2],
                type: 'reply'
            }, message);

            try {
                let i = await message.awaitMessageComponent({ filter, max: 1, time: 30e3, componentType: Discord.ComponentType.Button, errors: ['time'] });
            } catch (e) {
                if (!player.queue.current) player.destroy();
                row.components.forEach((button) => button.setDisabled(true));
                row2.components.forEach((button) => button.setDisabled(true));
                return client.errNormal({
                    error: `You didn't provide a selection`,
                    type: 'reply',
                    components: [row, row2]
                }, message);
            }

            const first = i.customId;
            i.message.delete();
            i.deferUpdate();

            if (first.toLowerCase() === 'cancel') {
                if (!player.queue.current) player.destroy();
                return message.channel.send('Cancelled selection.');
            }

            const index = Number(first) - 1;
            if (index < 0 || index > max - 1) return client.errNormal({
                error: `The number you provided is too small or too big (1-${max})`,
                type: 'reply'
            }, message);

            const track = res.tracks[index];
            player.queue.add(track);

            if (!player.playing && !player.paused) {
                player.play();
            } else {
                client.embed({
                    title: `${client.emotes.normal.music}・${track.title}`,
                    url: track.uri,
                    desc: `The song has been added to the queue!`,
                    thumbnail: track.thumbnail,
                    fields: [
                        {
                            name: `👤┆Requested By`,
                            value: `${track.requester}`,
                            inline: true
                        },
                        {
                            name: `${client.emotes.normal.clock}┆Ends at`,
                            value: `<t:${((Date.now() / 1000) + (track.duration / 1000)).toFixed(0)}:f>`,
                            inline: true
                        },
                        {
                            name: `🎬┆Author`,
                            value: `${track.author}`,
                            inline: true
                        }
                    ],
                    type: 'reply'
                }, message);
            }
        }
    }
});