forked from JDegner0129/slack-etiquette-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (34 loc) · 1.46 KB
/
index.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
const { WebClient, RtmClient, MemoryDataStore, RTM_EVENTS, CLIENT_EVENTS } = require('@slack/client');
const token = process.env.SLACK_API_TOKEN || '';
const rtm = new RtmClient(token, { logLevel: 'error', dataStore: new MemoryDataStore() });
const web = new WebClient(token);
rtm.start();
const trySendMessageToChannel = ({ id, name, members }) => {
const channelMemberThreshold = process.env.CHANNEL_MEMBER_THRESHOLD || 20;
const channelBlacklist = (process.env.CHANNEL_BLACKLIST || '').split(',');
if (!members || members.length < channelMemberThreshold || channelBlacklist.includes(name)) {
return;
}
rtm.sendMessage(
`:wave: Hey there! You just used \`@here\` or \`@channel\` in a channel with ${members.length} members. Next time, consider giving your message a few minutes without the tag before tagging a large group (and maybe tag specific people!). :slightly_smiling_face:`,
id
);
};
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, () => {
console.log('RTM client authenticated!');
});
rtm.on(RTM_EVENTS.MESSAGE, ({ user, channel, text }) => {
if (!(text.includes('<!here|@here>') || text.includes('<!channel>'))) {
return;
}
let channelInfo = rtm.dataStore.getChannelById(channel);
if (!channelInfo) {
web.channels.info(channel).then(info => {
channelInfo = info.channel;
rtm.dataStore.setChannel(channelInfo);
trySendMessageToChannel(channelInfo);
});
} else {
trySendMessageToChannel(channelInfo);
}
});