diff --git a/classes/FileManager.js b/classes/FileManager.js new file mode 100644 index 0000000..179d5e6 --- /dev/null +++ b/classes/FileManager.js @@ -0,0 +1,30 @@ +/* +from BookmanDB: https://www.npmjs.com/package/bookman +*/ +const { writeFileSync, mkdirSync, existsSync, readFileSync, createWriteStream } = require("fs"); +const SuperError = require("./SuperError"); + +module.exports = class FileManager { + static writeFile(r, o) { + writeFileSync(`./${r}`, JSON.stringify(o, null, 4), (r) => { + if (r) throw new SuperError("CanNotWriteFile", r.toString()); + }); + } + static checkFile(file) { + let r = file.split("/"); + r.length > 1 && (r.pop(), (r = r.join("/")), existsSync(r) || mkdirSync(`./${r}/`, { recursive: !0 })); + + try { + let t = JSON.parse(readFileSync(`./${file}`, "utf8")); + t || ((t = []), this.writeFile(file, t)); + } catch (r) { + try { + createWriteStream(`./${file}`); + let r = []; + this.writeFile(file, r); + } catch (e) { + throw new SuperError("CanNotCreateExportFile", e.toString()); + } + } + } +} \ No newline at end of file diff --git a/classes/ReactionRole.js b/classes/ReactionRole.js index 3f6adaa..4040fa5 100644 --- a/classes/ReactionRole.js +++ b/classes/ReactionRole.js @@ -1,5 +1,6 @@ const { Client } = require("discord.js"); const SuperError = require("./SuperError"); +const FileManager = require("./FileManager"); module.exports = function ReactionRole(token) { self = this; @@ -7,9 +8,12 @@ module.exports = function ReactionRole(token) { self.token = token; self.config = []; self.client = new Client(); + self.fileManager = FileManager; self.createOption = (...arguments) => require("../methods/createOption")(...arguments); self.createMessage = (...arguments) => require("../methods/createMessage")(self, ...arguments); self.init = async () => await require("../methods/init")(self); self.reInit = async () => await require("../methods/reInit")(self); + self.importConfig = (config) => require("../methods/importConfig")(self, config); + self.exportConfig = (file) => require("../methods/exportConfig")(self, file); }; \ No newline at end of file diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..5af4953 --- /dev/null +++ b/config.example.json @@ -0,0 +1,29 @@ +[ + { + "messageID": "MESSAGE_ID", + "channelID": "CHANNEL_ID", + "limit": 2, + "restrictions": null, + "reactions": [ + { + "emoji": "EMOJI", + "roles": [ + "ROLE_ID", + "ROLE_ID" + ] + }, + { + "emoji": "EMOJI", + "roles": [ + "ROLE_ID" + ] + }, + { + "emoji": "EMOJI", + "roles": [ + "ROLE_ID" + ] + } + ] + } +] \ No newline at end of file diff --git a/methods/exportConfig.js b/methods/exportConfig.js new file mode 100644 index 0000000..1f7a3b8 --- /dev/null +++ b/methods/exportConfig.js @@ -0,0 +1,4 @@ +module.exports = (self, file) => { + self.fileManager.checkFile(file); + self.fileManager.writeFile(file, self.config); +}; \ No newline at end of file diff --git a/methods/importConfig.js b/methods/importConfig.js new file mode 100644 index 0000000..9627d9a --- /dev/null +++ b/methods/importConfig.js @@ -0,0 +1,3 @@ +module.exports = (self, config) => { + self.config = self.config.concat(config) +} \ No newline at end of file diff --git a/package.json b/package.json index aee2b61..f813d2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reaction-role", - "version": "2.0.1", + "version": "2.0.2", "description": "ReactionRole is a module that allows you to create Discord reaction role easily!", "main": "classes/ReactionRole.js", "scripts": { diff --git a/test.js b/test.js index d16acf4..a387faf 100644 --- a/test.js +++ b/test.js @@ -1,10 +1,10 @@ const ReactionRole = require("."); -const reactionRole = new ReactionRole("TOKEN"); +const system = new ReactionRole("TOKEN"); -let option1 = reactionRole.createOption("✅", "697809380137107478"); -let option2 = reactionRole.createOption("rifcat:720623460321198152", "708355720436777033"); -let option3 = reactionRole.createOption("cat1:720623437466435626", "703908514887761930"); +let option1 = system.createOption("✅", "697809380137107478"); +let option2 = system.createOption("rifcat:720623460321198152", "708355720436777033"); +let option3 = system.createOption("cat1:720623437466435626", "703908514887761930"); -reactionRole.createMessage("727272497157898372", "702115562158948432", 2, null, option1, option2, option3); +system.createMessage("727272497157898372", "702115562158948432", 2, null, option1, option2, option3); -reactionRole.init(); \ No newline at end of file +system.init(); \ No newline at end of file