类似于我的代码的音乐命令的暂停/恢复?

Posted

技术标签:

【中文标题】类似于我的代码的音乐命令的暂停/恢复?【英文标题】:A pause/resume for my music command similar to my code? 【发布时间】:2020-12-19 04:56:36 【问题描述】:

您好,我正在处理我的音乐命令。我已经有一个 !play、!skip 和 !stop 命令,但我想做它,这样我就可以有一个暂停和恢复命令。任何想法如何做到这一点? 任何形式的帮助都将不胜感激

另外,我对编码很陌生,所以请帮助我逐步完成 这是我的代码

const ytdl = require("ytdl-core");
const yts = require("yt-search");
bot.on("message", async message => 
    if (message.author.bot) return;
    if (!message.content.startsWith(PREFIX)) return;
    if (message.channel.type === 'dm') 
        return message.reply('I can\'t execute that command inside DMs!');
    

    const serverQueue = queue.get(message.guild.id);

    if (message.content.startsWith(`$PREFIXplays`)) 
        execute(message, serverQueue).catch((_err) =>
            message.channel.send('I was not able to fulfill your request')
        )
        return;
     else if (message.content.startsWith(`$PREFIXskips`)) 
        skip(message, serverQueue);
        return;
     else if (message.content.startsWith(`$PREFIXstops`)) 
        stop(message, serverQueue);
        return;
     
);

async function execute(message, serverQueue) 
    const args = message.content.split(" ");

    const voiceChannel = message.member.voice.channel;
    if (!voiceChannel)
        return message.channel.send(
            "You need to be in a voice channel to play music!"
        );
    
    let song;
    if (ytdl.validateURL(args[1])) 
        const songInfo = await ytdl.getInfo(args[1]);
        song = 
            title: songInfo.title,
            url: songInfo.video_url
        ;
     else 
        const  videos  = await yts(args.slice(1).join(" "));
        if (!videos.length) return message.channel.send("No songs were found!");
        song = 
            title: videos[0].title,
            url: videos[0].url
        ;
    

    if (!serverQueue) 
        const queueContruct = 
            textChannel: message.channel,
            voiceChannel: voiceChannel,
            connection: null,
            songs: [],
            volume: 5,
            playing: true
        ;

        queue.set(message.guild.id, queueContruct);

        queueContruct.songs.push(song);

        try 
            var connection = await voiceChannel.join();
            queueContruct.connection = connection;
            play(message.guild, queueContruct.songs[0]);
         catch (err) 
            console.log(err);
            queue.delete(message.guild.id);
            return message.channel.send(err);
        
     else 
        serverQueue.songs.push(song);
        return message.channel.send(`$song.title has been added to the queue!`);
    


function skip(message, serverQueue) 
    if (!message.member.voice.channel)
        return message.channel.send(
            "You have to be in a voice channel to stop the music!"
        );
    if (!serverQueue)
        return message.channel.send("There is no song that I could skip!");
    serverQueue.connection.dispatcher.end();


function stop(message, serverQueue) 
    if (!message.member.voice.channel)
        return message.channel.send(
            "You have to be in a voice channel to stop the music!"
        );
    serverQueue.songs = [];
    serverQueue.connection.dispatcher.end();
    message.channel.send('Succesfully stopped all songs')


function play(guild, song) 
    const serverQueue = queue.get(guild.id);
    if (!song) 
        serverQueue.voiceChannel.leave();
        queue.delete(guild.id);
        return;
    

    const dispatcher = serverQueue.connection
        .play(ytdl(song.url))
        .on("finish", () => 
            serverQueue.songs.shift();
            play(guild, serverQueue.songs[0]);
        )
        .on("error", error => console.error(error));
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
    serverQueue.textChannel.send(`Started playing: **$song.title**`);




bot.on('message', message => 
    if (!message.content.startsWith(PREFIX) || message.author.bot) return;

    const args = message.content.slice(PREFIX.length).split(/ +/);
    const command = args.shift().toLowerCase;

);

async function execute(message, serverQueue) 
    const args = message.content.split(" ");

    const voiceChannel = message.member.voice.channel;
    if (!voiceChannel)
        return message.channel.send(
            "You need to be in a voice channel to play music!"
        );
    const permissions = voiceChannel.permissionsFor(message.bot.user);
    if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) 
        return message.channel.send(
            "I need the permissions to join and speak in your voice channel!"
        );
    
    let song;
    if (ytdl.validateURL(args[1])) 
        const songInfo = await ytdl.getInfo(args[1]);
        song = 
            title: songInfo.title,
            url: songInfo.video_url
        ;
     else 
        const  videos  = await yts(args.slice(1).join(" "));
        if (!videos.length) return message.channel.send("No songs were found!");
        song = 
            title: videos[0].title,
            url: videos[0].url
        ;
    

    if (!serverQueue) 
        const queueContruct = 
            textChannel: message.channel,
            voiceChannel: voiceChannel,
            connection: null,
            songs: [],
            volume: 5,
            playing: true
        ;

        queue.set(message.guild.id, queueContruct);

        queueContruct.songs.push(song);

        try 
            var connection = await voiceChannel.join();
            queueContruct.connection = connection;
            play(message.guild, queueContruct.songs[0]);
         catch (err) 
            console.log(err);
            queue.delete(message.guild.id);
            return message.channel.send(err);
        
     else 
        serverQueue.songs.push(song);
        return message.channel.send(`$song.title has been added to the queue!`);
    


function skip(message, serverQueue) 
    if (!message.member.voice.channel)
        return message.channel.send(
            "You have to be in a voice channel to stop the music!"
        );
    if (!serverQueue)
        return message.channel.send("There is no song that I could skip!");
    serverQueue.connection.dispatcher.end();


function stop(message, serverQueue) 
    if (!message.member.voice.channel)
        return message.channel.send(
            "You have to be in a voice channel to stop the music!"
        );
    serverQueue.songs = [];
    serverQueue.connection.dispatcher.end();


function play(guild, song) 
    const serverQueue = queue.get(guild.id);
    if (!song) 
        serverQueue.voiceChannel.leave();
        queue.delete(guild.id);
        return;
    

    const dispatcher = serverQueue.connection
        .play(ytdl(song.url))
        .on("finish", () => 
            serverQueue.songs.shift();
            play(guild, serverQueue.songs[0]);
        )
        .on("error", error => console.error(error));
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
    serverQueue.textChannel.send(`Start playing: **$song.title**`);

提前谢谢你

【问题讨论】:

【参考方案1】:

你将不得不像这样制作一个 Dispatcher:

let dispatcher = connection.play(song)

在暂停功能中,做

dispatcher.pause()

简历

dispatcher.resume()

【讨论】:

你能把我的答案标记为正确吗?它的小灰色复选标记请大声笑 你知道如何改变音量吗?像 !volume 1 或 @OctagonalT 是的,只要dispatcher.setVolume(number)

以上是关于类似于我的代码的音乐命令的暂停/恢复?的主要内容,如果未能解决你的问题,请参考以下文章

暂停和恢复背景音乐,而其他播放之间

如何在 Inno Setup 中制作停止和暂停/恢复/播放音乐按钮

试图让我的播放按钮与我的暂停按钮和谐地工作

如何停止或暂停 SKAction 播放音乐? - 斯威夫特

播放自己的音频后恢复 Spotify

使用 AVAudioPlayer 播放声音后恢复背景音频