This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroles.js
172 lines (146 loc) · 4.75 KB
/
roles.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const chalk = require('chalk');
const guildAssignableRolesSchema = require('./classes/guild-assignable-roles-schema');
module.exports = Kirbi => {
Kirbi.Config.roles = [];
require('./lib/on-event')(Kirbi);
function createEmbed(info) {
const ret = {
embed: {
color: Kirbi.Config.discord.defaultEmbedColor,
author: {
name: 'Role Manager',
icon_url: 'https://emojipedia-us.s3.amazonaws.com/thumbs/120/google/110/lock_1f512.png'
},
description: info
}
};
return ret;
}
return {
commands: [
'iam',
'iamnot',
'role'
],
iam: {
usage: '',
description: '',
process: (msg, suffix, isEdit, cb) => {
if (!isEdit) {
const role = msg.guild.roles.find(r => r.name.toLowerCase() === suffix);
const guildRoles = Kirbi.Config.discord.roles[msg.guild.id];
if (guildRoles) {
const roles = guildRoles.assignable;
if (roles && roles.includes(role.id)) {
msg.member.addRole(role.id);
cb(createEmbed(`Added ${role.name} to your roles.`), msg);
}
}
}
}
},
iamnot: {
usage: '',
description: '',
process: (msg, suffix, isEdit, cb) => {
if (!isEdit) {
const role = msg.guild.roles.find(r => r.name.toLowerCase() === suffix);
const guildRoles = Kirbi.Config.discord.roles[msg.guild.id];
if (role && guildRoles) {
const roles = guildRoles.assignable;
if (roles && roles.includes(role.id)) {
msg.member.removeRole(role.id);
cb(createEmbed(`Removed ${role.name} from your roles.`), msg);
}
}
}
}
},
role: {
usage: 'add|remove|default|list <role name>',
description: '',
process: (msg, suffix, isEdit, cb) => {
if (!isEdit) {
const guildAssignableRolesSet = Kirbi.Database.model('GuildAssignableRolesSchema', guildAssignableRolesSchema);
const params = suffix.split(' ');
const command = params.shift();
suffix = params.join(' ');
let guildRoles = Kirbi.Config.discord.roles[msg.guild.id];
if (!guildRoles) {
Kirbi.Config.discord.roles[msg.guild.id] = {};
Kirbi.Config.discord.roles[msg.guild.id].assignable = [];
guildRoles = Kirbi.Config.discord.roles[msg.guild.id];
}
const roles = guildRoles.assignable;
switch (command) {
case 'add':
if (msg.member.hasPermission('MANAGE_ROLES')) {
try {
const role = msg.guild.roles.find(r => r.name.toLowerCase() === suffix);
if (role) {
if (!roles.includes(role.id)) {
guildAssignableRolesSet.findOneAndUpdate({ guildId: msg.guild.id }, { $addToSet: { roleIds: role.id } }, { upsert: true }, (err, result) => {
if (err) {
chalk.red(err);
cb(`Error saving role.`, msg);
} else {
if (!result) {
result = guildAssignableRolesSchema;
result.roleIds = [role.id];
}
result.save();
console.log(chalk.blue(`Added ${role.name}:${role.id} on ${msg.guild.name}:${msg.guild.id} to self-assignable list.`));
Kirbi.Config.discord.roles[msg.guild.id].assignable.push(role.id);
cb(`Added role <@${role.id}> to self-assignable list.`, msg);
}
});
} else {
cb(createEmbed(`Already assigned.`), msg);
}
} else {
cb(createEmbed(`Not a valid role.`), msg);
}
} catch (err) {
console.log(chalk.red(`Error: ${err}`));
}
}
break;
case 'remove':
if (msg.member.hasPermission('MANAGE_ROLES')) {
try {
const role = msg.guild.roles.find(r => r.name.toLowerCase() === suffix);
if (role) {
if (!roles.includes(role.id)) {
guildAssignableRolesSet.update({ guildId: msg.guild.id }, { $pullAll: { roleIds: role.id } }, err => {
if (err) {
console.log(chalk.red(`Error: ${err}`));
cb(`Error removing role.`, msg);
} else {
console.log(chalk.blue(`Removed ${role.name}:${role.id} on ${msg.guild.name}:${msg.guild.id} from self-assignable list.`));
Kirbi.Config.discord.roles[msg.guild.id].assignable.splice(Kirbi.Config.roles[msg.guild.id].assignable.indexOf(role.id), 1);
cb(createEmbed(`Removed <@${role.id}> from self-assignable list.`), msg);
}
});
} else {
cb(createEmbed(`This role is not self-assignable.`), msg);
}
} else {
cb(createEmbed(`Not a valid role.`), msg);
}
} catch (err) {
console.log(chalk.red(`Error ${err}`));
}
}
break;
case 'default':
break;
case 'list':
break;
default:
break;
}
}
}
}
};
};