Skip to content

Commit

Permalink
Save messages and import them from database
Browse files Browse the repository at this point in the history
  • Loading branch information
Barış Demirci authored and repl.it user committed Jul 8, 2020
1 parent 87b48d2 commit 611cf34
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 13 deletions.
49 changes: 46 additions & 3 deletions classes/ReactionRole.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
const { Client } = require("discord.js");
const SuperError = require("./SuperError");
const FileManager = require("./FileManager");
const SuperError = require("./SuperError");
const Mongoose = require("mongoose");

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

self.token = token;
self.mongoURL = mongoURL;
self.config = [];
self.client = new Client();
self.fileManager = FileManager;

if (self.mongoURL) {
self.rrModel = require("../models/rrModel");

Mongoose.connect(self.mongoURL, {
useNewUrlParser: true,
useUnifiedTopology: true
});

self.database = Mongoose.connection;

self.database.on("error", (err) => {
throw new SuperError("DataBaseError", err.toString());
});

self.database.createMessage = async(rr) => {
let exists = await self.rrModel.findOne({
messageID: rr.messageID
});
if (exists) {
await self.rrModel.updateOne({
messageID: rr.messageID
}, {
channelID: rr.channelID,
limit: rr.limit,
restrictions: rr.restrictions,
reactions: rr.reactions
});
} else {
exists = new self.rrModel(rr);
await exists.save();
}
}

self.database.deleteMessage = async(messageID) => {
await self.rrModel.deleteOne({
messageID
});
}
}

self.createOption = (...arguments) => require("../methods/createOption")(...arguments);
self.createMessage = (...arguments) => require("../methods/createMessage")(self, ...arguments);
self.createMessage = async(...arguments) => await require("../methods/createMessage")(self, ...arguments);
self.deleteMessage = async (messageID, channelID) => await require("../methods/deleteMessage")(self, messageID, channelID);
self.init = async () => await require("../methods/init")(self);
self.reInit = async () => await require("../methods/reInit")(self);
self.importConfig = (config) => require("../methods/importConfig")(self, config);
Expand Down
32 changes: 29 additions & 3 deletions methods/createMessage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
module.exports = (master, ...arguments) => {
const SuperError = require("../classes/SuperError");

module.exports = async(self, ...arguments) => {
let reactions = [];
for (let i = 4; i < arguments.length; i++) reactions.push(arguments[i]);
master.config.push({
let message = {
"messageID": arguments[0],
"channelID": arguments[1],
"limit": arguments[2],
"restrictions": arguments[3],
"reactions": reactions
});
}
if (self.client.user) {
let msg = await self.client.channels.cache.get(message.channelID).messages.fetch(message.messageID).catch(err => {
throw new SuperError("CanNotFetchMesssage", err.toString());
});
if (!msg) throw new SuperError("CanNotFetchMesssage", err.toString());

for (let { emoji } of reactions) {
emoji = require("./cleanEmoji")(emoji);
let messageReaction = msg.reactions.cache.get(emoji);
if (!messageReaction) await msg.react(emoji).catch((err) => {
throw new SuperError("CanNotReactMesssage", err.toString());
});
else {
if (!messageReaction.me) {
messageReaction.users.fetch();
await msg.react(emoji).catch((err) => {
throw new SuperError("CanNotReactMesssage", err.toString());
});
};
};
};
if (self.mongoURL) self.database.createMessage(message);
}
self.config.push(message);
};
13 changes: 13 additions & 0 deletions methods/deleteMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = async(self, messageID, channelID) => {
self.config = self.config.filter(rr => {
return rr.messageID != messageID;
});
if (self.client.user) {
let message = self.client.channels.cache.get(channelID).messages.cache.get(messageID).reactions.cache.forEach(async reaction => {
await reaction.users.remove(self.client.user.id).catch((err) => {
throw new SuperError("CanNotRemoveReaction", err.toString());
});
});
if (self.mongoURL) self.database.deleteMessage(messageID);
}
}
20 changes: 17 additions & 3 deletions methods/init.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
const SuperError = require("../classes/SuperError");
module.exports = async (self) => {
await self.client.login(self.token).catch((err) => {
throw new SuperError("InvalidToken", "Please specify a valid BOT token.");
});

if (self.mongoURL) {
self.database.once("open", async() => {
self.config.forEach(async rr => {
await self.database.createMessage(rr);
});
let savedConfig = await self.rrModel.find();
self.importConfig(savedConfig);
await self.client.login(self.token).catch((err) => {
throw new SuperError("InvalidToken", "Please specify a valid BOT token.");
});
});
} else {
await self.client.login(self.token).catch((err) => {
throw new SuperError("InvalidToken", "Please specify a valid BOT token.");
});
}

self.client.on("ready", async () => {
console.info("[ReactionRole] Fetching messages");
Expand Down
23 changes: 23 additions & 0 deletions models/rrModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Mongoose = require("mongoose");

const rrSchema = new Mongoose.Schema({
messageID: {
type: String,
required: true
},
channelID: {
type: String,
required: true
},
limit: {
type: Number,
required: true
},
restrictions: Object,
reactions: {
type: Object,
required: true
}
});

module.exports = Mongoose.model("rrModel", rrSchema, "REACTION_ROLE_MODELS");
Loading

0 comments on commit 611cf34

Please sign in to comment.