1. Access values that aren’t available synchronously
const { bridge } = require('../../../app');
/* export the data attribute, then... */
async execute(interaction) {
await interaction.deferReply();
const question = interaction.options.getString('question');
const reply = await bridge(question);
await interaction.editReply(reply);
}
2. Filter & Map
discord.js
'sCollection()
method is essentially aMap()
with additional utilities
const { Collection } = require('discord.js');
client.commands = new Collection();
Running a
filter()
on./discord/commands/
to return only.js
files
const comPath = path.join(__dirname, 'commands');
const comSubs = fs.readdirSync(comPath);
for (const sub of comSubs) {
const subPath = path
.join(comPath, sub);
const subFiles = fs
.readdirSync(subPath)
.filter(file => file.endsWith('.js'));
/* more stuff */
}
3. Managing project dependencies with `npm`
"dependencies": {
"discord.js": "^14.16.2",
"dotenv": "^16.4.5",
"openai": "^4.61.1"
}
4. Protecting keys & secrets
I like to store my
.env
values in an object. It helps with coherent naming and means I can fold it all out of sight
const dotenv = require('dotenv').config();
const keys = {
discord: {
/* other discord keys */
token: process.env.DISCORD_TOKEN,
},
/* other APIs */
};
/* other stuff */
client.login(keys.discord.token);
- Making sure code isn't replicated in multiple files
Name: Alexander What Went Well: I love that you used not only code snippets, but also screenshots and even config files to explain your progress. Very well detailed and concise Even Better If: I just would like to see a bit of project management progress. But apart from that there is not much to improve, to be honest.