-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrole-manager.js
executable file
·67 lines (50 loc) · 2.93 KB
/
role-manager.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
const store = require('data-store');
const roleManagerData = new store({ path: __dirname+'/../role-manager-data.json' });
new Module('role manager', 'react', {}, function (message,user,reaction) {
if (!reaction) throw new Error('reaction is null');
let ROLEMANAGERS = roleManagerData.get();
//get role manager based on the message id
let roleManager = Object.keys(ROLEMANAGERS).find((k,i) => ROLEMANAGERS[k].messageId == reaction._emoji.reaction.message.id);
if (!roleManager) return 'CONTINUE';
roleManager = ROLEMANAGERS[roleManager];
let matchedReaction = roleManager.roles.find(r => r.emoji == reaction._emoji.id || r.emoji == reaction._emoji.name);
//if the reaction wasn't matched - remove it and exit
if (!matchedReaction) {
//remove the reaction
reaction._emoji.reaction.remove(message.user)
.then(e=>log({module: 'role manager'},'reaction removed: ',reaction._emoji.name,'['+reaction._emoji.id+']','by',user.username))
.catch(e=>log({module: 'role manager', error: new Error('failed reaction removal '+reaction._emoji.name+' ['+reaction._emoji.id+'] by'+user.username)}));
return;
}
//get the "guild member" (user relative to server), and add the role to them
reaction.message.channel.guild.members.fetch(user.id)
.then(guildMember=> {
//give user role base on matched emoji
guildMember.roles.add(matchedReaction.role)
.then(e=>log({module: 'role manager'},'gave',user.username,matchedReaction.name,'role'))
.catch(e=>log({module: 'role manager', error: new Error('maybe failed to give '+user.username+' '+matchedReaction.name+'role')}));
})
.catch(d => log({module: 'role manager', error: new Error('failed to get guild member')}));
});
new Module('role manager', 'unreact', {}, function (message,user,reaction) {
if (!message) return;
if (!reaction) throw new Error('reaction is null');
let ROLEMANAGERS = roleManagerData.get();
//get role manager based on the message id
let roleManager = Object.keys(ROLEMANAGERS).find((k,i) => ROLEMANAGERS[k].messageId == reaction._emoji.reaction.message.id);
if (!roleManager) return 'CONTINUE';
roleManager = ROLEMANAGERS[roleManager];
let matchedReaction = roleManager.roles.find(r => r.emoji == reaction._emoji.id || r.emoji == reaction._emoji.name);
//if the reaction wasn't matched - this probably can't happen, but just ignore it
if (!matchedReaction) return;
//get the "guild member" (user relative to server), and add the role to them
reaction.message.channel.guild.members.fetch(user.id)
.then(guildMember=> {
//give user role base on matched emoji
guildMember.roles.remove(matchedReaction.role)
.then(e=>console.log('\tremoved',user.username,matchedReaction.name,'role'))
.catch(e=>console.log('\tmaybe failed to remove',user.username,matchedReaction.name,'role'));
})
.catch(d => console.log('failed to get guild member',d));
});
/*global Module, CONFIG, client, guild, log, error, send, react, sendEmoji, pickRandom, messageHasBotPing, isMod */