What's the preferred way of accessing collections in a command file?

Currently I have something like this
try {
if (interaction.commandName === 'user') {
await command.execute(interaction, currency) // Currency is a Collection of user ids and balances
} else {
await command.execute(interaction)
}
} catch (error) {
console.error(error)
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true })
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true })
}
}
try {
if (interaction.commandName === 'user') {
await command.execute(interaction, currency) // Currency is a Collection of user ids and balances
} else {
await command.execute(interaction)
}
} catch (error) {
console.error(error)
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true })
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true })
}
}
Where I just send the collections along when I execute the command from the main file, depending on the command but this feels a bit hacky, as does having a separate file exporting collections. What should I be doing? Thanks! npm list discord.js: 14.14.1 node -v: v21.1.0
11 Replies
d.js toolkit
d.js toolkit5mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
dank
dank5mo ago
get your collection within the command module?
ras
ras5mo ago
Yeah, if like if I was following the guide for making a currency system, except I wanted my commands to broken down into individual files. How would I access the collections defined to modify them from within that command file. Or is that not what you're asking?
import { SlashCommandBuilder } from 'discord.js'

export default {
data: new SlashCommandBuilder()
.setName('user')
.setDescription('Provides information about the user.'),
async execute(interaction, currency) {
const user = currency.get(interaction.member.id)
await interaction.reply(`${interaction.user.username} has ${user.balance}`)
}
}
import { SlashCommandBuilder } from 'discord.js'

export default {
data: new SlashCommandBuilder()
.setName('user')
.setDescription('Provides information about the user.'),
async execute(interaction, currency) {
const user = currency.get(interaction.member.id)
await interaction.reply(`${interaction.user.username} has ${user.balance}`)
}
}
I suppose I could also define it on the client object like the commands collection but I'm not sure what the right hing to do here is.
dank
dank5mo ago
How is your collection defined
ras
ras5mo ago
just
const currency = new Collection()
const currency = new Collection()
in the main file where that try catch block from the top is
dank
dank5mo ago
So it doesn’t persist between restarts, is that not what you want for a currency statement System **
ras
ras5mo ago
I have an sqlite database for that
dank
dank5mo ago
I see, I mean the better approach would be to always pass uniform arguments to your interactions instead of hard coding the same function but executing different arguments In this case attaching it to client would be best or you b can get access to the collection (or the date of the database) within your indiv command handling
ras
ras5mo ago
Okay, thanks! I'll attach it to the client, that makes sense to me. Would accessing the collection within the command handling just be an import statement, or?
dank
dank5mo ago
interaction.client.currency
ras
ras5mo ago
right, that's if i attach it to the client, I meant when you said "or you b can get access to the collection (or the date of the database) within your indiv command handling". Sorry for the confusion