【问题标题】:The Music Bot is playing the Music but No One is able to hear音乐机器人正在播放音乐,但没有人能听到
【发布时间】:2021-10-25 07:51:49
【问题描述】:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const { joinVoiceChannel } = require('@discordjs/voice');
const { getVoiceConnection } = require('@discordjs/voice');
const { createAudioPlayer} = require('@discordjs/voice');
const{createAudioResource} = require('@discordjs/voice');
const{NoSubscriberBehavior } = require('@discordjs/voice');
module.exports = {
    name: 'play',
    description: 'Joins and plays a video from youtube',
    execute: async (client, msg, arg, Discord) => {
        const voiceChannel = msg.member.voice.channel;

        if (!voiceChannel) return msg.channel.send('You need to be in a channel to execute this command!');
        const permissions = voiceChannel.permissionsFor(msg.client.user);
        if (!permissions.has('CONNECT')) return msg.channel.send('You dont have the correct permissins');
        if (!permissions.has('SPEAK')) return msg.channel.send('You dont have the correct permissins');
        if (!arg.length) return msg.channel.send('You need to send the second argument!');

        // const validURL = (str) =>{
        //     var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
        //         return false;
        //     } else {
        //         return true;
        //     }
        // }

        // if(validURL(arg[0])){

        //     const  connection = await voiceChannel.join();
        //     const stream  = ytdl(arg[0], {filter: 'audioonly'});

        //     connection.play(stream, {seek: 0, volume: 1})
        //     .on('finish', () =>{
        //         voiceChannel.leave();
        //         msg.channel.send('leaving channel');
        //     });

        //     await msg.reply(`:thumbsup: Now Playing ***Your Link!***`)

        //     return
        // }


        const connection = joinVoiceChannel({
            channelId: voiceChannel.id,
            guildId: voiceChannel.guild.id,
            adapterCreator: msg.channel.guild.voiceAdapterCreator,
        });

        const connection1 = getVoiceConnection(voiceChannel.guild.id);

        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);

            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;

        }
        const keywords = await arg.join(' ')
        const video = await videoFinder(keywords);

        if (video) {
            const stream = ytdl(video.url, { filter: 'audioonly' });
            

         


            
           
            const subscription = connection1.subscribe(stream,{seek:0,volume:1});

           




            await msg.reply(`:thumbsup: Now Playing ***${video.title}***`)
        }
        else{
            msg.reply('No Results Found')
        }
        
    }
}

现在的问题是消息来了,它正在播放音频,但没有人能听到它 我想可能是由于新的 Discord.js v13 我没有收到任何错误,但有人可以帮助我

同样在 v12 中它工作正常,由于新的 v13,我不得不更改我的代码 我没有从文档中得到任何帮助,但现在我希望来自 Stack

提前致谢!

【问题讨论】:

  • 但是音乐正在播放,但没有广播到语音频道。根据documentation,您还需要Player。您已包含 createAudioResource 但从未使用过它,您需要它来准备音频流。
  • 我对代码做了一些修改,但还是不行你可以参考这个链接srcb.in/6OzLvz4IGp

标签: javascript node.js discord.js


【解决方案1】:

我遇到了类似的问题,但这似乎解决了它。 在您的index.js 文件中,确保在定义您的客户端时使用GUILD_VOICE_STATES 意图。一旦我添加了它,音频就可以正常工作了。

const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES],
});

在遇到一个完全不相关的错误之前,我什至没有考虑过这一点。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 2021-05-28
    • 1970-01-01
    • 2013-11-28
    • 2022-01-21
    • 2019-12-12
    • 2020-03-16
    • 2022-01-04
    • 2019-05-24
    相关资源
    最近更新 更多