typeerror:无法读取未定义的属性“execute”,;

oipij1gg  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(425)

因此,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");
        }
    },
};
nfg76nw0

nfg76nw01#

我认为您会出现此错误,因为您从未设置命令。这只是一个你必须自己填写才能使用的集合 .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 (如果你的机器人还没有这样构造)

相关问题