【问题标题】:How do I make my music bot resume playing?如何让我的音乐机器人恢复播放?
【发布时间】:2021-10-01 03:26:59
【问题描述】:

这是我的 play.js。我不是编码员,所以我不理解大部分代码。它需要进行一些故障排除才能正常工作。

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');

module.exports = {
    name: 'play',
    description: 'plays a song or whatever.',
    async execute(message, args) {
        const voiceChannel = message.member.voice.channel;

        if(!voiceChannel) return message.channel.send('lol u need to be in a voice channel first');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if(!permissions.has('CONNECT')) return message.channel.send('u dont have the correct permissions smh');
        if(!permissions.has('SPEAK')) return message.channel.send('u dont have the correct permissions smh');
        if(!args.length) return message.channel.send('ok, but u need to tell me what u want to play');

        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('ok fine ill leave')
            });

            await message.reply(`ok ill play whatever that is`)

            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: 0.5})
            .on('finish', () =>{
                voiceChannel.leave();
            });

            await message.reply(`ok im now playing ***${video.title}***`)
        } else {
            message.channel.send('lol i cant find the video ur looking for.')
        }
    }
}

这是我的 pause.js。我对这个有一点问题,但我花了一些时间和一些搜索来解决它。

    name: 'pause',
    description: 'makes me pause or whatever',
    async execute(message,args, command, client, Discord){
        const ytdl = require('ytdl-core');
        const stream = ytdl(args[0], {filter: 'audioonly'});
        const voiceChannel = message.member.voice.channel;
        const connection = await voiceChannel.join();
        const streamOptions = {seek:0, volume:1}
        DJ = connection.play(stream, streamOptions)
        DJ.pause();
    }
}

最后,我的 resume.js(它不起作用。)

    name: 'resume',
    description: 'makes me resume or whatever',
    async execute(message,args, command, client, Discord){
        const ytdl = require('ytdl-core');
        const stream = ytdl(args[0], {filter: 'audioonly'});
        const voiceChannel = message.member.voice.channel;
        const connection = await voiceChannel.join();
        const streamOptions = {seek:0, volume:1}
        DJ = connection.play(stream, streamOptions)
        DJ.resume();
    }
}

我观看了有关如何制作音乐机器人的教程。但是,他们没有提到如何制作播放/暂停命令。每当我运行 resume.js 时,它都会显示错误:未找到视频 ID:未定义。这是我完成音乐机器人之前的最后一个命令(到目前为止)如果有人能回答我,将不胜感激。谢谢!

【问题讨论】:

    标签: javascript discord discord.js bots


    【解决方案1】:

    您将需要一个StreamDispatcher 的实例来调用该方法。您可以尝试从消息对象中检索该实例。

    const dispatcher = message.guild.voice.connection.dispatcher;
    dispatcher.pause();
    

    但请注意,某些属性可能是undefined。所以添加安全检查。

    if (!message.guild) return;
    const voiceState = message.guild.voice;
    if (!voiceState || !voiceState.connection) return;
    const dispatcher = voiceState.connection.dispatcher;
    if (!dispatcher) return;
    dispatcher.pause();
    

    您可以对dispatcher.resume() 应用相同的逻辑。

    编辑: 答案与问题的第一个版本相匹配。在我能够回答之前,OP 对他的问题进行了重大更改。 (我也没有看到对问题进行了编辑。)

    【讨论】:

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