Skip to content

Commit

Permalink
New leveling formula, limit, and many more
Browse files Browse the repository at this point in the history
  • Loading branch information
SlavyanDesu committed Feb 5, 2021
1 parent 4052e54 commit de5d6ac
Show file tree
Hide file tree
Showing 16 changed files with 851 additions and 349 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ If you want to unlock premium commands, please buy me a coffee at least 1 on Ko-
| Sholat schedule | ✔️ |
| Latest Line stickers | ✔️ |
| Check postage | ✔️ |
| Spoiler text | ✔️ |
| Sending email | ✔️ |
| Random quotes | ✔️ |
| Genshin chara info | ✔️ |
Expand Down
2 changes: 1 addition & 1 deletion database/bot/mute.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[]
2 changes: 1 addition & 1 deletion database/bot/registered.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[]
2 changes: 1 addition & 1 deletion database/group/leveling.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[]
2 changes: 1 addition & 1 deletion database/user/card/background.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[]
2 changes: 1 addition & 1 deletion database/user/level.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
[]
27 changes: 11 additions & 16 deletions function/card.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
const fs = require('fs-extra')

/**
* Add background card.
* @param {String} userId
* @param {Object} _dir
*/
const addBg = (userId, _dir) => {
const obj = { id: userId, link: 'https://i.ibb.co/tYf3jmz/amos-yan-no-entry-1.jpg' }
_dir.push(obj)
fs.writeFileSync('./database/user/card/background.json', JSON.stringify(_dir))
}

/**
* Get background.
* @param {String} userId
* @param {Object} _dir
* @returns {String}
*/
const getBg = (userId, _dir) => {
let position = null
let pos = null
let found = false
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
pos = i
found = true
}
})
if (position !== null) {
return _dir[position].link
if (found === false && pos === null) {
const obj = { id: userId, link: 'https://i.ibb.co/tYf3jmz/amos-yan-no-entry-1.jpg' }
_dir.push(obj)
fs.writeFileSync('./database/user/card/background.json', JSON.stringify(_dir))
return 'https://i.ibb.co/tYf3jmz/amos-yan-no-entry-1.jpg'
} else {
return _dir[pos].link
}
}

Expand All @@ -49,7 +45,6 @@ const replaceBg = (userId, link, _dir) => {
}

module.exports = {
addBg,
getBg,
replaceBg
}
36 changes: 36 additions & 0 deletions function/daily.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs-extra')

/**
* Add limit.
* @param {String} userId
* @param {String} dir
* @param {Object} _dir
*/
const addLimit = (userId, _dir) => {
const obj = { id: userId, time: Date.now() }
_dir.push(obj)
fs.writeFileSync('./database/user/daily.json', JSON.stringify(_dir))
}

/**
* Get time limit.
* @param {String} userId
* @param {Object} _dir
* @returns {Number}
*/
const getLimit = (userId, _dir) => {
let position = null
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
}
})
if (position !== null) {
return _dir[position].time
}
}

module.exports = {
addLimit,
getLimit
}
1 change: 1 addition & 0 deletions function/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
exports.level = require('./level')
exports.daily = require('./daily')
exports.limit = require('./limit')
exports.card = require('./card')
exports.register = require('./register')
Expand Down
80 changes: 55 additions & 25 deletions function/level.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fs = require('fs-extra')
const { getBg } = require('./card')
const _bg = JSON.parse(fs.readFileSync('./database/user/card/background.json'))

/**
* Get user ID from db.
Expand All @@ -7,14 +9,22 @@ const fs = require('fs-extra')
* @returns {String}
*/
const getLevelingId = (userId, _dir) => {
let position = null
let pos = null
let found = false
getBg(userId, _bg)
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
pos = i
found = true
}
})
if (position !== null) {
return _dir[position].id
if (found === false && pos === null) {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
return userId
} else {
return _dir[pos].id
}
}

Expand All @@ -25,14 +35,22 @@ const getLevelingId = (userId, _dir) => {
* @returns {Number}
*/
const getLevelingLevel = (userId, _dir) => {
let position = null
let pos = null
let found = false
getBg(userId, _bg)
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
pos = i
found = true
}
})
if (position !== null) {
return _dir[position].level
if (found === false && pos === null) {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
return 1
} else {
return _dir[pos].level
}
}

Expand All @@ -43,28 +61,25 @@ const getLevelingLevel = (userId, _dir) => {
* @returns {Number}
*/
const getLevelingXp = (userId, _dir) => {
let position = null
let pos = null
let found = false
getBg(userId, _bg)
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
pos = i
found = true
}
})
if (position !== null) {
return _dir[position].xp
if (found === false && pos === null) {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
return 0
} else {
return _dir[pos].xp
}
}

/**
* Add user to db.
* @param {String} userId
* @param {Object} _dir
*/
const addLevelingId = (userId, _dir) => {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
}

/**
* Add user level to db.
* @param {String} userId
Expand Down Expand Up @@ -103,11 +118,26 @@ const addLevelingXp = (userId, amount, _dir) => {
}
}

// Cooldown XP gains to prevent spam
const xpGain = new Set()

const isGained = (from) => {
return !!xpGain.has(from)
}

const addCooldown = (from) => {
xpGain.add(from)
setTimeout(() => {
return xpGain.delete(from)
}, 60000) // Each minute
}

module.exports = {
getLevelingId,
getLevelingLevel,
getLevelingXp,
addLevelingId,
addLevelingLevel,
addLevelingXp
addLevelingXp,
isGained,
addCooldown
}
74 changes: 62 additions & 12 deletions function/limit.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,86 @@
const fs = require('fs-extra')

/**
* Add limit.
* Check limit.
* @param {String} userId
* @param {String} dir
* @param {Object} _dir
* @param {Number} limitCount
* @param {Boolean} isPremium
* @param {Boolean} isOwner
* @returns {Boolean}
*/
const addLimit = (userId, _dir) => {
const obj = { id: userId, time: Date.now() }
_dir.push(obj)
fs.writeFileSync('./database/user/daily.json', JSON.stringify(_dir))
const isLimit = (userId, _dir, limitCount, isPremium, isOwner) => {
if (isPremium || isOwner) return false
let found = false
for (let i of _dir) {
if (i.id === userId) {
if (i.limit <= 0) {
found = true
return true
} else {
found = true
return false
}
}
}
if (found === false) {
const obj = { id: userId, limit: limitCount }
_dir.push(obj)
fs.writeFileSync('./database/user/limit.json', JSON.stringify(_dir))
return false
}
}

/**
* Add limit to user.
* @param {String} userId
* @param {Object} _dir
* @param {Boolean} isPremium
* @param {Boolean} isOwner
* @returns {*}
*/
const addLimit = (userId, _dir, isPremium, isOwner) => {
if (isPremium || isOwner) return false
let pos = null
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
pos = i
}
})
if (pos !== null) {
_dir[pos].limit -= 1
fs.writeFileSync('./database/user/limit.json', JSON.stringify(_dir))
}
}

/**
* Get time limit.
* Get user's limit.
* @param {String} userId
* @param {Object} _dir
* @param {Number} limitCount
* @returns {Number}
*/
const getLimit = (userId, _dir) => {
let position = null
const getLimit = (userId, _dir, limitCount) => {
let pos = null
let found = false
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
pos = i
found = true
}
})
if (position !== null) {
return _dir[position].time
if (found === false && pos === null) {
const obj = { id: userId, limit: limitCount }
_dir.push(obj)
fs.writeFileSync('./database/user/limit.json', JSON.stringify(_dir))
return limitCount
} else {
return _dir[pos].limit
}
}

module.exports = {
isLimit,
addLimit,
getLimit
}
18 changes: 17 additions & 1 deletion function/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* eslint-disable no-empty-function */
const fs = require('fs-extra')
const { color } = require('../tools')

/**
* Returns an array of files.
* @param {*} dirPath
* @param {String[]} arrayOfFiles
* @returns {String[]}
*/
const getAllDirFiles = (dirPath, arrayOfFiles) => {
const files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
Expand All @@ -14,6 +21,10 @@ const getAllDirFiles = (dirPath, arrayOfFiles) => {
return arrayOfFiles
}

/**
* Uncache a changes.
* @param {*} module
*/
const uncache = (module = '.') => {
return new Promise((resolve, reject) => {
try {
Expand All @@ -25,7 +36,12 @@ const uncache = (module = '.') => {
})
}

const nocache = (module, call = () => { }) => {
/**
* Delete file cache.
* @param {*} module
* @param {*} call
*/
const nocache = (module, call = () => {}) => {
console.log(color('[WATCH]', 'orange'), color(`=> '${module}'`, 'yellow'), 'file is now being watched by me!')
fs.watchFile(require.resolve(module), async () => {
await uncache(require.resolve(module))
Expand Down
Loading

0 comments on commit de5d6ac

Please sign in to comment.