#I have a database, but it doesn't work for one command?

48 messages · Page 1 of 1 (latest)

magic flame
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } = require('discord.js');
const chalk = require('chalk');
const { Users, CurrencyShop, UserItems } = require ('../../dbObjects')

module.exports = {
    data: new SlashCommandBuilder()
        .setName('referral')
        .setDescription(`Enter a user's referral code to earn free rewards!`)
        .addStringOption(option => option
            .setName(`code`)
            .setDescription(`Enter the code here:`)
            .setRequired(true)),

    async execute(interaction) {

        let today = new Date();
        let date = today.getMonth()+1+`/`+today.getDate()+`/`+today.getFullYear();
        let logTime = today.getHours() + `:` + today.getMinutes() 
        let dateTime = date+' '+logTime;

        const target = interaction.options.getString('code')

        const userDB = Users.findOne({ where: { user_id: interaction.user.id } })
        const referralDB = Users.findOne({ where: { user_id: target } })

        const referEmbed = new EmbedBuilder()
            .setTitle('🙌・REFERRAL CODE')
            .setColor('Green')
            .setDescription(`Success! <@${interaction.user.id}> was referred by <@${target}> \n \n You have received **75,000 Bucks ![Bucks](https://cdn.discordapp.com/emojis/1085444880035237938.webp?size=128 "Bucks")**, and <@${target}> has received **750,000 Bucks ![Bucks](https://cdn.discordapp.com/emojis/1085444880035237938.webp?size=128 "Bucks") **!`)
            .setFooter({ text: `Tip: Get people to use your referral code to earn a free 750,000 Bucks.` })
            
            if (userDB.referred_user === 0  || userDB.referred_user != null) {
                referEmbed.setColor('Red')
                referEmbed.setDescription(`Uh Oh! <@${interaction.user.id}>, you've already used someone's referral code.`)
                referEmbed.setFooter({ text: `You can find your own referral code at the bottom of your CITY profile.` })
                interaction.reply({ embeds: [referEmbed] })
                console.log(chalk.white(`${dateTime} │ `,chalk.green(`${interaction.user.tag} in ${interaction.guild.name} used /referral but they already have a referral.`)));
                return;
            } else if (!referralDB) {
                referEmbed.setColor('Red')
                referEmbed.setDescription(`<@${interaction.user.id}>, that referral code doesn't exist in the CITY database.`)
                referEmbed.setFooter({ text: `Make sure you typed it in correctly.` })
                interaction.reply({ embeds: [referEmbed] })
                console.log(chalk.white(`${dateTime} │ `,chalk.green(`${interaction.user.tag} in ${interaction.guild.name} used /referral but they weren't in the database.`)));
                return;
            } else if (interaction.user.id === target) {
                referEmbed.setColor('Red')
                referEmbed.setDescription(`<@${interaction.user.id}>, you can't redeem your own referral code. Please enter a different one.`)
                referEmbed.setFooter({ text: `Ask a friend to submit your referral code to earn a free 750,000 bucks.` })
                interaction.reply({ embeds: [referEmbed] })
                console.log(chalk.white(`${dateTime} │ `,chalk.green(`${interaction.user.tag} in ${interaction.guild.name} tried using /referral but it was their own code.`)));
                return;
            } else {
                interaction.reply({ embeds: [referEmbed] })
                userDB.referred_user += Number(target)
                userDB.balance_bucks += Number(75000)
                referralDB.invited_users += Number(1)
                referralDB.balance_bucks += Number(750000)
                await userDB.save();
                await referralDB.save();
                console.log(chalk.white(`${dateTime} │ `,chalk.green(`${interaction.user.tag} in ${interaction.guild.name} used /referral successfully.`)));
                return;
            }
    }
}
visual loomBOT
  • 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!
waxen orchid

Hi. How does your save(); function look like ?

magic flame

it works fine for all of my other commands

waxen orchid

Its hard to help without any context

@magic flame Can u show snippet of your User.DB() "function " ?

magic flame
const userDB = Users.findOne({ where: { user_id: interaction.user.id } })```
waxen orchid

are u using prisma in any chance?

magic flame

no, sequelize

waxen orchid

huh, looked like that

Where did u get hat .save() from

waxen orchid
magic flame

idk, that's what it tells you to do in the djs guide

it works for every other command i have, except this one

waxen orchid

Are you using vs code?

magic flame

yea

waxen orchid

can u ctrl + lmb on that userDB.save() ?

waxen orchid

Do you have the right imports?

magic flame

yup

waxen orchid

@magic flame can u console.log(userDB) right before u call it with save()

Just curious whats it all about

magic flame
waxen orchid

can u do await userDB.save().catch((e) => console.log(e).then((c) => console.log(c)

magic flame
waxen orchid

Can u show me some snippet of your code where u used that before

magic flame

daily.js >> gives 15,000 bucks once every 12 hours

const { Client, Interaction, SlashCommandBuilder, EmbedBuilder, } = require('discord.js')
const {CommandCooldown, msToMinutes} = require('discord-command-cooldown');
const chalk = require('chalk');
const { Users, CurrencyShop, UserItems } = require ('../../dbObjects')
const ms = require('ms');
const { Op } = require('sequelize');

const dailyCooldown = new CommandCooldown('daily', ms('43200s'));

module.exports = {
    data: new SlashCommandBuilder()
        .setName('daily')
        .setDescription('Earn free bucks once every 12 hours.'),

    async execute (interaction) {

        const target = interaction.user
        const dailyBucks = 15000

        const targetCooldown = interaction.user.id

        const dailyCooldownUser = await dailyCooldown.getUser(targetCooldown);

        let today = new Date();
        let date = today.getMonth()+1+`/`+today.getDate()+`/`+today.getFullYear();
        let logTime = today.getHours() + `:` + today.getMinutes() 
        let dateTime = date+' '+logTime;
        const userDB = await Users.findOne({ where: { user_id: target.id } })
        const dailyItemPackage = await CurrencyShop.findOne({ where: { name: { [Op.like]: 'item_package' } } });

            const dailyEmbed = new EmbedBuilder()
            .setTitle('💸・DAILY BUCKS')
            .setDescription(`<@${target.id}>, you received **15,000 Bucks** ![Bucks](https://cdn.discordapp.com/emojis/1085444880035237938.webp?size=128 "Bucks") and **x1 Package** ![Package](https://cdn.discordapp.com/emojis/1085349632667824209.webp?size=128 "Package") as your daily reward.`)
            .setColor('Green')
            .setFooter({ text: `Tip:  You can do this once every 12 hours.` })

            const dailyErrorCooldownEmbed = new EmbedBuilder()
            .setTitle('💸・DAILY BUCKS')
            .setColor('Red')
            .setFooter({ text: `Tip:  Go to the currency section in the /help command.` })

            if (dailyCooldownUser) {
                const dailyCooldownTimeLeft = msToMinutes(dailyCooldownUser.msLeft, false);
                dailyErrorCooldownEmbed.setDescription(`<@${interaction.user.id}>, you've already collected your daily. \n \n Please wait **${dailyCooldownTimeLeft.hours + ' hours, ' + dailyCooldownTimeLeft.minutes + ' minutes, ' + dailyCooldownTimeLeft.seconds} seconds** before collecting again.`)
                interaction.reply({ embeds: [dailyErrorCooldownEmbed] })
                console.log(chalk.white(`${dateTime} │ `,chalk.green(`${interaction.user.tag} in ${interaction.guild.name} tried using /daily but has a cooldown.`)));
                return;
            } else
                interaction.reply({ embeds: [dailyEmbed] })
                userDB.balance_bucks += Number(dailyBucks);
                userDB.addItem(dailyItemPackage)
                userDB.daily_count += Number(1)
                await userDB.save();
                await dailyCooldown.addUser(interaction.user.id);
                console.log(chalk.white(`${dateTime} │ `,chalk.green(`${interaction.user.tag} in ${interaction.guild.name} used /daily. they now have ran this command ${userDB.daily_count} times. `)));
                return;
        }
    }
waxen orchid

u sure it actually works everywhere ?

Have u tested it ?

magic flame

yupp 100%

waxen orchid

const userDB = await Users.findOne({ where: { user_id: target.id } })

is in your "good" file

this is in your "wrong file"
const userDB = await Users.findOne({ where: { user_id: interaction.user.id } })

magic flame

some of them just use interaction tho

yeah i know

waxen orchid

have u tried printing the id itself?

magic flame

ill give that a shot ig, don't think it's gonna make a differce tho

magic flame

the stringoption in my referral command only allows to collect ids that are in my database.
(that's what it's supposed to do anyway)

waxen orchid

Well , u the fact that u cant access save() function could mean that u got response from db u dont expect

magic flame

i could try a user option instead?

waxen orchid

thats why i told u to console.log(userDB)

magic flame

i did yeah it's just pending a promise

waxen orchid

pending promise means u never resolved it.

then try console.log(await userDB)

waxen orchid

if that doesnt work u can try

const test = await userDB;
console.log(test);

I cant really help you with sequelize specific issues because i dont work with that , im working with prisma 90% of the time , but lucky us it's pretty similar