Skip to content

Commit

Permalink
Merge pull request #19 from barbarbar338/dev-_config-support_
Browse files Browse the repository at this point in the history
Dev  config support 
Still in development
  • Loading branch information
Barış Demirci authored Jul 8, 2020
2 parents 675704d + 8d7442b commit 87b48d2
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 7 deletions.
30 changes: 30 additions & 0 deletions classes/FileManager.js
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}
4 changes: 4 additions & 0 deletions classes/ReactionRole.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const { Client } = require("discord.js");
const SuperError = require("./SuperError");
const FileManager = require("./FileManager");

module.exports = function ReactionRole(token) {
self = this;

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);
};
29 changes: 29 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
]
4 changes: 4 additions & 0 deletions methods/exportConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (self, file) => {
self.fileManager.checkFile(file);
self.fileManager.writeFile(file, self.config);
};
3 changes: 3 additions & 0 deletions methods/importConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (self, config) => {
self.config = self.config.concat(config)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 6 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -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();
system.init();

0 comments on commit 87b48d2

Please sign in to comment.