【问题标题】:UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefinedUnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“声音”
【发布时间】:2021-03-03 19:37:07
【问题描述】:

所以我正在尝试创建一个在 vc 中播放音乐的命令,但这是我的问题,每当我运行该命令时,它都会给我这个错误 (node:11) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined 每当我尝试使用此命令执行任何操作时它都不起作用.它只是一遍又一遍地给我这个错误。我还没有找到任何解决方法,但我会继续寻找。如果你们中的任何人发现如何解决这个问题,请回复我的帖子。

代码:

const { Util, voice, Client } = require('discord.js');
const bot = new Client();

module.exports.run = async(message, args) => {
const channel = message.member.voice.channel.join
    if (!channel) return message.channel.send('I\'m sorry but you need to be in a voice channel to play music!');
    const permissions = channel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT')) return message.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
    if (!permissions.has('SPEAK')) return message.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');

    const serverQueue = message.client.queue.get(message.guild.id);
    const songInfo = await ytdl.getInfo(args[0].replace(/<(.+)>/g, '$1'));
    const song = {
        id: songInfo.videoDetails.video_id,
        title: Util.escapeMarkdown(songInfo.videoDetails.title),
        url: songInfo.videoDetails.video_url
    };

    if (serverQueue) {
        serverQueue.songs.push(song);
        console.log(serverQueue.songs);
        return message.channel.send(`✅ **${song.title}** has been added to the queue!`);
    }

    const queueConstruct = {
        textChannel: message.channel,
        voiceChannel: channel,
        connection: null,
        songs: [],
        volume: 2,
        playing: true
    };
    message.client.queue.set(message.guild.id, queueConstruct);
    queueConstruct.songs.push(song);

    const play = async song => {
        const queue = message.client.queue.get(message.guild.id);
        if (!song) {
            queue.voiceChannel.leave();
            message.client.queue.delete(message.guild.id);
            return;
        }

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

    try {
        const connection = await channel.join();
        queueConstruct.connection = connection;
        play(queueConstruct.songs[0]);
    } catch (error) {
        console.error(`I could not join the voice channel: ${error}`);
        message.client.queue.delete(message.guild.id);
        await channel.leave();
        return message.channel.send(`I could not join the voice channel: ${error}`);
    }
}

module.exports.help = {
    name: "play",
    aliases: []
}```

【问题讨论】:

  • message 变量包含什么?

标签: node.js discord discord.js bots


【解决方案1】:
const channel = message.member.voice.channel.join

不存在...

尝试使用:

const channel = message.member.voice.channel

它检查用户是否在任何语音通道中。 如果它不起作用,那么您的 message.member 将返回 undefined。尝试记录 message.member 以查看它是否真的返回 undefined 并找出原因,因为这部分代码应该可以正常工作。

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 2021-04-18
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2021-05-18
    • 2021-01-15
    • 2021-04-09
    相关资源
    最近更新 更多