Androz2091 / discord-player

🎧 Complete framework to simplify the implementation of music commands using discord.js v14
https://discord-player.js.org/
MIT License
589 stars 192 forks source link

A persistent error prevents playback from working. #1890

Closed B3ni15 closed 6 months ago

B3ni15 commented 6 months ago

Describe the bug When I type command, I get an error and it doesn't do anything when it should, because that's how it's written in the documentation.

The Error:

2024-02-27 13:45:53 [info] [ERROR] [unhandledRejection] Cannot read properties of undefined (reading 'createQueue')
TypeError: Cannot read properties of undefined (reading 'createQueue')
    at Object.run (/root/bot/src/slash/play.js:67:30)
    at Handler.<anonymous> (/root/bot/node_modules/discord-slash-command-handler/src/classes/handler.js:183:33)
    at Generator.next (<anonymous>)
    at fulfilled (/root/bot/node_modules/discord-slash-command-handler/src/classes/handler.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) Promise {
  <rejected> TypeError: Cannot read properties of undefined (reading 'createQueue')
      at Object.run (/root/bot/src/slash/play.js:67:30)
      at Handler.<anonymous> (/root/bot/node_modules/discord-slash-command-handler/src/classes/handler.js:183:33)
      at Generator.next (<anonymous>)
      at fulfilled (/root/bot/node_modules/discord-slash-command-handler/src/classes/handler.js:5:58)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
}

The code:

const { MessageEmbed, } = require("discord.js");
const { QueryType, player } = require("discord-player")

module.exports = {
    name: "play",
    description: "Zene lejátszása",
    category: "song",
    slash: true,
    global: true,
    error: async () => {},
    options: [
        {
            name: "zene_keresése",
            description: "Zene keresése a YouTube-on.",
            type: 1,
            options: [
                {
                    name: "szöveg",
                    description: "Ă­rd ide a cĂ­met",
                    type: 3,
                    required: true,
                },
            ],
        },
        {
            name: "zene_link",
            description: "Youtube link",
            type: 1,
            options: [
                {
                    name: "url",
                    description: "Ă­rd ide a linket",
                    type: 3,
                    required: true,
                },
            ],
        },
        {
            name: "playlist_link",
            description: "Youtube playlist link",
            type: 1,
            options: [
                {
                    name: "url",
                    description: "Ă­rd ide a linket",
                    type: 3,
                    required: true,
                },
            ],
        },
    ],

    run: async (data) => {
        const userVoiceChannel = data.interaction.guild.members.cache.get(data.interaction.user.id).voice.channel;

        if (!userVoiceChannel) {
            const errorEmbed = new MessageEmbed()
                .setTitle(`Hiba`)
                .setDescription(`> Nem vagy egy voice csatornán!`)
                .setColor("#FF0000");

            return data.interaction.editReply({
                embeds: [errorEmbed],
            });
        }

        const queue = await player.createQueue(data.interaction.guild);
        if (!queue.connection) await queue.connect(data.interaction.member.voice.channel)

        let embed = new MessageEmbed()
        if (data.interaction.getSubcommand() === "zene_link") {
            let url = data.interaction.options.getString("url")

            const result = await player.search(url, {
                requestedBy: data.interaction.user,
                searchEngine: QueryType.YOUTUBE_VIDEO
            })

            if (result.tracks.length === 0)
                return data.interaction.reply("> Nincs válasz!")

            const song = result.tracks[0]
            await queue.addTrack(song)
            embed
                .setDescription(`**[${song.title}](${song.url})** Hozzá lett adva a várólistához.`)
                .setThumbnail(song.thumbnail)
                .setFooter({ text: `Idő: ${song.duration}`})

        }
        else if (data.interaction.options.getSubcommand() === "playlist_link") {
            let url = data.interaction.options.getString("url")

            const result = await player.search(url, {
                requestedBy: data.interaction.user,
                searchEngine: QueryType.YOUTUBE_PLAYLIST
            })

            if (result.tracks.length === 0)
                return data.interaction.reply(`Nincs ilyen lejátszási lista ${url}`)

            const playlist = result.playlist
            await queue.addTracks(result.tracks)
            embed
                .setDescription(`**${result.tracks.length} zene betöltve [${playlist.title}](${playlist.url})** hozzá lett adva!`)
                .setThumbnail(playlist.thumbnail)

        } 
        else if (data.interaction.options.getSubcommand() === "zene_keresése") {
            let url = data.interaction.options.getString("szöveg")
            const result = await player.search(url, {
                requestedBy: data.interaction.user,
                searchEngine: QueryType.AUTO
            })

            if (result.tracks.length === 0)
                return data.interaction.editReply("> Nincs válasz!")

            const song = result.tracks[0]
            await queue.addTrack(song)
            embed
                .setDescription(`**[${song.title}](${song.url})** hozzá lett adva a várólistához.`)
                .setThumbnail(song.thumbnail)
                .setFooter({ text: `idő: ${song.duration}`})
        }

        if (!queue.playing) await queue.play()

        await data.interaction.reply({
            embeds: [embed]
        })
    },
};

Please complete the following information:

twlite commented 6 months ago

@B3ni15 documentation does not state player.createQueue or the ability to import player from discord-player.

// main file
const player = new Player(client);

// player events
// this event is emitted whenever discord-player starts to play a track
player.events.on('playerStart', (queue, track) => {
    queue.metadata.channel.send(`Started playing **${track.title}**!`);
});
// your play command
const { useMainPlayer } = require('discord-player');

export async function execute(interaction) {
    const player = useMainPlayer();
    const channel = interaction.member.voice.channel;
    if (!channel) return interaction.reply('You are not connected to a voice channel!'); // make sure we have a voice channel
    const query = interaction.options.getString('query', true); // we need input/query to play

    // let's defer the interaction as things can take time to process
    await interaction.deferReply();

    try {
        const { track } = await player.play(channel, query, {
            nodeOptions: {
                // nodeOptions are the options for guild node (aka your queue in simple word)
                metadata: interaction // we can access this metadata object using queue.metadata later on
            }
        });

        return interaction.followUp(`**${track.title}** enqueued!`);
    } catch (e) {
        // let's return error if something failed
        return interaction.followUp(`Something went wrong: ${e}`);
    }
}

this is how a simple play command looks like.

B3ni15 commented 6 months ago

Thanks.

B3ni15 commented 6 months ago

But @twlite there is a other error:

image

B3ni15 commented 6 months ago

The code:

const { MessageEmbed, } = require("discord.js");
const useMainPlayer = require('discord-player');

module.exports = {
    name: "play",
    description: "Zene lejátszása",
    category: "song",
    slash: true,
    global: true,
    error: async () => {},
    options: [
        {
            name: "query",
            description: "Zene hozzáadása a várólistához.",
            type: 3,
            required: true,
        },
    ],
    run: async (data) => {
        const player = useMainPlayer();
        const channel = data.interaction.member.voice.channel;
        if (!channel) return data.interaction.reply('You are not connected to a voice channel!');
        const query = data.interaction.options.getString('query', true); 

        await data.interaction.deferReply();

        try {
            const { track } = await player.play(channel, query, {
                nodeOptions: {
                    metadata: data.interaction 
                }
            });

            return data.interaction.followUp(`**${track.title}** enqueued!`);
        } catch (e) {
            return data.interaction.followUp(`Something went wrong: ${e}`);
        }
    },
};
twlite commented 6 months ago

The error is self explanatory