为什么我的 discord.js 音乐机器人在歌曲结束前停止?

时间:2021-02-12 21:45:27

标签: javascript discord discord.js bots

我的音乐机器人工作正常,除了歌曲之外的所有内容在结束前 10-15 秒停止播放。我的代码工作正常,但这是我唯一的问题,我没有收到任何错误。

这是我的代码:

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 voice channel to use this smh');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins lol');
        if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins lol');
        if (!args.length) return message.channel.send('Put a song name lol');
        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(await ytdl(url, {
                filter: format => ['251'],
                highWaterMark: 1 << 25
            }), {
                type: 'opus'
            })
            .on('finish', () =>{
                voiceChannel.leave();
                message.channel.send('leaving channel');
            });
 
            await message.reply(`<:pepefingergun:759099199949373511> 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();
                message.channel.send('leaving channel');
            });
 
            await message.reply(`<:pepefingergun:759099199949373511> Now Playing ***${video.title}***`)
        } else {
            message.channel.send('No video results found');
        }
    }
}

0 个答案:

没有答案