-
botname
botfilepath
-
botfile
botfunction
-
bots
const fs = require( "fs" ),
path = require( "path" ),
botsDir = path.join( __dirname, "bots" );
/** One time read to fetch all the bot files in bots dir */
const files = fs.readdirSync( botsDir ),
/** Here we create the map of bot and bot file path */
availableBots = files
.reduce( ( botMap, file ) => Object.assign( botMap, {
[ file.replace(".js", "") ] : path.join( botsDir, file )
} ), {} );
// Your code
const botThatWillBeJoiningAtRuntime = "BotC"; // This would be some calculation to determine bot that joins at runtime.
/** Here we safely require the bot file only if we have one corresponding to the bot that is joining */
if ( availableBots[botThatWillBeJoiningAtRuntime] ) {
const botFunc = require( availableBots[botThatWillBeJoiningAtRuntime] );
}
if