Получение неопределенного значения на node.js - PullRequest
0 голосов
/ 23 июня 2019

Мне нужно понять, почему я получаю undefined для коммандос. Я определил это раньше, и команда верна.

Я пытаюсь получить мои команды из каталога.

Это бот

Привет.

client.commands = new Discord.Collection();

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

console.log(`Command read:  ${commandFiles}`);

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.on('message', msg => {
    let args = msg.content.slice(prefix.length).split(/ +/); //Slices off the prefix entirely and then splits it into an array by spaces. / +/ to avoid issues with spaces
    const command = args.shift().toLowerCase(); //Variable by calling args.shift(), which will take the first element in array and return it while also removing it from the original array(so that you dont have the command name string inside the args array).

    console.log(command);

    const commando = client.commands.get(command);

   console.log(commando);
})

1 Ответ

0 голосов
/ 23 июня 2019

Предполагая, что ваша коллекция работает как объект Map, вы делаете это:

client.commands.set(command.name, command);

, но затем вы переходите к .get() следующим образом:

const commando = client.commands.get(command);

IЯ думаю, что в этих двух примерах, возможно, command, в котором вы выполняете .get(), отличается от command.name, в котором вы включили .set().

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...