简体   繁体   English

Discord.js - 类型错误:无法读取未定义的属性“声音”。 有人可以告诉我我做错了什么吗?

[英]Discord.js - TypeError: Cannot read property 'voice' of undefined. Can someone tell me what I'm doing wrong?

Here's my play.js file I got this from a Source Bin.这是我从 Source Bin 获得的 play.js 文件。 If someone can help me that would be very appreciated.如果有人可以帮助我,那将不胜感激。

const ytdl = require("ytdl-core");
const ytSearch = require("yt-search");

//Global queue for your bot. Every server will have a key and value pair in this map. { guild.id, queue_constructor{} }
const queue = new Map();

module.exports = {
    name: "play",
    aliases: ["skip", "stop"],
    cooldown: 0,
    description: "Advanced music bot",
    async execute(message, args, cmd, client, Discord) {
        //Checking for the voicechannel and permissions (you can add more permissions if you like).
        const voice_channel = message.member.voice.channel;
        if (!voice_channel) return message.channel.send("You need to be in a channel to execute this command!");
        const permissions = voice_channel.permissionsFor(message.client.user);
        if (!permissions.has("CONNECT")) return message.channel.send("You dont have the correct permissins");
        if (!permissions.has("SPEAK")) return message.channel.send("You dont have the correct permissins");

        //This is our server queue. We are getting this server queue from the global queue.
        const server_queue = queue.get(message.guild.id);

        //If the user has used the play command
        if (cmd === "play") {
            if (!args.length) return message.channel.send("You need to send the second argument!");
            let song = {};

            //If the first argument is a link. Set the song object to have two keys. Title and URl.
            if (ytdl.validateURL(args[0])) {
                const song_info = await ytdl.getInfo(args[0]);
                song = { title: song_info.videoDetails.title, url: song_info.videoDetails.video_url };
            } else {
                //If there was no link, we use keywords to search for a video. Set the song object to have two keys. Title and URl.
                const video_finder = async (query) => {
                    const video_result = await ytSearch(query);
                    return video_result.videos.length > 1 ? video_result.videos[0] : null;
                };

                const video = await video_finder(args.join(" "));
                if (video) {
                    song = { title: video.title, url: video.url };
                } else {
                    message.channel.send("Error finding video.");
                }
            }

            //If the server queue does not exist (which doesn't for the first video queued) then create a constructor to be added to our global queue.
            if (!server_queue) {
                const queue_constructor = {
                    voice_channel: voice_channel,
                    text_channel: message.channel,
                    connection: null,
                    songs: [],
                };

                //Add our key and value pair into the global queue. We then use this to get our server queue.
                queue.set(message.guild.id, queue_constructor);
                queue_constructor.songs.push(song);

                //Establish a connection and play the song with the vide_player function.
                try {
                    const connection = await voice_channel.join();
                    queue_constructor.connection = connection;
                    video_player(message.guild, queue_constructor.songs[0]);
                } catch (err) {
                    queue.delete(message.guild.id);
                    message.channel.send("There was an error connecting!");
                    throw err;
                }
            } else {
                server_queue.songs.push(song);
                return message.channel.send(`👍 **${song.title}** added to queue!`);
            }
        } else if (cmd === "skip") skip_song(message, server_queue);
        else if (cmd === "stop") stop_song(message, server_queue);
    },
};

const video_player = async (guild, song) => {
    const song_queue = queue.get(guild.id);

    //If no song is left in the server queue. Leave the voice channel and delete the key and value pair from the global queue.
    if (!song) {
        song_queue.voice_channel.leave();
        queue.delete(guild.id);
        return;
    }
    const stream = ytdl(song.url, { filter: "audioonly" });
    song_queue.connection.play(stream, { seek: 0, volume: 0.5 }).on("finish", () => {
        song_queue.songs.shift();
        video_player(guild, song_queue.songs[0]);
    });
    await song_queue.text_channel.send(`🎶 Now playing **${song.title}**`);
};

const skip_song = (message, server_queue) => {
    if (!message.member.voice.channel) return message.channel.send("You need to be in a channel to execute this command!");
    if (!server_queue) {
        return message.channel.send(`There are no songs in queue 😔`);
    }
    server_queue.connection.dispatcher.end();
};

const stop_song = (message, server_queue) => {
    if (!message.member.voice.channel) return message.channel.send("You need to be in a channel to execute this command!");
    server_queue.songs = [];
    server_queue.connection.dispatcher.end();
};

The error message:错误信息:

TypeError: Cannot read property 'voice' of undefined
const voice_channel = message.member.voice.channel;
                                        ^     

How I call my execute() :我如何调用我的execute()

module.exports = (Discord, client, message) => {
    const prefix = "-";
    if (!message.content.startsWith(prefix) || message.author.bot) return;

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

    const command = client.commands.get(cmd) || client.commands.find((a) => a.aliases && a.aliases.includes(cmd));

    if (command) command.execute(client, message, args, Discord);
};

Does anyone know what I'm doing wrong?有谁知道我做错了什么?

Message#member can be null if the message was sent in a DM.如果消息是在 DM 中发送的,则消息Message#member可以是null

To fix the error, ensure that the command cannot be used in a DM:要修复此错误,请确保该命令不能在 DM 中使用:

async execute(message, args, cmd, client, Discord) {
    if (!message.guild) return message.channel.send("This command doesn't work in DMs!");
    // rest of code...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord.js | 类型错误:无法读取未定义的属性“声音” - Discord.js | TypeError: Cannot read property 'voice' of undefined 我试图运行命令 Discord.js 但出现错误“TypeError:无法读取未定义的属性'get'” - i m trying to run a command Discord.js but error appear “TypeError: Cannot read property 'get' of undefined” Discord.js - 无法读取未定义的属性“声音” - Discord.js - Cannot read property 'voice' of undefined 我正在尝试在 firebase 数据库上推送数据,但我收到此错误“无法读取属性“推送””,谁能告诉我我做错了什么? - I'm trying to push data on firebase database but I'm getting this error 'Cannot read property "push" ', can anyone tell me what I'm doing wrong? 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript 类型错误:无法读取未定义的属性 'has' // Discord.js - TypeError: Cannot read property 'has' of undefined // Discord.js 遇到 TypeError:无法读取 Discord.JS 中未定义的属性“0” - Encountering TypeError: Cannot read property '0' of undefined in Discord.JS Discord.JS UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“startsWith” - Discord.JS UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined TypeError:无法读取未定义的属性“数据”| discord.js - TypeError: Cannot read property 'data' of undefined | discord.js 类型错误:无法读取未定义的属性“角色”|| Discord.js - TypeError: Cannot read property 'roles' of undefined || Discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM