【问题标题】:The discord.js/voice Library isn't workingdiscord.js/voice 库不工作
【发布时间】:2021-11-02 10:48:25
【问题描述】:
const ytSearch = require('yt-search');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice');
const player = createAudioPlayer()
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!');

        

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

       
        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 player = createAudioPlayer()


            var resource = createAudioResource(stream)
            player.play(resource)
      

           connection.subscribe(player);

       
         


            
           
            

           




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

过去几个月我一直在寻找解决方案 甚至尝试了每个平台 并去了支持服务器,但一无所获

代码执行没有任何错误 机器人加入 VC 还会发送一条消息,播放您的歌曲

但在 VC 中,机器人不会说话

所以有人可以帮助我 解决代码或修复我需要的东西 注意:我已经安装了每个必需的依赖项

【问题讨论】:

  • 请在您的问题client.on("debug", e => console.log(e)) 中添加调试日志,以便我们查看它是否真的在发送流位。
  • 这对我也不起作用,在最新版本的 discord.js (13.x) 上。降级到 12.x 版的工作符合我的预期。

标签: javascript node.js discord discord.js voice


【解决方案1】:

试试这个:

const ytSearch = require('yt-search');
const voice = require('@discordjs/voice');
const ytdl = require('ytdl-core');
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 connection = voice.joinVoiceChannel({
            channelId: voiceChannel.id,
            guildId: voiceChannel.guild.id,
            adapterCreator: msg.channel.guild.voiceAdapterCreator,
        });
        try{
            await voice.entersState(connection, voice.VoiceConnectionStatus.Ready, 30e3);
        } catch(error) {
            connection.destroy();
            return console.error(error);
        }

    
        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 videoinfo = await ytdl.getInfo(video.url);
            try{
                var stream = await ytdl.downloadFromInfo(videoinfo);
            } catch (error){
                msg.reply(`Error while creating stream`);
                return console.error(error);
            }
            
            const player = voice.createAudioPlayer()


            const resource = voice.createAudioResource(stream, {
                inputType: voice.StreamType.Arbitrary,
                inlineVolume: true
            });

            player.play(resource);
            voice.entersState(player, voice.AudioPlayerStatus.Playing, 5e3);
    

            connection.subscribe(player);

            player.on('subscribe', async () => {
                await msg.reply(`:thumbsup: Now Playing ***${video.title}***`);
            });
        } else{
            msg.reply('No Results Found')
        }
        
    }
}

【讨论】:

  • @AryanGagare 您是否在 Discord.js 客户端中启用了“GUILD_VOICE_STATES”意图?那是我一直忘记的事情。
  • 我已将意图设为 32767
  • @AryanGagare 您需要在客户端中指定您的意图。在索引中,它看起来像 new Discord.Client({intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES, Discord.Intents.FLAGS.GUILD_VOICE_STATES]})
猜你喜欢
  • 2023-01-18
  • 2018-05-15
  • 2017-09-12
  • 2020-08-29
  • 2018-02-05
  • 1970-01-01
  • 2021-12-04
  • 2021-11-11
  • 2021-11-21
相关资源
最近更新 更多