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 **, and <@${target}> has received **750,000 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;
}
}
}
#I have a database, but it doesn't work for one command?
48 messages · Page 1 of 1 (latest)
- 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!
Hi. How does your save(); function look like ?
it works fine for all of my other commands
Its hard to help without any context
@magic flame Can u show snippet of your User.DB() "function " ?
const userDB = Users.findOne({ where: { user_id: interaction.user.id } })```
are u using prisma in any chance?
no, sequelize
huh, looked like that
Where did u get hat .save() from
.
idk, that's what it tells you to do in the djs guide
it works for every other command i have, except this one
Are you using vs code?
yea
can u ctrl + lmb on that userDB.save() ?
brings me to this
Do you have the right imports?
yup
@magic flame can u console.log(userDB) right before u call it with save()
Just curious whats it all about
can u do await userDB.save().catch((e) => console.log(e).then((c) => console.log(c)
Can u show me some snippet of your code where u used that before
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**  and **x1 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;
}
}
u sure it actually works everywhere ?
Have u tested it ?
yupp 100%
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 } })
some of them just use interaction tho
yeah i know
have u tried printing the id itself?
ill give that a shot ig, don't think it's gonna make a differce tho
wdym
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)
Well , u the fact that u cant access save() function could mean that u got response from db u dont expect
i could try a user option instead?
thats why i told u to console.log(userDB)
i did yeah it's just pending a promise
pending promise means u never resolved it.
then try console.log(await userDB)
u actually have to resolve it
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