类型错误:无法读取未定义的属性“执行”;

时间:2021-07-26 13:37:21

标签: javascript node.js discord.js

所以我的 play.js 命令的 main.js 文件有问题。

当我检查控制台时,它说问题就在这里:

client.commands.get('play').execute(message, args);
                                   ^

这是 play.js 命令:

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

module.exports = {
    name: "play",
    description: "Play Komanda",
    async execute(message, args) {
        const voiceChannel = message.member.voice.channel;

        if (!voiceChannel) return message.channel.send("Moraš biti u nekom kanalu kako bi koristio ovu komandu!");
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has("CONNECT")) return message.channel.send("Nemaš permisije!");
        if (!permissions.has("SPEAK")) return message.channel.send("Nemaš permisiju!");
        if (!args.length) return message.channel.send("Moraš poslati drugi argumenat.");

        const validURL = (str) => {
            var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
            if (!regex.test(str)) {
                return false;
            } else {
                return true;
            }
        };

        if (validURL(args[0])) {
            const connection = await voiceChannel.join();
            const stream = ytdl(args[0], { filter: "audioonly" });

            connection.play(stream, { seek: 0, volume: 1 }).on("finish", () => {
                voiceChannel.leave();
                message.channel.send("leaving channel");
            });

            await message.reply(`:musical_note: Trenutno slušaš ***Your Link!***`);

            return;
        }

        const connection = await voiceChannel.join();

        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);

            return videoResult.videos.length > 1 ? videoResult.videos[0] : null;
        };

        const video = await videoFinder(args.join(" "));

        if (video) {
            const stream = ytdl(video.url, { filter: "audioonly" });
            connection.play(stream, { seek: 0, volume: 1 }).on("finish", () => {
                voiceChannel.leave();
            });

            await message.reply(`:musical_note: Trenutno slušaš ***${video.title}***`);
        } else {
            message.channel.send("Nijedan video nije pronadjen");
        }
    },
};

1 个答案:

答案 0 :(得分:0)

我认为您收到此错误是因为您从未设置命令。 只是一个集合,您必须自己填写才能使用 .get()

要解决此问题,您可以尝试类似的操作(例如在您的 index.js 中):

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

for(const file of commandFiles){
    // Set the command equal to the file
    const command = require(`./commands/${file}`);

    // Add the command to the collection
    bot.commands.set(command.name, command);
}

但是...

...这要求您的所有命令都位于名为 commands 的文件夹中的单独文件中(如果您的机器人尚未具有这样的结构)