Skip to content
This repository has been archived by the owner on Apr 28, 2023. It is now read-only.

Commit

Permalink
feat: Persistent data 11 (Like my number inconsistencies?)
Browse files Browse the repository at this point in the history
  • Loading branch information
minotaa committed Jul 23, 2021
1 parent e0876c2 commit d0f11c7
Show file tree
Hide file tree
Showing 20 changed files with 322 additions and 276 deletions.
2 changes: 1 addition & 1 deletion src/commands/Economy/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BalanceCommand extends Command {
}

async run (bot, msg, args) {
checkUser(bot, msg.author.id)
checkUser(msg.author.id, bot)
const user = await User.findOne({
id: msg.author.id
}).exec()
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Economy/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BuyCommand extends Command {
}

async run (bot, msg, args) {
checkUser(bot, msg.author.id)
checkUser(msg.author.id, bot)
const color = await getColor(bot, msg.member)
const ai = allItems()
const prefix = await getPrefix(msg.guild.id)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Economy/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DepositCommand extends Command {
return false
}
}
if (!user.balance = 0) user.balance = 0
if (!user.balance === 0) user.balance = 0
user.balance -= amount
user.bank += amount
user.save()
Expand Down
3 changes: 2 additions & 1 deletion src/commands/Economy/farm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FarmCommand extends Command {
this.client.emit('customError', `You don't have a bank account! Create one using \`${prefix}start\`.`, msg)
return false
}
const items = user.items || []
const items = user.items || {}
if (
!items['flimsy_hoe'] &&
!items['decent_hoe'] &&
Expand Down Expand Up @@ -200,6 +200,7 @@ class FarmCommand extends Command {
embed.addField(':sparkles: Lucky!', `You also found gold! You get :coin: **${goldEggBonus}** as a bonus.`)
}
await addExp(msg.author, 'farming', msg)
user.save()
embed.addField(':diamond_shape_with_a_dot_inside: Progress', `:trident: **EXP** needed until next level up: **${Math.floor(eco.get(`${msg.author.id}.skills.farming.req`) - eco.get(`${msg.author.id}.skills.farming.exp`))}**`)
const filter2 = i => i.customID === 'farm' && i.user.id === msg.author.id
const row2 = new MessageActionRow()
Expand Down
44 changes: 23 additions & 21 deletions src/commands/Economy/fish.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@

import Command from '../../classes/Command'
import { MessageEmbed, MessageActionRow, MessageButton } from 'discord.js'
import { getRandomInt, getPrefix, addCommas, getItem, allItems } from '../../utils/utils'
import { getRandomInt, getPrefix, getColor, getItem, allItems } from '../../utils/utils'
import { addMoney, addExp } from '../../utils/economy'
import romanizeNumber from 'romanize-number'
import db from 'quick.db'

const eco = new db.table('economy')
const cfg = new db.table('config')
import User from '../../models/User'

async function getLoot(place: string) {
let odds = getRandomInt(1, 100)
Expand Down Expand Up @@ -53,18 +50,22 @@ class FishCommand extends Command {
}

async run (bot, msg, args) {
const color = cfg.get(`${msg.author.id}.color`) || msg.member.roles.highest.color
if (!eco.get(`${msg.author.id}.started`)) {
this.client.emit('customError', 'You don\'t have a bank account!', msg)
const color = await getColor(bot, msg.member)
const user = await User.findOne({
id: msg.author.id
}).exec()
const prefix = await getPrefix(msg.guild.id)
if (!user || !user.started) {
this.client.emit('customError', `You don't have a bank account! Create one using \`${prefix}start\`.`, msg)
return false
}
const items = eco.get(`${msg.author.id}.items`) || []
const items = user.items || {}
if (
!items['flimsy_fishing_rod'] &&
!items['decent_fishing_rod'] &&
!items['great_fishing_rod']
) {
this.client.emit('customError', `You need a fishing rod to fish, purchase one on the store using \`${getPrefix(msg.guild.id)}buy flimsy_fishing_rod\`.`, msg)
this.client.emit('customError', `You need a fishing rod to fish, purchase one on the store using \`${prefix}buy flimsy_fishing_rod\`.`, msg)
return false
}
const filter = i => {
Expand All @@ -84,10 +85,10 @@ class FishCommand extends Command {
correctDisplay = '🏞️ Lake'

}
const exp = eco.get(`${msg.author.id}.skills.fishing.exp`)
const exp = user.skills.fishing.exp
let bar
let barItem
if (eco.get(`${msg.author.id}.skills.fishing.exp`) !== 0) {
if (user.skills.fishing.exp !== 0) {
bar = Math.ceil(exp / 10)
barItem = '▇'
} else {
Expand All @@ -98,7 +99,7 @@ class FishCommand extends Command {
.setColor(color)
.setFooter('You have 8 seconds to pick a location.')
.setTitle(':fishing_pole_and_fish: Fishing')
.setDescription(`**:map: Correct Location**\n The **${correctDisplay}** is the correct place to fish at. \n\n:question: **How to fish**\nPlease choose a location to fish at from the corresponding bottom locations.\n\n**:diamond_shape_with_a_dot_inside: Progress**\n:fishing_pole_and_fish: Fishing [ ${barItem.repeat(bar)} ] (Level **${romanizeNumber(eco.get(`${msg.author.id}.skills.fishing.level`))}**) (**${Math.floor(eco.get(`${msg.author.id}.skills.fishing.exp`) / eco.get(`${msg.author.id}.skills.fishing.req`) * 100)}**%)`)
.setDescription(`**:map: Correct Location**\n The **${correctDisplay}** is the correct place to fish at. \n\n:question: **How to fish**\nPlease choose a location to fish at from the corresponding bottom locations.\n\n**:diamond_shape_with_a_dot_inside: Progress**\n:fishing_pole_and_fish: Fishing [ ${barItem.repeat(bar)} ] (Level **${romanizeNumber(user.skills.fishing.level)}**) (**${Math.floor(user.skills.fishing.exp / user.skills.fishing.req * 100)}**%)`)
const row = new MessageActionRow()
.addComponents(
new MessageButton()
Expand Down Expand Up @@ -160,27 +161,27 @@ class FishCommand extends Command {
let fishingBaitBonus
let loot = await getLoot(interaction.customID)
if (items['fishing_bait']) {
if (eco.get(`${msg.author.id}.items.fishing_bait`) === 0) eco.delete(`${msg.author.id}.items.fishing_bait`)
else eco.subtract(`${msg.author.id}.items.fishing_bait`, 1)
if (user.items.fishing_bait === 0) delete user.items.fishing_bait
else user.items.fishing_bait -= 1
bonus = bonus + getRandomInt(3, 10)
fishingBaitBonus = true
}
const goldenReelBonus = getRandomInt(45, 175)
let amount = getRandomInt(2, 8)
let lvl = eco.get(`${msg.author.id}.skills.fishing.level`) || 0
let lvl = user.skills.fishing.level || 1
let fishGained = Math.floor(amount + amount * (lvl * 0.1))
amount = addMoney(msg.author.id, Math.floor(amount + amount * (lvl * 0.1)))
const embed = new MessageEmbed()
.setColor(color)
let lootDisplay = []
loot.forEach(item => {
let i = getItem(allItems(), item)
if (eco.get(`${msg.author.id}.items.${i.id}`)) eco.add(`${msg.author.id}.items.${i.id}`, 1)
else eco.set(`${msg.author.id}.items.${i.id}`, 1)
if (user.items[i.id]) user.items[i.id] += 1
else user.items[i.id] = 1
lootDisplay.push(`${i.emoji} **${i.display}**`)
})
if (eco.get(`${msg.author.id}.items.fish`)) eco.add(`${msg.author.id}.items.fish`, fishGained)
else eco.set(`${msg.author.id}.items.fish`, fishGained)
if (user.items.fish) user.items.fish += fishGained
else user.items.fish = fishGained
if (!fishingBaitBonus) {
embed.addField(':fishing_pole_and_fish: Fishing', `You fished for **${getRandomInt(1, 10)} hours**, here's what you fished up!`)
lootDisplay.push(`${fishGained} :fish: **Fish** **(+${bonus})**`)
Expand All @@ -195,7 +196,8 @@ class FishCommand extends Command {
embed.addField(':sparkles: Lucky!', `You also found gold! You get :coin: **${goldenReelBonus}** as a bonus.`)
}
addExp(msg.author, 'fishing', msg)
embed.addField(':diamond_shape_with_a_dot_inside: Progress', `:trident: **EXP** needed until next level up: **${Math.floor(eco.get(`${msg.author.id}.skills.fishing.req`) - eco.get(`${msg.author.id}.skills.fishing.exp`))}**`)
user.save()
embed.addField(':diamond_shape_with_a_dot_inside: Progress', `:trident: **EXP** needed until next level up: **${Math.floor(user.skills.fishing.req - user.skills.fishing.exp)}**`)
const filter2 = i => i.customID === 'fish' && i.user.id === msg.author.id
const row2 = new MessageActionRow()
.addComponents(
Expand Down
43 changes: 23 additions & 20 deletions src/commands/Economy/hunt.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

import Command from '../../classes/Command'
import { MessageEmbed, MessageActionRow, MessageButton } from 'discord.js'
import { getRandomInt, getPrefix, addCommas, getItem, allItems } from '../../utils/utils'
import { getRandomInt, getPrefix, getColor, getItem, allItems } from '../../utils/utils'
import { addMoney, addExp } from '../../utils/economy'
import romanizeNumber from 'romanize-number'
import db from 'quick.db'

const eco = new db.table('economy')
const cfg = new db.table('config')
import User from '../../models/User'

async function getLoot(crop: string) {
let odds = getRandomInt(1, 100)
Expand Down Expand Up @@ -54,18 +52,22 @@ class HuntCommand extends Command {
}

async run (bot, msg, args) {
const color = cfg.get(`${msg.author.id}.color`) || msg.member.roles.highest.color
if (!eco.get(`${msg.author.id}.started`)) {
this.client.emit('customError', 'You don\'t have a bank account!', msg)
const color = await getColor(bot, msg.member)
const user = await User.findOne({
id: msg.author.id
}).exec()
const prefix = await getPrefix(msg.guild.id)
if (!user || !user.started) {
this.client.emit('customError', `You don't have a bank account! Create one using \`${prefix}start\`.`, msg)
return false
}
const items = eco.get(`${msg.author.id}.items`) || []
const items = user.items || {}
if (
!items['flimsy_rifle'] &&
!items['decent_rifle'] &&
!items['great_rifle']
) {
this.client.emit('customError', `You need a rifle to hunt, purchase one on the store using \`${getPrefix(msg.guild.id)}buy flimsy_rifle\`.`, msg)
this.client.emit('customError', `You need a rifle to hunt, purchase one on the store using \`${prefix}buy flimsy_rifle\`.`, msg)
return false
}
const filter = i => {
Expand All @@ -85,10 +87,10 @@ class HuntCommand extends Command {
correctDisplay = '🦌 Deer'

}
const exp = eco.get(`${msg.author.id}.skills.hunting.exp`)
const exp = user.skills.hunting.exp
let bar
let barItem
if (eco.get(`${msg.author.id}.skills.hunting.exp`) !== 0) {
if (user.skills.hunting.exp !== 0) {
bar = Math.ceil(exp / 10)
barItem = '▇'
} else {
Expand All @@ -99,7 +101,7 @@ class HuntCommand extends Command {
.setColor(color)
.setFooter('You have 8 seconds to pick an animal to hunt.')
.setTitle('🐇 Hunting')
.setDescription(`**:map: Available animals**\n The **${correctDisplay}** is the only available animal to hunt. \n\n:question: **How to hunt**\nPlease choose a location to hunt at from the corresponding bottom animals.\n\n**:diamond_shape_with_a_dot_inside: Progress**\n:rabbit2: Hunting [ ${barItem.repeat(bar)} ] (Level **${romanizeNumber(eco.get(`${msg.author.id}.skills.hunting.level`))}**) (**${Math.floor(eco.get(`${msg.author.id}.skills.hunting.exp`) / eco.get(`${msg.author.id}.skills.hunting.req`) * 100)}**%)`)
.setDescription(`**:map: Available animals**\n The **${correctDisplay}** is the only available animal to hunt. \n\n:question: **How to hunt**\nPlease choose a location to hunt at from the corresponding bottom animals.\n\n**:diamond_shape_with_a_dot_inside: Progress**\n:rabbit2: Hunting [ ${barItem.repeat(bar)} ] (Level **${romanizeNumber(user.skills.hunting.level)}**) (**${Math.floor(user.skills.hunting.exp / user.skills.hunting.req * 100)}**%)`)
const row = new MessageActionRow()
.addComponents(
new MessageButton()
Expand Down Expand Up @@ -161,27 +163,27 @@ class HuntCommand extends Command {
let trapBonus
let loot = await getLoot(interaction.customID)
if (items['trap']) {
if (eco.get(`${msg.author.id}.items.trap`) === 0) eco.delete(`${msg.author.id}.items.trap`)
else eco.subtract(`${msg.author.id}.items.trap`, 1)
if (user.items.trap === 0) delete user.items.trap
else user.items.trap -= 1
bonus = bonus + getRandomInt(3, 10)
trapBonus = true
}
const goldEggBonus = getRandomInt(45, 175)
let amount = getRandomInt(2, 8)
let lvl = eco.get(`${msg.author.id}.skills.hunting.level`) || 0
let lvl = user.skills.hunting.level || 1
let animalsHunted = Math.floor(amount + amount * (lvl * 0.1))
amount = addMoney(msg.author.id, Math.floor(amount + amount * (lvl * 0.1)))
const embed = new MessageEmbed()
.setColor(color)
let lootDisplay = []
loot.forEach(item => {
let i = getItem(allItems(), item)
if (eco.get(`${msg.author.id}.items.${i.id}`)) eco.add(`${msg.author.id}.items.${i.id}`, 1)
else eco.set(`${msg.author.id}.items.${i.id}`, 1)
if (user.items[i.id]) user.items[i.id] + 1
else user.items[i.id] = 1
lootDisplay.push(`${i.emoji} **${i.display}**`)
})
if (eco.get(`${msg.author.id}.items.${interaction.customID}`)) eco.add(`${msg.author.id}.items.${interaction.customID}`, animalsHunted)
else eco.set(`${msg.author.id}.items.${interaction.customID}`, animalsHunted)
if (user.items[interaction.customID]) user.items[interaction.customID] += animalsHunted
else user.items[interaction.customID] = animalsHunted
if (!goldEggBonus) {
embed.addField(':rabbit2: Hunting', `You hunted for **${getRandomInt(1, 10)} hours**, here's what you got for game!`)
lootDisplay.push(`${animalsHunted} ${correctDisplay} **(+${bonus})**`)
Expand All @@ -196,7 +198,8 @@ class HuntCommand extends Command {
embed.addField(':sparkles: Lucky!', `You also found gold! You get :coin: **${goldEggBonus}** as a bonus.`)
}
addExp(msg.author, 'hunting', msg)
embed.addField(':diamond_shape_with_a_dot_inside: Progress', `:trident: **EXP** needed until next level up: **${Math.floor(eco.get(`${msg.author.id}.skills.hunting.req`) - eco.get(`${msg.author.id}.skills.hunting.exp`))}**`)
user.save()
embed.addField(':diamond_shape_with_a_dot_inside: Progress', `:trident: **EXP** needed until next level up: **${Math.floor(user.skills.hunting.req - user.skills.hunting.exp)}**`)
const filter2 = i => i.customID === 'hunt' && i.user.id === msg.author.id
const row2 = new MessageActionRow()
.addComponents(
Expand Down
34 changes: 21 additions & 13 deletions src/commands/Economy/inventory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import Command from '../../classes/Command'
import { addCommas, getItem, allItems, getPrefix } from '../../utils/utils'
import { addCommas, getItem, allItems, getPrefix, getColor } from '../../utils/utils'
import { MessageActionRow, MessageButton, MessageEmbed } from 'discord.js'
import db from 'quick.db'

const eco = new db.table('economy')
const cfg = new db.table('config')
import User from '../../models/User'

function truncate( str, n, useWordBoundary ){
if (str.length <= n) { return str; }
Expand All @@ -27,8 +24,12 @@ class InventoryCommand extends Command {
}

async awaitControl(message, embed, filter, page, maxPages, user, guild) {
const color = cfg.get(`${user.user.id}.color`) || user.roles.highest.color
const items = eco.get(`${user.user.id}.items`) || {}
const color = await getColor(message.client, message.member)
const userResult = await User.findOne({
id: user.user.id
})
const items = userResult.items || {}
const prefix = await getPrefix(guild.id)
const keys = Object.keys(items)
const row = new MessageActionRow()
.addComponents(
Expand All @@ -52,7 +53,7 @@ class InventoryCommand extends Command {
.setFooter(`Page ${page} of ${maxPages}`)
for (let n = 0; n < 9; n++) {
const i = getItem(allItems(), keys[n + (9 * (page - 1))])
if (i) embed.addField(`${i.emoji} ${i.display}${items[i.id]}`, `ID: \`${i.id}\` | Sell price: :coin: **${addCommas(Math.floor(i.sell_price))}** | ${truncate(i.description.replace('[PRE]', getPrefix(guild.id)), 50, '...')}`, true)
if (i) embed.addField(`${i.emoji} ${i.display}${items[i.id]}`, `ID: \`${i.id}\` | Sell price: :coin: **${addCommas(Math.floor(i.sell_price))}** | ${truncate(i.description.replace('[PRE]', prefix), 50, '...')}`, true)
}
i.update({ embeds: [embed], components: [row] })
this.awaitControl(message, embed, filter, page, maxPages, user, guild)
Expand All @@ -68,7 +69,7 @@ class InventoryCommand extends Command {
.setFooter(`Page ${page} of ${maxPages}`)
for (let n = 0; n < 9; n++) {
const i = getItem(allItems(), keys[n + (9 * (page - 1))])
if (i) embed.addField(`${i.emoji} ${i.display}${items[i.id]}`, `ID: \`${i.id}\` | Sell price: :coin: **${addCommas(Math.floor(i.sell_price))}** | ${truncate(i.description.replace('[PRE]', getPrefix(guild.id)), 50, '...')}`, true)
if (i) embed.addField(`${i.emoji} ${i.display}${items[i.id]}`, `ID: \`${i.id}\` | Sell price: :coin: **${addCommas(Math.floor(i.sell_price))}** | ${truncate(i.description.replace('[PRE]', prefix), 50, '...')}`, true)
}
i.update({ embeds: [embed], components: [row] })
this.awaitControl(message, embed, filter, page, maxPages, user, guild)
Expand All @@ -83,22 +84,29 @@ class InventoryCommand extends Command {
}

async run (bot, msg, args) {
if (!eco.get(`${msg.author.id}.started`)) return this.client.emit('customError', 'You don\'t have a bank account!', msg)
const color = cfg.get(`${msg.author.id}.color`) || msg.member.roles.highest.color
const color = await getColor(bot, msg.member)
const prefix = await getPrefix(msg.guild.id)
const user = msg.guild.members.cache.get(args[0]) || msg.mentions.members.first() || msg.member
const userResult = await User.findOne({
id: user.user.id
}).exec()
if (!userResult || !userResult.started) {
this.client.emit('customError', `You don't have a bank account! Create one using \`${prefix}start\`.`, msg)
return false
}
let embed = new MessageEmbed()
.setColor(color)
.setAuthor(`${user.user.username}'s Inventory`, user.user.avatarURL())
let page = 1
const items = eco.get(`${msg.author.id}.items`) || {}
const items = user.items || {}
const keys = Object.keys(items)
let maxPages = Math.ceil(keys.length / 9)
embed.setFooter(`Page 1 of ${Math.ceil(keys.length / 9)}`)
let n = 0
keys.forEach(item => {
if (n !== 9) {
const i = getItem(allItems(), item)
embed.addField(`${i.emoji} ${i.display}${items[item]}`, `ID: \`${i.id}\` | Sell price: :coin: **${addCommas(Math.floor(i.sell_price))}** | ${truncate(i.description.replace('[PRE]', getPrefix(msg.guild.id)), 50, '...')}`, true)
embed.addField(`${i.emoji} ${i.display}${items[item]}`, `ID: \`${i.id}\` | Sell price: :coin: **${addCommas(Math.floor(i.sell_price))}** | ${truncate(i.description.replace('[PRE]', prefix), 50, '...')}`, true)
n++
}
})
Expand Down
Loading

0 comments on commit d0f11c7

Please sign in to comment.