Discord JS 播放器/播放器流和状态问题

问题描述 投票:0回答:0

我已经在一个不和谐的音乐机器人上工作了一段时间, 到目前为止一直做得很好,但我遇到了一些问题。 在意识到 ytld-core 没有正确地将声音传输到 in-uilt discord 播放器后,我切换到 play-dl 问题是每当我开始播放音频时,播放器的状态都会保持打开状态:正在缓冲 即使我可以通过机器人收听这首歌。 在寻找玩家切换状态的听众时 :

        player.on('stateChange', (oldState, newState) => {
            console.log(oldState.status, newState.status);
        });

它从“缓冲”变为: 歌曲结束后播放(旧状态)到空闲(紧接着)。 即使它实际上在播放声音,问题是我不能使用播放器的暂停、取消暂停和停止方法。

这是代码(我是新手,请不要介意):

const { SlashCommandBuilder, hideLinkEmbed } = require('@discordjs/builders');
const { createAudioPlayer, NoSubscriberBehavior, joinVoiceChannel, createAudioResource, subscribe, VoiceConnectionStatus } = require('@discordjs/voice');
const ConfigJSON = require('../config.json');
const ytdl = require('ytdl-core');
const playdl = require('play-dl');
const ytsr = require('ytsr');
const { EmbedBuilder } = require('discord.js');



module.exports = {
    data: new SlashCommandBuilder()
        .setName('music')
        .setDescription('Play a song in a voice channel')
        .addSubcommand(subcommand =>
            subcommand
                .setName('play')
                .setDescription('Play a song in a voice channel')
                .addStringOption(option =>
                    option.setName('query')
                        .setDescription('The song you want to play')
                        .setRequired(true)))
        .addSubcommand(subcommand =>
            subcommand
                .setName('pause')
                .setDescription('Stop the music and leave the voice channel'))
        .addSubcommand(subcommand =>
            subcommand
                .setName('resume')
                .setDescription('Resume the music in the voice channel'))
        .addSubcommand(subcommand =>
            subcommand
                .setName('stop')
                .setDescription('Stop the music and leave the voice channel')),


    async execute(interaction) {

        const subcommand = interaction.options.getSubcommand();


        const YTapiKey = ConfigJSON.YoutubeAPIKey;

        const player = new createAudioPlayer(
            {
                behaviors: {
                    noSubscriber: NoSubscriberBehavior.Play,
                },
            }
        );

        if (subcommand === 'play') {

            interaction.deferReply();
            const query = interaction.options.getString('query');


            const opts = {
                maxResults: 1,
                key: YTapiKey,

            }



            const queryResult = await ytsr(query);
            const Finalquery = String( await queryResult.items[0].url);
            const streamplay = await playdl.stream(Finalquery);
            const connection = new joinVoiceChannel({
                channelId: interaction.member.voice.channel.id,
                guildId: interaction.guild.id,
                adapterCreator: interaction.guild.voiceAdapterCreator,
                selfDeaf: true,
                selfMute: false,
            });

            connection.subscribe(player);
            const resource = createAudioResource(streamplay.stream, { inputType: streamplay.type });

            let embed = new EmbedBuilder()
                .setTitle(queryResult.items[0].title)
                .setURL(queryResult.items[0].url)
                .setThumbnail(String(queryResult.items[0].bestThumbnail.url))
                .setDescription("Currently playing ....")
                .setFooter({text:"Requested by " + interaction.user.username, iconURL:interaction.user.avatarURL()})
                .setTimestamp()
                .setColor(0x00AE86)

            player.play(resource);
            await interaction.editReply({ embeds: [embed] });
            console
        }

        if (subcommand === 'pause') {
            console.log("pause");
            console.log(player);
            await player.pause();
            player.on('error', (error) => {
                console.log(error);
            });
        }
        if (subcommand === 'resume') {
            console.log("resume");
            console.log(player.state.status);
        }
        if (subcommand === 'stop') {
            console.log("stop");
            console.log(player.state.status);
        }
        player.on('stateChange', (oldState, newState) => {
            console.log(oldState.status, newState.status);
        });
    }   
}

我已经尝试重新组织代码,制作函数,等待一些元素进行适当的流程..但我似乎找不到合适的方法来构建它。

javascript discord.js bots audio-streaming audio-player
© www.soinside.com 2019 - 2024. All rights reserved.