简体   繁体   中英

Losing data from Quick.db in my Discord Bot

I have a bot game in Discord, so i've used 'express' library to set the bot online (I guess). First of all to be clear, I'm not a dev pro, only a person who learned watching tutorials in YT. So I've created a bot game, but sometimes, my bot get offline suddenly and the users from my game lost Items, Cash and stuff like that in the game. I noticed that is like the database lost time and get back to the database from minutes ago. Someone know what could be doing that? And how I can fix?

An simple exemple of command code I've use to make my database with quick.db

let money = await db.fetch(`money_${user.id}`)
let amount = 300

if(message.content === (`${PREFIX}daily`)) {
  db.add(`money_${user.id}`, amount)
  message.channel.send(`${username} received your daily reward ${amount} money!`)
}

PS: I've use Repl.it to make my codes, if that matters. Thanks.

If you are using heroku or repl.it or others like it they tend to wipe local data daily you can use mongodb or json's in your case or switch your host

Use Repl.it Database for persistence! https://docs.repl.it/tutorials/11-using-the-replit-database

you can try QuickMongo . It is basically quick.db for mongodb

Test

QuickMongo

const { Database } = require("quickmongo");
const db = new Database("mongodb://localhost/quickmongo");

db.once("ready", () => {
    // Setting an object in the database:
    db.set("userInfo", { difficulty: "Easy" }).then(console.log);
    // -> { difficulty: 'Easy' }

    db.push("userInfo.items", "Sword").then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword'] }

    db.add("userInfo.balance", 500).then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword'], balance: 500 }

    // Repeating previous examples:
    db.push("userInfo.items", "Watch").then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 500 }

    db.add("userInfo.balance", 500).then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 1000 }

    // Fetching individual properties
    db.get("userInfo.balance").then(console.log);
    // -> 1000
    db.get("userInfo.items").then(console.log);
    // -> ['Sword', 'Watch']
})

QuickDB

const db = require('quick.db');

// Setting an object in the database:
db.set('userInfo', { difficulty: 'Easy' })
// -> { difficulty: 'Easy' }

// Pushing an element to an array (that doesn't exist yet) in an object:
db.push('userInfo.items', 'Sword')
// -> { difficulty: 'Easy', items: ['Sword'] }

// Adding to a number (that doesn't exist yet) in an object:
db.add('userInfo.balance', 500)
// -> { difficulty: 'Easy', items: ['Sword'], balance: 500 }

// Repeating previous examples:
db.push('userInfo.items', 'Watch')
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 500 }
db.add('userInfo.balance', 500)
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 1000 }

// Fetching individual properties
db.get('userInfo.balance') // -> 1000
db.get('userInfo.items') // ['Sword', 'Watch']

replit automatically deletes 'quickdb' files after a day or two use 'quickmongo' better and faster

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