Skip to content

Commit

Permalink
chore(errors): using the method to generate error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowplay1 committed Mar 19, 2023
1 parent d038888 commit a4b34b5
Show file tree
Hide file tree
Showing 37 changed files with 295 additions and 335 deletions.
6 changes: 3 additions & 3 deletions examples/json/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ client.on('messageCreate', async message => {
if (command == prefix + 'daily') {
const dailyResult = user.rewards.getDaily()

if (dailyResult.cooldown) {
if (!dailyResult.claimed) {
const cooldownTime = dailyResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -303,7 +303,7 @@ client.on('messageCreate', async message => {
if (command == prefix + 'work') {
const workResult = user.rewards.getWork()

if (workResult.cooldown) {
if (!workResult.claimed) {
const cooldownTime = workResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -332,7 +332,7 @@ client.on('messageCreate', async message => {
if (command == prefix + 'weekly') {
const weeklyResult = user.rewards.getWeekly()

if (weeklyResult.cooldown) {
if (!weeklyResult.claimed) {
const cooldownTime = weeklyResult.cooldown.time

const cooldownTimeString =
Expand Down
6 changes: 3 additions & 3 deletions examples/json/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ client.on('messageCreate', async msg => {
if (command == prefix + 'daily') {
const dailyResult = user.rewards.getDaily<false>()

if (dailyResult.cooldown) {
if (!dailyResult.claimed) {
const cooldownTime = dailyResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -304,7 +304,7 @@ client.on('messageCreate', async msg => {
if (command == prefix + 'work') {
const workResult = user.rewards.getWork<true>()

if (workResult.cooldown) {
if (!workResult.claimed) {
const cooldownTime = workResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -333,7 +333,7 @@ client.on('messageCreate', async msg => {
if (command == prefix + 'weekly') {
const weeklyResult = user.rewards.getWeekly<true>()

if (weeklyResult.cooldown) {
if (!weeklyResult.claimed) {
const cooldownTime = weeklyResult.cooldown.time

const cooldownTimeString =
Expand Down
6 changes: 3 additions & 3 deletions examples/mongodb/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ client.on('messageCreate', async message => {
if (command == prefix + 'daily') {
const dailyResult = await user.rewards.getDaily()

if (dailyResult.cooldown) {
if (!dailyResult.claimed) {
const cooldownTime = dailyResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -346,7 +346,7 @@ client.on('messageCreate', async message => {
if (command == prefix + 'work') {
const workResult = await user.rewards.getWork()

if (workResult.cooldown) {
if (!workResult.claimed) {
const cooldownTime = workResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -375,7 +375,7 @@ client.on('messageCreate', async message => {
if (command == prefix + 'weekly') {
const weeklyResult = await user.rewards.getWeekly()

if (weeklyResult.cooldown) {
if (!weeklyResult.claimed) {
const cooldownTime = weeklyResult.cooldown.time

const cooldownTimeString =
Expand Down
6 changes: 3 additions & 3 deletions examples/mongodb/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ client.on('messageCreate', async msg => {
if (command == prefix + 'daily') {
const dailyResult = await user.rewards.getDaily<false>()

if (dailyResult.cooldown) {
if (!dailyResult.claimed) {
const cooldownTime = dailyResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -347,7 +347,7 @@ client.on('messageCreate', async msg => {
if (command == prefix + 'work') {
const workResult = await user.rewards.getWork<true>()

if (workResult.cooldown) {
if (!workResult.claimed) {
const cooldownTime = workResult.cooldown.time

const cooldownTimeString =
Expand Down Expand Up @@ -376,7 +376,7 @@ client.on('messageCreate', async msg => {
if (command == prefix + 'weekly') {
const weeklyResult = await user.rewards.getWeekly<true>()

if (weeklyResult.cooldown) {
if (!weeklyResult.claimed) {
const cooldownTime = weeklyResult.cooldown.time

const cooldownTimeString =
Expand Down
1 change: 1 addition & 0 deletions mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"precommit": "bash ./.husky/scripts/commit-to-public.sh",
"docs:deploy": "bash ./.husky/scripts/deploy-docs.sh",
"deploy": "bash ./.husky/scripts/commit-and-deploy.sh",
"upload": "bash ./publish.sh && bash ./.husky/scripts/deploy-docs.sh",
"lint": "eslint ./",
"lint:fix": "eslint ./ --fix"
},
Expand Down
17 changes: 0 additions & 17 deletions mongodb/src/classes/HistoryItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const EconomyError = require('../classes/util/EconomyError')
const errors = require('../structures/errors')


/**
* History item class.
*/
Expand Down Expand Up @@ -113,19 +109,6 @@ class HistoryItem {
*/
async remove() {
const id = this.id

if (typeof id !== 'number' && typeof id !== 'string') {
throw new EconomyError(errors.invalidTypes.id + typeof id, 'INVALID_TYPE')
}

if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
}

const history = (await this._database.fetch(`${this.guildID}.${this.memberID}.history`)) || []

const historyItem = history.find(
Expand Down
2 changes: 1 addition & 1 deletion mongodb/src/classes/ShopItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class ShopItem extends Emitter {
*/
async buy(memberID, quantity = 1, currency = null, reason = 'received the item from the shop') {
if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('memberID', 'string', memberID), 'INVALID_TYPE')
}

const balance = this.cache.balance.get({
Expand Down
4 changes: 2 additions & 2 deletions mongodb/src/classes/user/History.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class History extends BaseManager {


if (typeof itemID !== 'number' && typeof itemID !== 'string') {
throw new EconomyError(errors.invalidTypes.editItemArgs.itemID + typeof itemID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('itemID', 'string or number', itemID), 'INVALID_TYPE')
}

if (!item) return false
Expand Down Expand Up @@ -115,7 +115,7 @@ class History extends BaseManager {
*/
async remove(id) {
if (typeof id !== 'number' && typeof id !== 'string') {
throw new EconomyError(errors.invalidTypes.id + typeof id, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('id', 'string or number', id), 'INVALID_TYPE')
}

const history = (await this.fetch(this.memberID, this.guildID)) || []
Expand Down
6 changes: 3 additions & 3 deletions mongodb/src/classes/user/Inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Inventory extends BaseManager {
const item = inventory[itemIndex]

if (typeof itemID !== 'number' && typeof itemID !== 'string') {
throw new EconomyError(errors.invalidTypes.editItemArgs.itemID + typeof itemID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('itemID', 'string or number', itemID), 'INVALID_TYPE')
}

if (!item) return null
Expand Down Expand Up @@ -183,7 +183,7 @@ class Inventory extends BaseManager {
const inventoryItems = inventory.filter(invItem => invItem.name == item.name)

if (typeof itemID !== 'number' && typeof itemID !== 'string') {
throw new EconomyError(errors.invalidTypes.editItemArgs.itemID + typeof itemID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('itemID', 'string or number', itemID), 'INVALID_TYPE')
}

if (!item) return false
Expand Down Expand Up @@ -222,7 +222,7 @@ class Inventory extends BaseManager {
const itemIndex = inventory.findIndex(invItem => invItem.id == item?.id)

if (typeof itemID !== 'number' && typeof itemID !== 'string') {
throw new EconomyError(errors.invalidTypes.editItemArgs.itemID + typeof itemID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('itemID', 'string or number', itemID), 'INVALID_TYPE')
}

if (!item) return false
Expand Down
8 changes: 0 additions & 8 deletions mongodb/src/classes/user/Rewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ class Rewards {
async receive(reward, reason) {
const rewardTypes = ['daily', 'work', 'weekly', 'monthly', 'hourly']

if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
}

if (isNaN(reward) || !rewardTypes[reward]) {
throw new EconomyError(
errors.invalidType('reward', 'key of RewardType enum', typeof reward),
Expand Down
38 changes: 19 additions & 19 deletions mongodb/src/managers/BalanceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ class BalanceManager extends Emitter {
*/
async fetch(memberID, guildID) {
if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('memberID', 'string', memberID), 'INVALID_TYPE')
}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('guildID', 'string', guildID), 'INVALID_TYPE')
}

const result = await this.database.fetch(`${guildID}.${memberID}.money`)
Expand Down Expand Up @@ -135,15 +135,15 @@ class BalanceManager extends Emitter {
const balance = await this.fetch(memberID, guildID)

if (isNaN(amount)) {
throw new EconomyError(errors.invalidTypes.amount + typeof amount, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('amount', 'number', amount), 'INVALID_TYPE')
}

if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('memberID', 'string', memberID), 'INVALID_TYPE')
}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('guildID', 'string', guildID), 'INVALID_TYPE')
}

await this.database.set(`${guildID}.${memberID}.money`, amount)
Expand Down Expand Up @@ -177,15 +177,15 @@ class BalanceManager extends Emitter {
const balance = await this.fetch(memberID, guildID)

if (isNaN(amount)) {
throw new EconomyError(errors.invalidTypes.amount + typeof amount, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('amount', 'number', amount), 'INVALID_TYPE')
}

if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('memberID', 'string', memberID), 'INVALID_TYPE')
}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('guildID', 'string', guildID), 'INVALID_TYPE')
}

await this.database.add(`${guildID}.${memberID}.money`, amount)
Expand Down Expand Up @@ -219,15 +219,15 @@ class BalanceManager extends Emitter {
const balance = await this.fetch(memberID, guildID)

if (isNaN(amount)) {
throw new EconomyError(errors.invalidTypes.amount + typeof amount, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('amount', 'number', amount), 'INVALID_TYPE')
}

if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('memberID', 'string', memberID), 'INVALID_TYPE')
}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('guildID', 'string', guildID), 'INVALID_TYPE')
}

await this.database.subtract(`${guildID}.${memberID}.money`, amount)
Expand Down Expand Up @@ -262,19 +262,19 @@ class BalanceManager extends Emitter {
const bank = await this.database.get(`${guildID}.${memberID}.bank`)

if (isNaN(amount)) {
throw new EconomyError(errors.invalidTypes.amount + typeof amount, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('amount', 'number', amount), 'INVALID_TYPE')
}

if (amount < 0) {
throw new EconomyError(errors.invalidTypes.depositInvalidInput, 'INVALID_INPUT')
}

if (typeof memberID !== 'string') {
throw new EconomyError(errors.invalidTypes.memberID + typeof memberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('memberID', 'string', memberID), 'INVALID_TYPE')
}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('guildID', 'string', guildID), 'INVALID_TYPE')
}

await this.database.subtract(`${guildID}.${memberID}.money`, amount)
Expand Down Expand Up @@ -316,7 +316,7 @@ class BalanceManager extends Emitter {
const data = await this.database.all()

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('guildID', 'string', guildID), 'INVALID_TYPE')
}

const guildData = data[guildID]
Expand Down Expand Up @@ -349,19 +349,19 @@ class BalanceManager extends Emitter {
} = options || {}

if (typeof guildID !== 'string') {
throw new EconomyError(errors.invalidTypes.guildID + typeof guildID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('guildID', 'string', guildID), 'INVALID_TYPE')
}

if (isNaN(amount)) {
throw new EconomyError(errors.invalidTypes.amount + typeof amount, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('amount', 'number', amount), 'INVALID_TYPE')
}

if (typeof senderMemberID !== 'string') {
throw new EconomyError(errors.invalidTypes.senderMemberID + typeof senderMemberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('senderMemberID', 'string', senderMemberID), 'INVALID_TYPE')
}

if (typeof receiverMemberID !== 'string') {
throw new EconomyError(errors.invalidTypes.receiverMemberID + typeof receiverMemberID, 'INVALID_TYPE')
throw new EconomyError(errors.invalidType('receiverMemberID', 'string', receiverMemberID), 'INVALID_TYPE')
}

await this.add(amount, receiverMemberID, guildID, receivingReason || 'receiving money from user')
Expand Down
Loading

0 comments on commit a4b34b5

Please sign in to comment.