I have a command handler that uses a map with commands assigned from user command folders and a general comm folder. User can add their own commands. the map filters using name inside the file which is also how the call them. How do I make a comparison if there is more than one with the same name? Afaik maps can't do this and fear I have to redo it all.
At first I was trying to assign every user it's own map
client.User1.get(command, argument);
client.User2.get(command argument);
was this actually the right way to do it? I am using a .JSON with enabled command names for initial comparison. Below some of the code, as you ccan probably tell i'm not that experienced.
const assignedChannels = client.opts.channels.slice(0);
assignedChannels.unshift('#basiccommands');
const rootForCommands = "./commands";
client.commandCall = new Map();
for (const channelspecificDir of assignedChannels) {
const commandFiles = fs.readdirSync(`${rootForCommands}/${channelspecificDir}`);
for (const file of commandFiles) {
const commandCollection = require(`${rootForCommands}/${channelspecificDir}/${file}`);
//sets a new item in the collection.
//Key of map as command name and the value the function to send message and argument true or false
client.commandCall.set(commandCollection.name, commandCollection);
const currentProfile = JSON.parse(fs.readFileSync(`./userProfiles/${channel}.json`, 'utf8'));
if (!currentProfile.all_commands.includes(commandPre)){
return console.log("Command does not exist");
}
try {
client.commandCall.get(commandPre, argU).execute(channel, argU);
} catch (error) {
console.log('command broke');
console.error(error);
}
My prevoious method was to assign user specific folder and general command folder to a map and at the end of each iteration change the name of the map object. But that does not work for some reason.
Actually, you can use symbols as keys for maps
const map = new Map()
map.set(Symbol(‘the same name’), value1)
map.set(Symbol(‘the same name’), value2)
You can use any values for Map’s keys, even objects and functions
map.set({}, value3)
map.set({}, value4)
In all previous expressions we get different values (links) for Map’s keys.
BTW. Be aware with const some = require(‘some_file_name’)
.
require()
caches a value for a file name. So, if your files may be updated during a run time and you need to read these updates you shouldn’t use ‘require()’. It’s good only for importing modules and static values (e.g. configs)