Reputation: 2025
For my bot in discord, I would like a !help command that loops through all commands, gets the name, and returns them in a message back to the user. I have created fs to loop through my /commands/ folder:
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.extraCommands.set(command.name, command);
}
console.log(client.extraCommands);
Returns a Collection Map that looks like: (cropped for sake of simplicity)
Collection [Map] {
'args-info' => {
name: 'args-info',
execute: [Function: execute]
},
'channel-info' => {
name: 'channel-info',
execute: [Function: execute]
}
All I need is to store the name
of each command into an array.
I've tried looping through to get the key but that doesn't seem to work...
Thanks in advance for any help
Upvotes: 1
Views: 13943
Reputation: 2025
So, at first, I thought I should make a loop to go through all files and get the 'name' attribute. However, the .keys()
returns an array for all properties inside the map
Documentation
Upvotes: 1
Reputation: 89
Both lines of codes will return the same result as you want.
[...client.extraCommands.keys()] //using map keys
[...client.extraCommands.values()].map(({name})=> name); // using key 'name' from each map value
Upvotes: 0
Reputation: 1989
Looks like the key names are your command names, so this should do the trick.
let keys = Array.from(client.extraCommands.keys());
Upvotes: 3