我需要了解为什么我收到突击队的未定义信息。我之前定义过,命令正确。
我正在尝试从目录获取命令。
这是一个机器人
致谢。
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);
})
答案 0 :(得分:0)
假设您的收藏集像Map
对象一样工作,您可以这样做:
client.commands.set(command.name, command);
但是,然后您像这样进入.get()
:
const commando = client.commands.get(command);
我认为在这两个示例中,您可能在command
上进行.get()
的操作与您在command.name
上进行操作的.set()
不同上。