From 3447a3760380eb4c1a46cd82f8d6068761615c2e Mon Sep 17 00:00:00 2001 From: DJj123dj <80536295+DJj123dj@users.noreply.github.com> Date: Tue, 12 Dec 2023 21:32:15 +0100 Subject: [PATCH] v3.5.0 push 2 --- core/dynamicdatabase/databases/stats.js | 15 ++++-- core/statsManager.js | 71 +++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 core/statsManager.js diff --git a/core/dynamicdatabase/databases/stats.js b/core/dynamicdatabase/databases/stats.js index 388b9eb..daa5f8a 100644 --- a/core/dynamicdatabase/databases/stats.js +++ b/core/dynamicdatabase/databases/stats.js @@ -16,6 +16,12 @@ function setData(data){ fs.writeFileSync(location,JSON.stringify(data,null,"\t")) } +/** + * @param {String} category + * @param {String} key + * @param {String|Boolean|Number} value + * @returns {Boolean} exists? + */ exports.set = (category,key,value) => { const currentData = getData() const exists = currentData.find((d) => (d.category == category) && (d.key == key)) ? true : false @@ -39,18 +45,17 @@ exports.set = (category,key,value) => { /** * @param {String} category * @param {String} key - * @returns {String|false} + * @returns {String|Boolean|Number|undefined} */ exports.get = (category,key) => { const currentData = getData() - const tempresult = currentData.find((d) => (d.category == category) && (d.key == key)) - const result = tempresult ? tempresult.value : false + const result = currentData.find((d) => (d.category == category) && (d.key == key)) return result } /** * @param {String} category - * @returns {[{key:String,value:String}]|false} + * @returns {[{key:String,value:String|Boolean|Number}]|false} */ exports.getCategory = (category) => { const currentData = getData() @@ -61,7 +66,7 @@ exports.getCategory = (category) => { /** * @param {String} category * @param {String} key - * @param {String} value + * @returns {Boolean} exists? */ exports.delete = (category,key) => { const currentData = getData() diff --git a/core/statsManager.js b/core/statsManager.js new file mode 100644 index 0000000..9c38809 --- /dev/null +++ b/core/statsManager.js @@ -0,0 +1,71 @@ +const bot = require('../index') +const client = bot.client +const config = bot.config +const log = bot.errorLog.log +const l = bot.language + +/**@typedef {"TICKETS_CREATED"|"TICKETS_CLOSED"|"TICKETS_DELETED"|"TICKETS_REOPENED"|"TICKETS_AUTOCLOSED"|"TRANSCRIPTS_CREATED"|"TIME_SINCE_UPDATE"} OTGlobalStatsType */ +/**@typedef {"TICKET_AGE"} OTTicketStatsType */ +/**@typedef {"TICKETS_CREATED"} OTUserStatsType */ +/** + * @callback OTStatsCallback + * @param {Boolean|Number|String} current + * @returns {Boolean|Number|String|undefined} + */ + + +/** + * @param {OTGlobalStatsType} type + * @param {OTStatsCallback} callback + */ +const updateGlobalStats = (type,callback) => { + const currentstat = bot.statsStorage.get("global",type) + const newstat = callback(currentstat) + if (["string","number","boolean"].includes(typeof newstat)){ + bot.statsStorage.set("global",type,newstat) + } +} + +/** + * @param {OTTicketStatsType} type + * @param {String} id + * @param {OTStatsCallback} callback + */ +const updateTicketStats = (type,id,callback) => { + const currentstat = bot.statsStorage.get("ticket_"+id,type) + const newstat = callback(currentstat) + if (["string","number","boolean"].includes(typeof newstat)){ + bot.statsStorage.set("ticket"+id,type,newstat) + } +} + +/** + * @param {OTUserStatsType} type + * @param {String} id + * @param {OTStatsCallback} callback + */ +const updateUserStats = (type,id,callback) => { + const currentstat = bot.statsStorage.get("user"+id,type) + const newstat = callback(currentstat) + if (["string","number","boolean"].includes(typeof newstat)){ + bot.statsStorage.set("user"+id,type,newstat) + } +} + +/** + * + * @param {"global"|"ticket"|"user"} scope + * @param {OTGlobalStatsType|OTTicketStatsType|OTUserStatsType} type + * @param {String} [id] + */ +const removeStats = (scope,type,id) => { + const newScope = (scope == "global") ? scope : scope+"_"+id + bot.statsStorage.delete(newScope,type) +} + +module.exports = { + updateGlobalStats, + updateTicketStats, + updateUserStats, + removeStats +} \ No newline at end of file diff --git a/package.json b/package.json index 4ad4f14..b05163d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "author": "DJdj Development", "license": "ISC", "dependencies": { - "axios": "^0.27.2", + "axios": "^1.6.2", "chalk": "^5.0.1", "discord.js": "^14.14.1" },