Discord音乐机器人-跳过歌曲的问题

时间:2020-07-10 07:07:40

标签: node.js discord discord.js

我在跳过歌曲时遇到问题。通过时,!skip bot会冻结,没有任何错误。

这是一个带有两个参数的模块,即msg作为整个消息发送和agrs,这是一个包含传递给字符串的字符串的数组。例如:!play url会将args中的url设为0索引。

传递!skip不会给出任何错误,但是如果我尝试!stop则在!skip之后会给出以下错误:

D:\nodeCode\discord\node_modules\opusscript\build\opusscript_native_wasm.js:8
var Module=typeof Module!=="undefined"?Module:{};var moduleOverrides={};var key;for(key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}Module["arguments"]=[];Module["thisProgram"]="./this.program";Module["quit"]=function(status,toThrow){throw toThrow};Module["preRun"]=[];Module["postRun"]=[];var ENVIRONMENT_IS_WEB=false;var ENVIRONMENT_IS_WORKER=false;var ENVIRONMENT_IS_NODE=false;var ENVIRONMENT_HAS_NODE=false;var ENVIRONMENT_IS_SHELL=false;ENVIRONMENT_IS_WEB=typeof window==="object";ENVIRONMENT_IS_WORKER=typeof importScripts==="function";ENVIRONMENT_HAS_NODE=typeof process==="object"&&typeof process.versions==="object"&&typeof process.versions.node==="string";ENVIRONMENT_IS_NODE=ENVIRONMENT_HAS_NODE&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;var scriptDirectory="";function locateFile(path){if(Module["locateFile"]
abort(TypeError: Cannot read property 'end' of null). Build with -s ASSERTIONS=1 for more info.
npm ERR! code ELIFECYCLE
npm ERR! errno 7
npm ERR! discord@1.0.0 start: `node src/index.js`
npm ERR! Exit status 7
npm ERR!
npm ERR! Failed at the discord@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\jaspr\AppData\Roaming\npm-cache\_logs\2020-07-10T06_44_56_465Z-debug.log
const ytdl = require('ytdl-core');
const queue = new Map();


const play = (guild, song) => {
    const serverQueue = queue.get(guild.id);
    console.log(serverQueue.songs);
    if (!song) {
        serverQueue.voiceChannel.leave();
        queue.delete(guild.id);
        return;
    }
    const dispatcher = serverQueue.connection
        .play(ytdl(song.url, { filter: "audioonly" }))
        .on('end', () => {
            dispatcher = null;
            serverQueue.songs.shift();
            play(guild, serverQueue.songs[0]);
        })
        .on('error', error => console.error(error));
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
    serverQueue.textChannel.send(`playing: **${song.title}**`);
};

const skip = (msg, serverQueue) => {
    if (!serverQueue)
        return msg.channel.send("There is no song to skip");
    serverQueue.connection.dispatcher.end();
}

const stop = (msg, serverQueue) => {
    serverQueue.songs = [];
    serverQueue.connection.dispatcher.end();
    serverQueue.voiceChannel.leave();
    queue.delete(serverQueue.voiceChannel.guild.id);

}

module.exports = async (msg, args) => {
    const serverQueue = queue.get(msg.guild.id);
    const voiceChannel = msg.member.voice.channel;
    if (!voiceChannel) return msg.channel.send(`you need to be in voice channel ${msg.author.username}`);
    const permissions = voiceChannel.permissionsFor(msg.client.user);
    if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
        return msg.channel.send(
            'I need the permissions to join and speak in your voice channel!'
        );
    }
    if (msg.content.toLowerCase() === '!skip') return skip(msg, serverQueue);
    if (msg.content.toLowerCase() === '!stop') return stop(msg, serverQueue);
    const songInfo = await ytdl.getInfo(args[0]);
    const song = {
        title: songInfo.videoDetails.title,
        url: songInfo.videoDetails.video_url
    };



    if (!serverQueue) {
        const queueContract = {
            textChannel: msg.channel,
            voiceChannel: voiceChannel,
            connection: null,
            songs: [],
            volume: 5,
            playing: true,
        };
        queue.set(msg.guild.id, queueContract);
        queueContract.songs.push(song);
        try {
            let connection = await voiceChannel.join();
            queueContract.connection = connection;
            play(msg.guild, queueContract.songs[0]);
        } catch (err) {
            console.log(err);
            queue.delete(msg.guild.id);
            return msg.channel.send('something went wrong');
        }
    } else {
        serverQueue.songs.push(song);
        console.log(serverQueue.songs);
        return msg.channel.send(`${song.title} has been added to the queue!`);
    }
};

0 个答案:

没有答案