Skip to content

Commit

Permalink
Huge update
Browse files Browse the repository at this point in the history
  • Loading branch information
Barış Demirci authored and repl.it user committed Jul 5, 2020
1 parent 6e0f19b commit b372185
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 78 deletions.
80 changes: 36 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Discord ReactionRole System
=================

<p><b>ReactionRole</b> is a module that allows you to create Discord reaction role easily! It also has several functions for you to use.</p>
<p>This module is compatible with all node.js discord modules (like discord.js, eris, discord.js-commando etc.)</p>
<p><b>ReactionRole</b> is a module that allows you to create Discord reaction role easily!</p>
<p>This module is compatible with all node.js discord wrappers (like discord.js, eris, discord.js-commando etc.)</p>
<p>You also don't need to write any bot code if you want! You can also use this module alone. You just need a Discord Bot Token!</p>

<b>[Discord: https://discord.com/invite/BjEJFwh](https://discord.com/invite/BjEJFwh)</b>
Expand All @@ -16,59 +16,51 @@ Usage
<p>Here is a simple but effective example! (if you are using discord.js)</p>

```js
/* Discord.js Packages */
const Discord = require("discord.js");
const client = new Discord.Client();

/* Reaction Role Packages */
const ReactionRole = require("reaction-role");
const reactionRole = new ReactionRole("TOKEN");

/**
* Creating Options
* You can add unlimited amounts of role ID's
* Example: let option = reactionRole.createOption("EMOJI", "ROLE_ID_1", "ROLE_ID_2", "ROLE_ID_3", ...);
* EMOJI must be a normal emoji like "✅" or custom emoji like "spotify:598532266515496970"
* ROLE_ID must be a Snowflake like "606046163564494859" (a normal role ID)
*/
let option1 = reactionRole.createOption("", "606046163564494859", "604212225493696512");
let option2 = reactionRole.createOption("spotify:598532266515496970", "604212225493696512", "606046163564494859");
// ReactionRole Packages
const ReactionRole = require(".");
const system = new ReactionRole("TOKEN");

/**
* Creating New Reaction Role Message
* You can add unlimited amounts of option
* Example: reactionRole.create("MESSAGE_ID", "CHANNEL_ID", limited?, option1, option2, option3, ...);
* MESSAGE_ID must be a Snowflake like "678345974460186651" (a normal channel ID)
* CHANNEL_ID must be a Snoflake like "675657998907211787" (a normal channel ID)
* limited? must be a boolean like true or false
* option must be a reactionRole option like "reactionRole.createOption("EMOJI", "ROLE_ID_1", "ROLE_ID_2", "ROLE_ID_3", ...)"
*/
reactionRole.createMessage("678345974460186651", "675657998907211787", true, option1, option2);
* Creating Options
*
* Example: system.createOption(EMOJI, ROLE_ID_1, ROLE_ID_2, ROLE_ID_3 ...);
* EMOJI: Emoji to be reacted (e.g: "✅", "rifcat:720623460321198152", "a:cat1:720623437466435626")
* ROLE_ID: ID of the role to be given (e.g: "697809380137107478") (you can add unlimited amount of roles)
*/
let option1 = system.createOption("", "697809380137107478");
let option2 = system.createOption("rifcat:720623460321198152", "708355720436777033");
let option3 = system.createOption("cat1:720623437466435626", "703908514887761930");

/**
* Initialize The System
* This line of code runs the whole system
* Example: reactionRole.init();
*/
reactionRole.init();

/**
* ReInitialize The System
* This line of code runs the whole system
* Example: reactionRole.init();
*/
reactionRole.reInit();
* Creating Message
*
* Example: system.createMessage(MESSAGE_ID, CHANNEL_ID, LIMIT, RESTRICTIONS, OPTION_1, OPTION_2, OPTION_3 ...);
* MESSAGE_ID: Message to be reacted (e.g: "727272497157898372")
* CHANNEL_ID: Channel with the message you specified (e.g "702115562158948432")
* LIMIT: Role limit that can be taken (e.g 1, 3, 7)
* RESTRICTIONS: Permissions required to use the system (e.g null, [ "BAN_MEMBERS" ]) (Type "null" to make it public)
* OPTION: ReactionRole Option (system.createOption(EMOJI, ROLE_ID_1, ROLE_ID_2, ROLE_ID_3 ...)) (you can add unlimited amount of options)
*/
system.createMessage("727272497157898372", "702115562158948432", 2, null, option1, option2, option3);

/* Initializing system */
system.init();

/* ReInitializing system (NOT REQUIRED)*/
system.reInit();
```

<p>It looks so scary right :D Don't worry here is a simpler system</p>

```js
const ReactionRole = require("reaction-role");
const ReactionRole = require(".");
const reactionRole = new ReactionRole("TOKEN");

let option1 = reactionRole.createOption("", "606046163564494859", "604212225493696512");
let option2 = reactionRole.createOption("spotify:598532266515496970", "604212225493696512", "606046163564494859");
reactionRole.createMessage("678345974460186651", "675657998907211787", true, option1, option2);
let option1 = reactionRole.createOption("", "697809380137107478");
let option2 = reactionRole.createOption("rifcat:720623460321198152", "708355720436777033");
let option3 = reactionRole.createOption("a:cat1:720623437466435626", "703908514887761930");

reactionRole.createMessage("727272497157898372", "702115562158948432", 2, null, option1, option2, option3);

reactionRole.init();
```
Expand Down
2 changes: 1 addition & 1 deletion classes/ReactionRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function ReactionRole(token) {
self.token = token;
self.config = [];
self.client = new Client();

self.createOption = (...arguments) => require("../methods/createOption")(...arguments);
self.createMessage = (...arguments) => require("../methods/createMessage")(self, ...arguments);
self.init = async () => await require("../methods/init")(self);
Expand Down
6 changes: 3 additions & 3 deletions methods/cleanEmoji.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const regEx = /[A-Za-z0-9_]+:[0-9]+/;

module.exports = (emoji) => {
let cleaned = regEx.exec(emoji);
if (cleaned) return cleaned[0];
return emoji;
let cleaned = regEx.exec(emoji);
if (cleaned) return cleaned[0];
return emoji;
};
4 changes: 2 additions & 2 deletions methods/getEmoji.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (emoji) => {
if (emoji.id) return `${emoji.name}:${emoji.id}`;
else return emoji.name;
if (emoji.id) return `${emoji.name}:${emoji.id}`;
else return emoji.name;
};
41 changes: 23 additions & 18 deletions methods/importEvents.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
const SuperError = require("../classes/SuperError");

module.exports = async (self) => {
let { client, config } = self;

client.on("messageReactionAdd", async(reaction, user) => {
if (client.user.equals(user)) return;
self.client.on("messageReactionAdd", async (reaction, user) => {
if (self.client.user.equals(user)) return;
let member = reaction.message.guild.members.cache.get(user.id);
if (!member) return;

let cleanEmoji = require("./getEmoji")(reaction.emoji);

for (let { messageID, channelID, reactions, limit, restrictions } of config) {
for (let { messageID, channelID, reactions, limit, restrictions } of self.config) {
if (channelID != reaction.message.channel.id) continue;
if (messageID != reaction.message.id) continue;
if (restrictions && !member.permissions.has(restrictions)) {
if (restrictions && member.permissions.has(restrictions)) {
console.log(restrictions)
await reaction.users.remove(user.id).catch((err) => {
throw new SuperError("CanNotRemoveReaction", err.toString());
});
continue;
}
continue;
};

let addRole = [];

for (let role of member.roles.cache.keys()) addRole.push(role);

let whitelist = [];
let blacklist = [];

for (let { emoji, roles } of reactions) {
if (cleanEmoji == emoji) whitelist.push.apply(whitelist, roles);
else blacklist.push.apply(blacklist, roles);
};

let reactionSize = reaction.message.reactions.cache.filter(reaction => {
return reaction.users.cache.has(user.id);
}).size;
let configEmojis = reactions.map(r => {
return r.emoji
});

let reactionSize = reaction.message.reactions.cache
.filter(r => {
return configEmojis.includes(require("./getEmoji")(r.emoji));
})
.filter(r => {
return r.users.cache.has(user.id);
}).size;

if (reactionSize > limit) {
await reaction.users.remove(user.id).catch((err) => {
throw new SuperError("CanNotRemoveReaction", err.toString());
});
continue;
}
};

addRole.push.apply(addRole, whitelist);

Expand All @@ -52,14 +59,12 @@ module.exports = async (self) => {
};
});



client.on("messageReactionRemove", async(reaction, user) => {
if (client.user.equals(user)) return;
self.client.on("messageReactionRemove", async (reaction, user) => {
if (self.client.user.equals(user)) return;
let member = reaction.message.guild.members.cache.get(user.id);
let cleanEmoji = require("./getEmoji")(reaction.emoji);

for (let { messageID, channelID, reactions } of config) {
for (let { messageID, channelID, reactions } of self.config) {
if (channelID != reaction.message.channel.id) continue;
if (messageID != reaction.message.id) continue;

Expand Down
12 changes: 6 additions & 6 deletions methods/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = async (self) => {
throw new SuperError("InvalidToken", "Please specify a valid BOT token.");
});

self.client.on("ready", async() => {
console.log("[ReactionRole] Fetching messages");
self.client.on("ready", async () => {
console.info("[ReactionRole] Fetching messages");

for (let { channelID, messageID, reactions } of self.config) {
let message = await self.client.channels.cache.get(channelID).messages.fetch(messageID).catch(err => {
Expand All @@ -17,8 +17,8 @@ module.exports = async (self) => {
emoji = require("./cleanEmoji")(emoji);
let messageReaction = message.reactions.cache.get(emoji);
if (!messageReaction) await message.react(emoji).catch((err) => {
throw new SuperError("CanNotReactMesssage", err.toString());
});
throw new SuperError("CanNotReactMesssage", err.toString());
});
else {
if (!messageReaction.me) {
messageReaction.users.fetch();
Expand All @@ -27,9 +27,9 @@ module.exports = async (self) => {
});
};
};
};
};
};
await require("./importEvents")(self);
console.log("[ReactionRole] Fetched messages, system is ready!");
console.info("[ReactionRole] Fetched messages, system is ready!");
});
};
5 changes: 3 additions & 2 deletions methods/reInit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { Client } = require("discord.js");

module.exports = (self) => {
module.exports = async (self) => {
console.info("[ReactionRole] ReInitializing System!");
self.client.destroy();
self.client = new Client();
require("./init")(self);
await require("./init")(self);
}
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const reactionRole = new ReactionRole("TOKEN");

let option1 = reactionRole.createOption("✅", "697809380137107478");
let option2 = reactionRole.createOption("rifcat:720623460321198152", "708355720436777033");
let option3 = reactionRole.createOption("eywreyiz:720623464507244576", "703908514887761930");
let option3 = reactionRole.createOption("cat1:720623437466435626", "703908514887761930");

reactionRole.createMessage("727272497157898372", "702115562158948432", 2, [ "MANAGE_GUILD" ], option1, option2, option3);
reactionRole.createMessage("727272497157898372", "702115562158948432", 2, null, option1, option2, option3);

reactionRole.init();

0 comments on commit b372185

Please sign in to comment.