Skip to content

Commit

Permalink
Discord Economy Super v1.7.1
Browse files Browse the repository at this point in the history
**v1.7.1:**

- Major bug fixes!
- Major typings fixes!
- Major JSDoc fixes!
- Improved error handling and debugging.
- Update checker fixes.
- Fixed broken inventory cache issue on startup in MongoDB version!

- **Custom currencies system! 🔥**
- Added a new `customCurrencySet`, `customCurrencyAdd` and `customCurrencySubtract` events so the changes in any custom currencies could be tracked!
- Added a new optional `currency` argument in **all item buying methods** that takes eaither *currency ID*, *name* or its *symbol* so the currency balance will be subtracted instead of core balance. Requires the `subtractOnBuy` option to be enabled.

- Added a `stack()` method for **_inventory items_** that returns the **number of specific item (quantity)** and the **total price** of it in the inventory!
- Added a new `clearDaily`, `clearWork` and `clearWeekly` methods in `CooldownItem` and `Cooldowns` classes to clear the specific cooldowns.

- Added a `save()` method for `Shop-`, `Inventory-` and `History-` **items** that allows you to edit the item's object properties save the edited objext in database!
- Added a `.toString()` method for some classes.

- `Shop-`, `Inventory-` and `History-` **items'** `itemObject` property was changed to `rawObject` so it could make sense in the code 
- Now a warning will be displayed in console if using a dev version in both MongoDB and JSON versions (see the screenshot below).

- Added the missing `buy()` method in `ShopItem` class.
- Added the missing `clear()` method in `Items` class.
- Fixed return values in database operations methods.
  • Loading branch information
shadowplay1 authored Dec 31, 2022
1 parent d21cebb commit f1e8b84
Show file tree
Hide file tree
Showing 32 changed files with 2,513 additions and 513 deletions.
38 changes: 31 additions & 7 deletions mongodb/src/cached/CachedItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const DatabaseManager = require('../managers/DatabaseManager')
const CacheManager = require('../managers/CacheManager')
const defaultUserObject = require('../structures/DefaultUserObject')
const Bank = require('../classes/user/Bank')
const Currency = require('../classes/Currency')

/**
* Cached item class. Used to work with data in Economy cache
Expand All @@ -23,9 +24,9 @@ class CachedItem {
* @param {any[]} constructorParams Array of parameters for `baseConstructor` to pass in.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(baseConstructor, constructorParams, options, database, cacheManager) {
constructor(baseConstructor, constructorParams, options, database, cache) {

/**
* Cache object.
Expand Down Expand Up @@ -63,7 +64,7 @@ class CachedItem {
* Cache Manager.
* @type {CacheManager}
*/
this.cacheManager = cacheManager
this.cacheManager = cache
}

/**
Expand Down Expand Up @@ -115,6 +116,14 @@ class CachedItem {
return economyUser
}

if (this.baseConstructor.name == 'Currency') {
const results = this.cache[id.guildID] || []

return results.map(result =>
new Currency(result.id, id.guildID, this.options, result, this._database, this.cacheManager)
)
}

if (this.baseConstructor.name == 'EconomyGuild') {
if (!result) {
return new EmptyEconomyGuild(
Expand Down Expand Up @@ -175,9 +184,10 @@ class CachedItem {
const constructorName = this.baseConstructor.name

const databaseProperties = {
'ShopItem': 'shop',
'InventoryItem': 'inventory',
'HistoryItem': 'history',
ShopItem: 'shop',
InventoryItem: 'inventory',
HistoryItem: 'history',
Currency: 'currencies'
}

const databaseProperty = databaseProperties[constructorName]
Expand Down Expand Up @@ -288,6 +298,20 @@ class CachedItem {

return Promise.resolve()

case 'Currency':
if (!id.guildID) {
throw new EconomyError(errors.cache.invalidIdentifiers(
constructorName,
['guildID'],
Object.keys(id)
), 'INVALID_CACHING_IDENTIFIERS')
}

const currenciesArray = await this._database.fetch(`${id.guildID}.${databaseProperty}`) || []
this.set(id.guildID, currenciesArray)

return Promise.resolve()

case 'ShopItem':
if (!id.guildID) {
throw new EconomyError(errors.cache.invalidIdentifiers(
Expand All @@ -297,7 +321,7 @@ class CachedItem {
), 'INVALID_CACHING_IDENTIFIERS')
}

const shopArray = await this._database.fetch(`${id.guildID}.${databaseProperty}`)
const shopArray = await this._database.fetch(`${id.guildID}.${databaseProperty}`) || []
this.set(id.guildID, shopArray)

return Promise.resolve()
Expand Down
77 changes: 53 additions & 24 deletions mongodb/src/cached/CachedItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,148 +7,176 @@ const CooldownItem = require('../classes/CooldownItem')

const BalanceItem = require('../classes/BalanceItem')
const BankBalanceItem = require('../classes/BankBalanceItem')
const Currency = require('../classes/Currency')

const ShopItem = require('../classes/ShopItem')
const InventoryItem = require('../classes/InventoryItem')
const HistoryItem = require('../classes/HistoryItem')


/**
* Cached guilds class. Allows to get the Economy guilds from the cache.
* Cached guilds class. Allows to get the cached Economy guilds.
* @extends {CachedItem}
*/
class CachedGuilds extends CachedItem {

/**
* Cached guilds class. Allows to get Economy guilds from the cache.
* Cached guilds class. Allows to get cached Economy guilds.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(guildID, options, database, cache) {
super(EconomyGuild, [guildID], options, database, cache)
}
}

/**
* Cached user class. Allows to get Economy users from the cache.
* Cached user class. Allows to get cached Economy users.
* @extends {CachedItem}
*/
class CachedUsers extends CachedItem {

/**
* Cached guilds class. Allows to get Economy guilds from the cache.
* Cached guilds class. Allows to get cached Economy guilds.
* @param {string} userID User ID.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(userID, guildID, options, database, cache) {
super(EconomyUser, [userID, guildID], options, database, cache)
}
}

/**
* Cached cooldowns class. Allows to get cooldowns from the cache.
* Cached cooldowns class. Allows to get cached cooldowns.
* @extends {CachedItem}
*/
class CachedCooldowns extends CachedItem {

/**
* Cached cooldowns class. Allows to get cooldowns from the cache.
* Cached cooldowns class. Allows to get cached cooldowns.
* @param {string} userID User ID.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(userID, guildID, options, database, cache) {
super(CooldownItem, [userID, guildID], options, database, cache)
}
}

/**
* Cached balance class. Allows to get balance from the cache.
* Cached balance class. Allows to get cached balance.
* @extends {CachedItem}
*/
class CachedBalance extends CachedItem {

/**
* Cached balance class. Allows to get balance from the cache.
* Cached balance class. Allows to get cached balance.
* @param {string} userID User ID.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(userID, guildID, options, database, cache) {
super(BalanceItem, [userID, guildID], options, database, cache)
}
}

/**
* Cached bank balance class. Allows to get bank balance from the cache.
* Cached bank balance class. Allows to get cached bank balance.
* @extends {CachedItem}
*/
class CachedBank extends CachedItem {

/**
* Cached bank balance class. Allows to get bank balance from the cache.
* Cached bank balance class. Allows to get cached bank balance.
* @param {string} userID User ID.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(userID, guildID, options, database, cache) {
super(BankBalanceItem, [userID, guildID], options, database, cache)
}
}

/**
* Cached shop class. Allows to get shop from the cache.
* Cached currency class. Allows to manage the cached guild currencies.
* @extends {CachedItem}
*/
class CachedCurrency extends CachedItem {

/**
* Cached currency class. Allows to manage the cached guild currencies.
* @param {number} currencyID Currency ID.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(currencyID, guildID, options, database, cache) {
super(Currency, [currencyID, guildID], options, database, cache)
}
}

/**
* Cached shop class. Allows to get the cached guildshop.
* @extends {CachedItem}
*/
class CachedShop extends CachedItem {

/**
* Cached shop class. Allows to get shop from the cache.
* Cached shop class. Allows to get the cached guild shop.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(guildID, options, database, cache) {
super(ShopItem, [guildID], options, database, cache)
}
}

/**
* Cached inventory class. Allows to get inventory from the cache.
* Cached inventory class. Allows to get the cached user inventory.
* @extends {CachedItem}
*/
class CachedInventory extends CachedItem {

/**
* Cached inventory class. Allows to get inventory from the cache.
* Cached inventory class. Allows to get the cached user inventory.
* @param {string} userID User ID.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(userID, guildID, options, database, cache) {
super(InventoryItem, [guildID, userID], options, database, cache)
}
}

/**
* Cached history class. Allows to get history from the cache.
* Cached history class. Allows to get the cached user purchases history.
* @extends {CachedItem}
*/
class CachedHistory extends CachedItem {

/**
* Cached history class. Allows to get history from the cache.
* Cached history class. Allows to get the cached user purchases history.
* @param {string} userID User ID.
* @param {string} guildID Guild ID.
* @param {EconomyConfiguration} options Economy configuration object.
* @param {DatabaseManager} database Database manager instance.
* @param {CacheManager} cacheManager Cache manager instance.
* @param {CacheManager} cache Cache manager instance.
*/
constructor(userID, guildID, options, database, cache) {
super(HistoryItem, [guildID, userID], options, database, cache)
Expand All @@ -161,6 +189,7 @@ module.exports = {
CachedCooldowns,
CachedBalance,
CachedBank,
CachedCurrency,
CachedShop,
CachedInventory,
CachedHistory
Expand Down
2 changes: 1 addition & 1 deletion mongodb/src/classes/BalanceItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BalanceItem {
* @param {BalanceObject} balanceObject User balance object.
* @param {DatabaseManager} database Database manager.
*/
constructor(guildID, memberID, ecoOptions, balanceObject, database) {
constructor(guildID, memberID, ecoOptions, balanceObject, database, cache) {

/**
* Member ID.
Expand Down
36 changes: 35 additions & 1 deletion mongodb/src/classes/CooldownItem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const DatabaseManager = require('../managers/DatabaseManager')
const CacheManager = require('../managers/CacheManager')

const CooldownManager = require('../managers/CooldownManager')


/**
* Cooldown item class.
Expand All @@ -13,8 +16,9 @@ class CooldownItem {
* @param {EconomyConfiguration} ecoOptions Economy configuration.
* @param {CooldownsObject} cooldownsObject User cooldowns object.
* @param {DatabaseManager} database Database manager.
* @param {CacheManager} cache Cache manager.
*/
constructor(guildID, memberID, ecoOptions, cooldownsObject, database) {
constructor(guildID, memberID, ecoOptions, cooldownsObject, database, cache) {

/**
* Member ID.
Expand All @@ -28,6 +32,12 @@ class CooldownItem {
*/
this.guildID = guildID

/**
* Cooldown Manager.
* @type {CooldownManager}
* @private
*/
this.cooldowns = new CooldownManager(ecoOptions, database, cache)

/**
* Daily cooldown.
Expand All @@ -47,6 +57,30 @@ class CooldownItem {
*/
this.weekly = cooldownsObject.weekly || 0
}

/**
* Clears user's daily cooldown.
* @returns {Promise<boolean>} If cleared: true; else: false
*/
clearDaily() {
return this.cooldowns.clearDaily(this.guildID, this.memberID)
}

/**
* Clears user's work cooldown.
* @returns {Promise<boolean>} If cleared: true; else: false
*/
clearWork() {
return this.cooldowns.clearWork(this.guildID, this.memberID)
}

/**
* Clears user's weekly cooldown.
* @returns {Promise<boolean>} If cleared: true; else: false
*/
clearWeekly() {
return this.cooldowns.clearWeekly(this.guildID, this.memberID)
}
}


Expand Down
Loading

0 comments on commit f1e8b84

Please sign in to comment.