-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdm.js
71 lines (63 loc) · 2.04 KB
/
dm.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
import { getInfoForUser, transcript, airFind, reaction } from '../utils'
import interactionJoinChannel from './joinChannel'
const substitutions = (text, targetChannel) =>
new Promise((resolve, reject) => {
const pocRegex = /@poc/
if (text.match(pocRegex)) {
airFind('Clubs', 'Slack Channel ID', targetChannel)
.then(club =>
airFind('People', `'${club.fields.POC}' = RECORD_ID()`).then(poc => {
if (!poc || !poc.fields) {
reject(new Error('No POC for club'))
}
if (!poc.fields['Slack ID']) {
reject(new Error('No Slack ID set for POC'))
}
resolve(text.replace(pocRegex, `<@${poc.fields['Slack ID']}>`))
})
)
.catch(reject)
} else {
resolve(text)
}
})
const interactionDM = async (bot, message) => {
try {
const { user, text } = message
const { slackUser } = await getInfoForUser(user)
if (!slackUser.is_admin) {
throw new Error('This command is admin only')
}
const encodedText = text
.replace('<', '<text')
.replace('>', '>')
.replace(/@_/, '@')
const messageRegex = /dm <.*?[@#](.+?(?=[>\|])).*?>\s*((?:.|\s)*)/
const [_, targetChannel, targetMessage] = encodedText.match(messageRegex)
if (targetChannel[0].toLowerCase() == 'c') {
try {
await interactionJoinChannel(bot, {channel: targetChannel})
} catch (e) {
// oh well...
}
}
return substitutions(targetMessage, targetChannel).then(
substitutedMessage => {
bot.say(
{ text: substitutedMessage, channel: targetChannel },
(err, response) => {
if (err) {
throw err
}
reaction(bot, 'add', message.channel, message.ts, 'white_check_mark')
}
)
}
)
} catch (err) {
console.error(err)
bot.reply(message, transcript('errors.general', { err }))
reaction(bot, 'add', message.channel, message.ts, 'no_entry')
}
}
export default interactionDM