From 1925deec9097792e395692f509f2939ff12bbbf7 Mon Sep 17 00:00:00 2001 From: ItzEmerald Date: Wed, 29 Jan 2020 18:55:32 +0000 Subject: [PATCH] Create TicketNumber.js --- utility/TicketNumber.js | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 utility/TicketNumber.js diff --git a/utility/TicketNumber.js b/utility/TicketNumber.js new file mode 100644 index 0000000..c6771fc --- /dev/null +++ b/utility/TicketNumber.js @@ -0,0 +1,63 @@ +class TicketId { + + static pad(guildId) { + let n = this.get(guildId) + ''; + let z = '0'; + let width = 4; + setTimeout(() => { + this.set(guildId, this.get(guildId) + 1); + }, 50); + return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; + } + + static get(guildId) { + + let ticketIds = JSON.parse(fs.readFileSync("./ticketIds.json", "utf8")); + + if (!ticketIds[guildId]) { + ticketIds[guildId] = { + id: 0 + } + + fs.writeFileSync("./ticketIds.json", JSON.stringify(ticketIds, null, 4), err => { + if (err) console.log(err); + }); + + return 0; + + + } else { + return ticketIds[guildId].id; + } + } + + static set(guildId, value) { + let ticketIds = JSON.parse(fs.readFileSync("./ticketIds.json", "utf8")); + + ticketIds[guildId] = { + id: value + } + + fs.writeFileSync("./ticketIds.json", JSON.stringify(ticketIds, null, 4), err => { + if (err) console.log(err); + }); + + return value; + } + + static reset(guildId) { + let ticketIds = JSON.parse(fs.readFileSync("./ticketIds.json", "utf8")); + + ticketIds[guildId] = { + id: 0 + } + + fs.writeFileSync("./ticketIds.json", JSON.stringify(ticketIds, null, 4), err => { + if (err) console.log(err); + }); + + return 0; + } +} + +module.exports = TicketId;