【问题标题】:Discord.js Music bot joins then leavesDiscord.js 音乐机器人加入然后离开
【发布时间】:2021-06-16 00:35:21
【问题描述】:

每当我输入 &play (name) 时,机器人都会加入我所在的 VC,然后它会立即离开。它会开始播放然后离开。我已经安装了 FFmpeg 并且 discord.js 是最新的。我还运行了 npm install @discordjs/opus ffmpeg-static yt-search ytdl-core 所以也安装了它。 代码如下:

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
 
module.exports = {
    name: 'play',
    description: 'Joins and plays a video from youtube',
    async execute(message, args) {
        const voiceChannel = message.member.voice.channel;
 
        if (!voiceChannel) return message.channel.send('You need to be in a channel to execute this command!');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
        if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
        if (!args.length) return message.channel.send('You need to send the second argument!');
 
        const validURL = (str) =>{
            var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
            if(!regex.test(str)){
                return false;
            } else {
                return true;
            }
        }
 
        if(validURL(args[0])){
 
            const  connection = await voiceChannel.join();
            const stream  = ytdl(args[0], {filter: 'audioonly'});
 
            connection.play(stream, {seek: 0, volume: 1})
            .on('finish', () =>{
                voiceChannel.leave();
                message.channel.send('leaving channel');
            });
 
            await message.reply(`:thumbsup: Now Playing ***Your Link!***`)
 
            return
        }
 
        
        const  connection = await voiceChannel.join();
 
        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);
 
            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
 
        }
 
        const video = await videoFinder(args.join(' '));
 
        if(video){
            const stream  = ytdl(video.url, {filter: 'audioonly'});
            connection.play(stream, {seek: 0, volume: 1})
            .on('finish', () =>{
                voiceChannel.leave();
            });
 
            await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
        } else {
            message.channel.send('No video results found');
        }
    }
}

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    尝试将您的 connection.play 选项更改为:

    {seek: 0, volume: 1, type: "opus"}
    

    您需要为 Discord.JS 指定流类型

    【讨论】:

    • 机器人现在留在 vc 中但没有音频,我得到的错误是:events.js:292 throw er; // 未处理的 'error' 事件 ^ TypeError: 传递的压缩数据在 Decoder._decode (C:\Users\socce\Desktop\Projects\Active Projects\HelexGames\Discord Bot\HelexTestBot\node_modules\prism-media\src\作品\Opus.js:64:25)
    • 尝试从ytdl删除过滤器
    • 我已通过再次运行 npm init 修复了该错误。但我没有得到任何音频。如果过滤器被删除,那是我收到错误的时候。
    • 不....不过一切都很好。我会弄清楚,或者根本没有这个功能。
    • @Brayden 您需要在 ytdl 选项中将属性 opusEncoded 设置为 true
    猜你喜欢
    • 2021-07-06
    • 2020-09-22
    • 2021-09-18
    • 2020-05-24
    • 2020-08-04
    • 2020-12-01
    • 2020-04-07
    • 2020-06-27
    • 2021-02-07
    相关资源
    最近更新 更多