forked from haykam821/Discord-RoleReact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roleReact.js
68 lines (55 loc) · 2.54 KB
/
roleReact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Settings!
const yourID = ""; //Instructions on how to get this: https://redd.it/40zgse
const setupCMD = "!createrolemessage"
let initialMessage = `**React to the messages below to receive the associated role. If you would like to remove the role, simply remove your reaction!**`;
const roles = ["Hacker", "Artist", "Public Relations", "Intern"];
const reactions = ["💻", "🖌", "😃", "🆕"];
const botToken = ""; /*You'll have to set this yourself; read more
here https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token*/
//Load up the bot...
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login(botToken);
//If there isn't a reaction for every role, scold the user!
if (roles.length !== reactions.length) throw "Roles list and reactions list are not the same length!";
//Function to generate the role messages, based on your settings
function generateMessages(){
var messages = [];
messages.push(initialMessage);
for (let role of roles) messages.push(`React below to get the **"${role}"** role!`); //DONT CHANGE THIS
return messages;
}
bot.on("message", message => {
if (message.author.id == yourID && message.content.toLowerCase() == setupCMD){
var toSend = generateMessages();
let mappedArray = [[toSend[0], false], ...toSend.slice(1).map( (message, idx) => [message, reactions[idx]])];
for (let mapObj of mappedArray){
message.channel.send(mapObj[0]).then( sent => {
if (mapObj[1]){
sent.react(mapObj[1]);
}
});
}
}
})
bot.on('raw', event => {
if (event.t === 'MESSAGE_REACTION_ADD' || event.t == "MESSAGE_REACTION_REMOVE"){
let channel = bot.channels.get(event.d.channel_id);
let message = channel.fetchMessage(event.d.message_id).then(msg=> {
let user = msg.guild.members.get(event.d.user_id);
if (msg.author.id == bot.user.id && msg.content != initialMessage){
var re = `\\*\\*"(.+)?(?="\\*\\*)`;
var role = msg.content.match(re)[1];
if (user.id != bot.user.id){
var roleObj = msg.guild.roles.find('name', role);
var memberObj = msg.guild.members.get(user.id);
if (event.t === "MESSAGE_REACTION_ADD"){
memberObj.addRole(roleObj)
} else {
memberObj.removeRole(roleObj);
}
}
}
})
}
});