首页 > 解决方案 > TypeError:无法读取未定义的属性'get',如何?

问题描述

run: async (client, message, args) => {
    const { channel } = message.member.voice;

    const permissions = channel.permissionsFor(message.client.user);
    if (!permissions.has("CONNECT"))
        return message.reply("Je n'est pas les permissions de rejoindre votre channel.");
    if (!permissions.has("SPEAK"))
        return message.reply("Je ne peux pas parler dans ce channel vocal.");

    const search = args.join(" ");
    const videoPattern = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/gi;
    const playlistPattern = /^.*(list=)([^#\&\?]*).*/gi;
    const url = args[0];
    const urlValid = videoPattern.test(args[0]);

    const serverQueue = message.client.queue.get(message.guild.id);
                          ^^^^^ÊRROR HERE^^^^^^
    const queueConstruct = {
        textChannel: message.channel,
        channel,
        connection: null,
        songs: [],
        loop: false,
        volume: 50,
        playing: true
    };

    let songInfo = null;
    let song = null;

    if (urlValid) {
        try {
            songInfo = await ytdl.getInfo(url);
            song = {
                title: songInfo.title,
                url: songInfo.video_url,
                duration: songInfo.length_seconds,
            };
        } catch (error) {
            if (error.message.includes("copyright")) {
                return message
                    .reply("⛔ Cette vidéo est copyright, elle ne peut donc être jouée. ⛔")
                    .catch(console.error);
            } else {
                console.error(error);
            }
        }
    } else {
        try {
            const results = await youtube.searchVideos(search, 1);
            songInfo = await ytdl.getInfo(results[0].url);
            song = {
                title: songInfo.title,
                url: songInfo.video_url,
                duration: songInfo.length_seconds,
            };
        } catch (error) {
            console.error(error);
        }
    }

    if (serverQueue) {
        serverQueue.songs.push(song);
        return serverQueue.textChannel
            .send(`✅ **${song.title}** a été ajoutée a la queue par ${message.author}`)
            .catch(console.error);
    } else {
        queueConstruct.songs.push(song);
    }

    if (!serverQueue) message.client.queue.set(message.guild.id, queueConstruct);

当代码正常启动时,它应该播放一些音乐,但是当我运行代码时它没有按预期运行,它告诉我 get 没有定义......你能帮帮我吗?

我不明白为什么在它起作用之前就知道它为什么会这样做......如果你帮助我,它将非常有用!没有所有的代码,因为否则网站会问我更多的细节对不起......

标签: node.jsdiscord.js

解决方案


每当您想访问对象中不存在的属性时,就会出现此错误。你确定这个queue属性的存在message.client吗?根据discord.js doc,客户端对象中没有任何queue可以通过消息对象访问的属性。为保证记录,message.client以通过该对象查找可用属性。


推荐阅读