-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbots.js
62 lines (57 loc) · 1.67 KB
/
bots.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
const { apply } = require('async')
const metamapBot = require('./botCode')
const ChannelSetting = require('./models/ChannelSetting')
// will store the bot instances that are running
const bots = {}
// setup a function for persisting channel settings
function persistChannelSetting (serverType, id, channelSettings, channelId, mapId, metacodeId, capture) {
let channelSetting = channelSettings.find(cS => cS.get('channel_id') === channelId)
if (!channelSetting) {
channelSetting = new ChannelSetting({
channel_id: channelId,
team_id: id,
server_type: serverType
})
channelSettings.push(channelSetting)
}
channelSetting.capture = capture
channelSetting.map_id = mapId
channelSetting.metacode_id = metacodeId
channelSetting.save()
}
// setup a function which will spin up a bot for a team
async function startBot(serverType, botConfig, tokens = {}, mmUserIds = {}, channelSettings = []) {
let id
if (serverType === 'slack') {
id = botConfig.get('team_id')
} else if (serverType === 'mattermost') {
id = botConfig._id
}
const channelSettingsObj = {}
channelSettings.forEach(function (cS) {
channelSettingsObj[cS.get('channel_id')] = {
map: cS.get('map_id'),
metacode: cS.get('metacode_id'),
capture: cS.capture
}
})
try {
bots[id] = await metamapBot.setup(
serverType,
botConfig,
tokens,
mmUserIds,
channelSettingsObj,
apply(persistChannelSetting, serverType, id, channelSettings)
)
} catch (err) {
console.error(err)
}
}
module.exports = {
persistChannelSetting: persistChannelSetting,
startBot: startBot,
getBot: function (id) {
return bots[id]
}
}