[V 14.5.0] using the official DJS guide for a currency system, `getBalance` is giving me grief

hello and thank you to anyone who is willing to read this/help out! as the title says, i am using the official discord.js guide for my bot, and i am working on a currency system at the moment. however, while the bot itself runs just fine, whenever i try to use the /balance command, it throws this error: TypeError: Cannot read properties of undefined (reading 'getBalance') i am following the guide at its core but i have made a few edits and additions to the code that could be causing this error. i will provide the code ASAP if no one already knows what the issue is. i've been scouring the internet and different programming servers for answers and i haven't found a solution that works. it should be noted that this is my first time making a bot but i still have minimal knowledge of js, and this really has me stumped. thank you to anyone willing to help, and my sincere apologies if this has an obvious answer that i'm not seeing! <a:th_rainbowheart:1008940957556805752>
11 Replies
d.js docs
d.js docsā€¢2y ago
ā€¢ What's your exact discord.js npm list discord.js and node node -v version? ā€¢ Post the full error stack trace, not just the top part! ā€¢ Show your code! ā€¢ Explain what exactly your issue is. ā€¢ Not a discord.js issue? Check out #useful-servers.
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Wicked ā† šŸ’Šā…¦āœ„
discord.js@14.5.0 node v18.2.0 yes, one sec!
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('balance')
.setDescription("Check a user's balance!")
.addUserOption(option =>
option.setName('user')
.setDescription('the user whose balance you will check!')
.setRequired(false)),
async execute(interaction) {
const target = interaction.options.getUser('user') ?? interaction.user;
const { currency } = require('../index.js');
await interaction.reply(`${target.tag} has ${currency.getBalance(target.id)} <@&${1022710689279791134}>`);
},
};
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('balance')
.setDescription("Check a user's balance!")
.addUserOption(option =>
option.setName('user')
.setDescription('the user whose balance you will check!')
.setRequired(false)),
async execute(interaction) {
const target = interaction.options.getUser('user') ?? interaction.user;
const { currency } = require('../index.js');
await interaction.reply(`${target.tag} has ${currency.getBalance(target.id)} <@&${1022710689279791134}>`);
},
};
thank you for replying so quickly!
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Wicked ā† šŸ’Šā…¦āœ„
i believe this is a djs issue but i could be wrong. here is the currency log: Collection(0) [Map] {}
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Wicked ā† šŸ’Šā…¦āœ„
gotcha, i've learned something new today :0 let me take a look around and see how i can fix that, i'll message again here if the issue persists. thank you for your time! /g
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Wicked ā† šŸ’Šā…¦āœ„
the bot has restarted a few times because i am still adding commands and such, so i wouldn't be surprised if that was the cause. i'm checking the djs guide to see if i missed what i should add to the collection
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
Wicked ā† šŸ’Šā…¦āœ„
the main difference between my code and the djs guide is that i'm using a command handler, so i've had to edit some parts of the code and move them to different files, but i'm not seeing anything i've actually left out. you're definitely right that i haven't passed in anything to the currency collection, however the guide itself doesn't add anything to it. i'll keep poking around the internet ThonkISSUE FIXED i'm actually not sure exactly what fixed it, but i added const currency = new Collection(); to the top of my index.js and then exported it after the Reflect.defineProperty functions and suddenly it started working?? i would give specific info if i knew what info to give, but that was apparently the catalyst. thank you for your help, Joe! <a:th_rainbowheart:1008940957556805752>
Want results from more Discord servers?
Add your server
More Posts