【问题标题】:TypeError: Cannot read property 'execute' of undefined,;TypeError:无法读取未定义的属性“执行”;
【发布时间】:2021-10-02 10:53:38
【问题描述】:

所以我对 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");
        }
    },
};

【问题讨论】:

  • 你能记录这个吗:console.log(client.commands.get('play')) 并检查它是否为空/未定义
  • 另外,你能告诉我们你设置所有命令的代码吗?
  • @Toasty 它说它是未定义的
  • 你还在设置你的命令吗?就像在您的 index.js 或命令处理程序中一样?
  • @Toasty 如果你的意思是这样(prnt.sc/1g66ofj)是的。

标签: javascript node.js discord.js


【解决方案1】:

我认为您收到此错误是因为您从未设置命令。 只是一个集合,您必须自己填写才能使用.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 的文件夹中的单独文件中(如果您的机器人还没有这样的结构)

【讨论】:

  • 这能解决您的问题吗?
猜你喜欢
  • 2021-06-06
  • 2021-07-03
  • 2021-04-30
  • 2021-04-01
  • 2021-01-19
  • 2021-03-31
  • 2021-09-25
  • 2017-10-14
相关资源
最近更新 更多