【问题标题】:Voice Channel Detection Erorr语音信道检测错误
【发布时间】:2020-09-24 05:10:54
【问题描述】:

当我尝试制作我的 Discord 音乐机器人时,我在 Discord 而非命令行中遇到错误,但它无法判断我是否在语音频道中 这是我的代码

    if (message.author.bot) return;
    if (!message.content.startsWith(prefix)) return;

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

    if (message.content.startsWith(`${prefix}play`)) {
        execute(message, serverQueue);
        return;
    } else if (message.content.startsWith(`${prefix}skip`)) {
        skip(message, serverQueue);
        return;
    } else if (message.content.startsWith(`${prefix}stop`)) {
        stop(message, serverQueue);
        return;
    } else {
        message.channel.send('You need to enter a valid command!')
    }
});

process.on('unhandledRejection', error => console.error('Uncaught Promise Rejection', error));

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

    const voiceChannel = message.member.voiceChannel;
    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!');
    }

    const songInfo = await ytdl.getInfo(args[1]);
    const song = {
        title: songInfo.title,
        url: songInfo.video_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);
        console.log(serverQueue.songs);
        return message.channel.send(`${song.title} has been added to the queue!`);
    }

}

function skip(message, serverQueue) {
    if (!message.member.voiceChannel) 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.voiceChannel) 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.playStream(ytdl(song.url))
        .on('end', () => {
            console.log('Music ended!');
            serverQueue.songs.shift();
            play(guild, serverQueue.songs[0]);
        })
        .on('error', error => {
            console.error(error);
        });
    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}

我在这个网站上找到了它https://medium.com/free-code-camp/how-to-create-a-music-bot-using-discord-js-4436f5f3f0f8

当我启动代码时,它说我不在一个, 任何类型的反馈都将不胜感激!

【问题讨论】:

  • 您使用的是哪个版本的 discord.js?
  • 我在 Visual Studio 中使用 discord.js 版本 12.2.0

标签: discord discord.js


【解决方案1】:

试试这个

如果 (!message.member.voice.channel) return message.reply('你不在语音频道'),

【讨论】:

    【解决方案2】:

    问题在于您使用的是GuildMember.voiceChannel 属性,该属性仅存在于discord.js@v11 中。 v12 对应的是GuildMember.voice.channel,你可以这样使用它:

    // You can use this as before, since it's the same VoiceChannel element
    const voiceChannel = message.member.voice.channel
    

    【讨论】:

    • 但我仍然遇到错误:DeprecationWarning: Unhandled Promise Rejection
    • 仅此并不意味着什么,您应该查看堆栈以找出它的来源,甚至进行一些调试:您的代码中的错误可能不止一个。调试取决于您,在 SO 中您应该只询问特定问题
    • 是的,你能帮我告诉我把处理程序放在哪里吗
    • 你指的是什么处理程序?
    • 这一进程.on('unhandledRejection', error => console.error('Uncaught Promise Rejection', error));
    猜你喜欢
    • 2018-12-29
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2020-09-09
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    相关资源
    最近更新 更多