forked from bognix/sandbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
70 lines (62 loc) · 2.01 KB
/
release.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
const Promise = require('bluebird');
const { getSandboxNameFromMessage, getSandboxOwner } = require('./common');
const db = require('./db/connection');
function updateDbWithRelease(sandboxName, channel) {
return new Promise(((resolve, reject) => {
db.run(
"UPDATE sandboxes SET owner = '' WHERE team = $teamChannel AND sandbox = $sandboxName",
{ $teamChannel: channel, $sandboxName: sandboxName },
(err) => {
if (err) {
reject(err);
}
resolve();
},
);
}));
}
function releaseSandbox(message) {
let response;
let sandboxName;
return new Promise(((resolve, reject) => {
sandboxName = getSandboxNameFromMessage(message);
return getSandboxOwner(message.channel, sandboxName)
.then((sandboxOwner) => {
if (sandboxOwner.result && sandboxOwner.result !== message.user) {
response = `:pirate: take over!!! <@${sandboxOwner.result}>, <@${message.user}> is releasing your sandbox! :pirate:`;
return updateDbWithRelease(sandboxName, message.channel)
.then(() => {
resolve({
response,
data: {},
});
});
}
return updateDbWithRelease(sandboxName, message.channel)
.then(() => {
resolve({
data: {},
});
});
})
.catch((err) => { reject(err); });
}));
}
module.exports = {
pattern: /(zwalniam|releasing) (sandbox|adeng|neutron-api)-|^[zr] (sandbox|adeng|neutron-api)-/i,
action(rtm, message) {
releaseSandbox(message)
.then((data) => {
if (data.response) {
rtm.sendMessage(data.response, message.channel);
} else {
rtm.sendMessage(`<@${message.user}> :+1:`, message.channel);
}
}, (err) => {
rtm.sendMessage(`:x: sandbot error: \`${err}\`, try again`, message.channel);
})
.catch((err) => {
rtm.sendMessage(`:x: sandbot error: \`${err}\`, try again`, message.channel);
});
},
};