1

Pretty much, I'm having difficulty being able to find a role in a user's Quick.db inventory. I'd like to do this to: allow them to only buy a role once; allow them to equip an owned role only if it's found in their inventory. Problem is that the user is able to buy a role more than once, and able to equip a role even if it's not owned. I believe the line I'm having trouble with is if(db.has(message.author.id + '.items' + 'hot rod red')) (possibly because I'm searching for a role and not a plain text item?), but it could be something else.

I'm completely lost and run out of ideas at this point, any help is appreciated!

Code for Equipping a Role:

    let user = message.guild.members.cache.get(message.author.id)
    let items = await db.fetch(message.author.id);
    if(items === null) items = "Nothing"
    let author = db.get(`items_${message.guild.id}_${user.id}`)


  if (args[0] == 'red') {
    let rejectEmbed = new Discord.MessageEmbed()
    .setDescription('You do not own this role!');
    
    if(db.has(message.author.id + '.items' + 'hot rod red')){
      if (message.member.roles.cache.some(role => role.name === 'hot rod red')) {
        let embed = new Discord.MessageEmbed().setDescription('You already have this role!');
        return message.channel.send(embed); }
       
    else {
        await message.guild.members.cache.get(user.id).roles.add('733373020491481219');
          let embed = new Discord.MessageEmbed().setDescription(`You now have the ${message.guild.roles.cache.get('733373020491481219')} role!`);
          message.channel.send(embed);
          user.roles.remove(user.roles.highest); 
        } 
      
      } else return message.channel.send(rejectEmbed)            
}


Code for Buy Command:

        let Embed = new Discord.MessageEmbed()
            .setColor("#FFFFFF")
            .setDescription(`> :no_entry_sign:  You need 20,000 credits to purchase ${message.guild.roles.cache.get('733373020491481219')}`);
            if (message.member.roles.cache.some(role => role.name === "level 25") ||(message.member.roles.cache.some(role => role.name === "frequent flyers"))){
     
        if (console.log(db.has(message.author.id, 'hot rod red'))){
        let EmbedError = new Discord.MessageEmbed()     
        .setColor('#FFFFFF')
        .setDescription(`:no_entry_sign: You already own ${message.guild.roles.cache.get('733373020491481219')} !`);
               return message.channel.send(EmbedError)
    
            }else
                if (amount < 20000) return message.channel.send(Embed)
                let Embed3 = new Discord.MessageEmbed()
                .setColor("#FFFFFF")
                .setDescription(`:white_check_mark: You bought ${message.guild.roles.cache.get('733373020491481219')} for 20,000 credits!`);
                message.channel.send(Embed3)
                db.subtract(`money_${message.guild.id}_${user.id}`, 20000)

        db.push(message.author.id, `${message.guild.roles.cache.get('733373020491481219')}`);
        db.fetch(`hot_rod_red${message.guild.id}_${user.id}`);
        db.set(`hot_rod_red_${message.guild.id}_${user.id}`, true)
        
        }else {return message.channel.send('You do not have the required level to buy this role!')}
    }
washingm
  • 87
  • 1
  • 11
  • Just trying to understand the structure of your data. In which key is the role stored? – Arun Kumar Mohan Mar 20 '21 at 02:20
  • @ArunKumarMohan I believe the role is store in the 'items' key, not too sure why 'let author' is there sorry about that – washingm Mar 20 '21 at 02:24
  • Well, the key has to be unique for each user. Can you share the code where you're saving the role to the database? – Arun Kumar Mohan Mar 20 '21 at 02:27
  • @ArunKumarMohan Of course! I got most of the code from a half-built bot on github and went from there, so I am not entirely sure if all of this is needed: ```db.push(message.author.id,`${message.guild.roles.cache.get('733373020491481219')}`);db.fetch(`hot_rod_red${message.guild.id}_${user.id}`); db.set(`hot_rod_red_${message.guild.id}_${user.id}`, true)``` Edit: user is set to message.author – washingm Mar 20 '21 at 02:30
  • Sure thing, It's a bit messy, but hopefully legibile enough @ArunKumarMohan Code should be uploaded now – washingm Mar 20 '21 at 02:32
  • @ArunKumarMohan That pushes the role into the user's inventory, which seems to work, it's just one discord role. Also should I go ahead and remove the db.fetch line? – washingm Mar 20 '21 at 02:41
  • @ArunKumarMohan Got it, If you're referring to the question of where the role is stored, I had understood that the role would be sent to the user's inventory using db.push, but is that not correct? I'm not *too* familiar with quick.db, sorry about that – washingm Mar 20 '21 at 02:54
  • What's the significance of `'hot rod red'`? Is it a single role? Or an item? Btw, `console.log(db.has(message.author.id, 'hot rod red'))` will always return false since console.log returns `undefined`. – Arun Kumar Mohan Mar 20 '21 at 02:57
  • @ArunKumarMohan Ah sorry, yes it's a single discord role. This is what it shows when the user buys the role, and runs the inventory command: https://imgur.com/tBzzHFw – washingm Mar 20 '21 at 03:01
  • Just curious, why are you setting ``hot_rod_red_${message.guild.id}_${user.id}`` to `true`? Can't you use the key `message.author.id` to get the roles and check if it has `'hot rod red'?` – Arun Kumar Mohan Mar 20 '21 at 03:04
  • @ArunKumarMohan To be honest, I'm not sure where that came from, I might've been following a tutorial and just assumed that was the correct formatting. And that's what I've been unsure of, I'm not too sure if it will be able to check for 'hot rod red' since it is a role and not a plain text item – washingm Mar 20 '21 at 03:09
  • Looks like `${message.guild.roles.cache.get('733373020491481219')}` [returns the role mention as a string](https://github.com/discordjs/discord.js/blob/master/src/structures/Role.js#L401) - `'<@733373020491481219'`. This is the value that gets stored in the database. – Arun Kumar Mohan Mar 20 '21 at 03:12
  • @ArunKumarMohan I see, Now that you mention it, I tried console.logging the inventory check, and the console sent back '<@733373020491481219'. – washingm Mar 20 '21 at 03:17

0 Answers0