简体   繁体   中英

Discord.js err: It's deleting the entire array. Quick.db and discord.js

I am extremely new to to JavaScript, so I decided to give myself a challenge by creating a discord bot. What I am trying to do is detect if the user has item called "Rod", if he does, the bot replies with "You already own this item," Otherwise. the bot will let the user purchase the item, When I make it delete the "Temporary" in the array, it deletes the entire array? how do I fix this. If there's any way to fix this and "optimize" it better that'd be amazing.

This is a snippet of the code.

Variables are: const db = require('quick.db'); const { MessageEmbed } = require('discord.js');

db.push(message.author.id, "Temporary") //This is so the .includes() function could work.
    let checkForItems = db.get(message.author.id)
    console.log(checkForItems);


            if(args[0] === "Rod"){

                //See if the user has "Rod" in their inventory
                let a1 = (checkForItems.includes('Rod'));
                console.log(help);

                if(a1 === false){

                    if (money.balance < 15000){
                        message.channel.send(`Insufficent Funds! You have ${money.balance} coins while the item you are trying to buy costs 15000 coins.`)
                    } else {

                        console.log("A User has purchased a Rod.")

                        let items = db.fetch(message.author.id, {items: [] } )

                        message.channel.send("You have bought 1 Rod.");
                        db.push(message.author.id, "Rod")
                    }
                } else {

                    message.channel.send("You already have this item!")

                }


            }
            db.delete(message.author.id, 'Temporary') //This is not working, how do I fix this? Its deleting the entire array.

The method db.delete(key) only accepts one parameter, and that parameter is the key in the database that you want to remove from the database. So when you do db.delete(message.author.id) , what you're actually doing is removing what's saved at the key message.author.id , which of course, is your entire array.

For a better visualization of what's happening, let's assume your database looks something like this in a JSON format:

{
  "userID1": ["Item 1", "Item 2", "Temporary"],
  "userID2": ["Item 6", "Item 9"]
}

Now if you do db.delete(message.author.id) , assuming the author's ID is "userID1", it will remove the entirety of "userID1" from the database. Your data will now look like so:

{
  "userID2": ["Item 6", "Item 9"]
}

Obviously, that is what is causing your entire array to be deleted. While quick.db has a utility method to push items into arrays, it does not have any similarly convenient way of removing items from arrays. You need to do a bit of extra work to do that.

However, based on your code, I am confused as to why you need this "Temporary" string at all. The line let items = db.fetch(message.author.id, {items: [] } ) seems to suggest that the actual structure of your database is something like this:

{
    "userID1": {
        items: ["Item 1", "Item 2", "Item 9"]
    }
}

Based on that structure, you don't need to push "Temporary" at all. In fact, it wouldn't make sense to do so. If I'm understanding your database structure correctly, then this is how I would rewrite your code :

let items = db.get(message.author.id, {items: []}).items;
console.log(items);
if(args[0] === "Rod"){
    //See if the user has "Rod" in their inventory
    let a1 = items.includes('Rod');

    if(!a1){
        if (money.balance < 15000){
            message.channel.send(`Insufficent Funds! You have ${money.balance} coins while the item you are trying to buy costs 15000 coins.`)
        } else {
            console.log("A User has purchased a Rod.")

            //Add "Rod" item to user's "items" array
            items.push("Rod");
            db.set(message.author.id + ".items", items);

            message.channel.send("You have bought 1 Rod.");
        }
    }
    else message.channel.send("You already have this item!");
}

I find this to be a much neater solution. This should work a lot better. However, I still want to answer your original question as to how to delete items from an array using quick.db .

Assume, for this instance, that your database structure looks more like the first one I showed at the top of this answer, such that db.get(message.author.id) returns the array of items the user has. You already know you can add items to the array using db.push() . This is how you would remove a "Temporary" string from the array:

let items = db.get(message.author.id, []);
let index = items.indexOf("Temporary");

//If "Temporary" does not exist in the array, 'index' is -1
//We use Array.splice(index, elementsToRemove) to remove an item from the array
if (index > -1) items.splice(index, 1);

//Now we update the database with the newly modified array
db.set(message.author.id, items);

That should answer your question of how to delete a single element from an array in quick.db , and the code snippet before this one should answer your question of how best to "optimize" your functionality.

If you want, I have written a module called evg.js that adds simple utility methods (such as removing an item from an array) to quick.db. You can find it here . You do not need to directly use my module, but you can look at its code for an idea of how to do certain things with quick.db. The code there has really simplified my use of quick.db as a storage system in some of my previous bots.

Well I had a problem too. so as i understand you, you wanna remove a single value from an quick.db array. so

 let items = db.get(message.author.id, []); items.splice(value, 1); db.set(message.authoe.id, items);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM