-
Notifications
You must be signed in to change notification settings - Fork 0
/
seat.js
142 lines (99 loc) · 3.39 KB
/
seat.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
const axios = require('axios');
const _ = require('lodash');
const SeatApi = axios.create({
baseURL: process.env.SEAT_API_URL,
timeout: 10000,
headers: {
'X-Token' : process.env.SEAT_API_TOKEN,
}
});
const Seat = {
roles: [],
users: [],
process(command, character, roles, msg) {
switch (command) {
case '!sync':
this.getAllRoles(msg);
return this.getAllUsers(msg);
case '!roles':
return this.getAllRoles(msg);
case '!auth':
return this.addUserToRoles(character, roles, msg);
case '!deauth':
return this.removeUserFromRoles(character, roles, msg);
}
return;
},
async getAllUsers(msg) {
let {
data: {
data: users,
meta,
}
} = await SeatApi.get('/users');
this.users = users;
while (meta.current_page !== meta.last_page) {
let nextPage = meta.current_page + 1;
let response = await SeatApi.get(`/users?page=${nextPage}`);
meta = response.data.meta;
for (let user of response.data.data) {
this.users.push(user);
}
}
return msg.channel.send('Users synchronized with SeAT.');
},
async getAllRoles(msg) {
let req = await SeatApi.get('/roles');
this.roles = req.data;
if (msg.content !== '!roles')
return msg.channel.send('Roles synchronized with SeAT.');
let response = 'Available roles:```';
for (let r of this.roles) {
response = `${response}\n${r.title}`;
}
response = response + '```';
return msg.channel.send(response);
},
async addUserToRoles(character, roles, msg) {
if (!msg.member)
return msg.channel.send('This command must be run from a Discord server, not a PM.');
let isAuthorizedRequest = _.find(msg.member.roles.array(), { 'name': process.env.DISCORD_AUTHORIZED_ROLE_NAME });
if (!isAuthorizedRequest)
return msg.channel.send('You do not have the roles required to manage authorization');
let user = _.find(this.users, { 'name': character });
if (!user) {
return;
}
for (let roleName of roles) {
let role = _.find(this.roles, { 'title': roleName });
if (!role) {
return msg.channel.send(`Couldn't find a role by the name of ${roleName}. Try \`!roles\` to see the available roles.`);
}
await SeatApi.post(`/roles/groups`, {
group_id: user.group_id,
role_id: role.id
});
msg.channel.send(`Added ${user.name} to ${role.title}.`);
}
},
async removeUserFromRoles(character, roles, msg) {
if (!msg.member)
return msg.channel.send('This command must be run from a Discord server, not a PM.');
let isAuthorizedRequest = _.find(msg.member.roles.array(), { 'name': process.env.DISCORD_AUTHORIZED_ROLE_NAME });
if (!isAuthorizedRequest)
return msg.channel.send('You do not have the roles required to manage authorization');
let user = _.find(this.users, { 'name': character });
if (!user) {
return;
}
for (let roleName of roles) {
let role = _.find(this.roles, { 'title': roleName });
if (!role) {
return msg.channel.send(`Couldn't find a role by the name of ${roleName}. Try \`!roles\` to see the available roles.`);
}
await SeatApi.delete(`/roles/groups/${user.group_id}/${role.id}`);
msg.channel.send(`Removed ${user.name} from ${role.title}.`);
}
}
}
module.exports = Seat;