-
Notifications
You must be signed in to change notification settings - Fork 9
/
leaderAdd.js
57 lines (46 loc) · 1.61 KB
/
leaderAdd.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
import { getInfoForUser, airPatch, transcript, airFind } from '../utils'
const interactionLeaderAdd = async (bot, message) => {
const { user, text, channel } = message
const taggedUserID = (text.match(/\<@(.*)\|/) || [])[1]
if (!taggedUserID) {
bot.replyPrivateDelayed(message, transcript('leaderAdd.help'))
return
}
const taggedUser = await getInfoForUser(taggedUserID)
const commandUser = await getInfoForUser(user)
const recipientClub = await airFind('Clubs', 'Slack Channel ID', channel)
if (!commandUser.club && !commandUser.permissionedAmbassador) {
throw transcript('leaderAdd.invalidClub')
}
if (!recipientClub) {
throw transcript('leaderAdd.clubNotFound')
}
if (commandUser.club && commandUser.club.id != recipientClub.id) {
// A leader is trying to permission someone to a channel that's not their
// club channel
throw transcript('leaderAdd.invalidChannel')
}
const taggedUserClubs = taggedUser.person.fields['Clubs'] || []
if (taggedUserClubs.includes(recipientClub)) {
throw transcript('leaderAdd.alreadyLeader')
}
await airPatch('People', taggedUser.person.id, {
Clubs: [...taggedUserClubs, recipientClub.id],
})
// invite leader to #leaders and their club channel
bot.api.conversations.invite({
token: bot.config.bot.access_token,
channel,
users: taggedUserID
})
bot.api.conversations.invite({
token: bot.config.bot.access_token,
channel: 'GAE0FFNFN',
users: taggedUserID
})
bot.replyPrivateDelayed(
message,
transcript('leaderAdd.success', { taggedUserID, channel })
)
}
export default interactionLeaderAdd