The manager will automatically save all data to your database, but if you want to save them manually or create a command to do so, you can use the saveData()
method of the LevelManager. This method saves the data for each guild individually in the database, and it may take some time depending on the number of guilds in the cache. Keep in mind that it returns a Promise, so you can use then
to send a message and ensure that the data has been saved. If an error occurs while saving the data, only data up to a certain point will be saved.
const { LevelManager } = require('discord-xplus')
const manager = new LevelManager({
mongoUri: 'mongodb://localhost:27017/database',
})
manager.saveData().then(() => {
console.log('Data saved!')
})
Do you want to create a method to save data when the bot shuts down?
If you want the bot to save data when it shuts down or crashes, you can use the process.on('uncaughtException', args)
event in Node.js, and inside this event, call the saveData()
method of the LevelManager, and then use process.exit(1)
. This way, the data will be saved before the main process of your bot ends.
const { LevelManager } = require('discord-xplus')
const manager = new LevelManager({
mongoUri: 'mongodb://localhost:27017/database',
})
process.on('uncaughtException', async (args) => {
await manager.saveData().then(() => {
console.log('Data saved!')
process.exit(1)
})
})
The uncaughtException
event is triggered when an unhandled error occurs, such as a syntax error, etc. If you want to learn more about other events like unhandledRejection
, you can refer to the Node.js documentation here.
Loading Data:
When the manager is initialized, it automatically loads the data, which may take some time depending on the amount of data in the database. So make sure to use the managerReady
event to know when the manager is ready to be used.
What is cache?
The cache is an instance of the Discord.Collection()
class that contains data. The cache is used in different classes to store data such as users, ranks, etc.
Make sure not to modify the cache
by assigning a new value to the cache property of the class, as this will break the functionality of the package.