-
Notifications
You must be signed in to change notification settings - Fork 1
/
SlackConnector.js
203 lines (177 loc) · 10.2 KB
/
SlackConnector.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const { RTMClient } = require('@slack/rtm-api');
var config = require('./config.js');
const Helper = require('./Helper.js');
var helper = new Helper();
function SlackConnector(command_handler) {
this.command_handler = command_handler;
var bot_token = config.slack.token;
var channels = 'undefined' !== typeof config.slack.channels ? config.slack.channels : [];
var aliases = 'undefined' !== typeof config.slack.aliases ? config.slack.aliases : {};
var rtm = new RTMClient(bot_token);
var id = undefined;
var logtime = undefined;
var followed_items = {};
rtm.on('authenticated', function (rtmStartData) {
console.log('Logged in as '+rtmStartData.self.name+' of team '+rtmStartData.team.name);
id = rtmStartData.self.id;
});
// wait 3sec for not getting last message
setTimeout(function() {
rtm.on('message', function (message) {
console.log(message)
if (channels.length == 0 || channels.indexOf(message.channel) != -1) {
// The prefix is the bot's id
var prefix = '<@'+id+'>';
if ('undefined' === typeof message.subtype && 'undefined' !== typeof message.text) {
var text = message.text;
} else if ('undefined' !== typeof message.previous_message && 'undefined' !== typeof message.previous_message.text) {
text = message.previous_message.text;
}
var botReply = function(msgToSend) {
rtm.sendMessage(msgToSend, message.channel);
};
if (message.channel == 'DABKFF5J4') {
console.log("Private message");
text = prefix+" "+message.text;
}
if ('undefined' !== typeof text && text.startsWith(prefix)) {
// Getting the command in good shape
var command = text.substr(prefix.length, (text.length - prefix.length)).trim();
// Check if their is alias
command = 'undefined' !== typeof aliases[command] ? aliases[command] : command;
if (command.startsWith('install') && 'undefined' === typeof message.subtype) {
var argsInstall = command.split('install ').pop().split(' ');
console.log("Install command");
if (argsInstall.length >= 2) {
var url = argsInstall[0].replace('<','').replace('>','');
var sampleIdToInstall = argsInstall[1];
if (sampleIdToInstall.match(/^([a-zA-Z0-9_]{2,})$/)) {
if (argsInstall.length == 4) {
var startAt = argsInstall[2];
var endAt = argsInstall[3];
if (startAt.match(/^([0-9]{2}):([0-9]{2})$/) && endAt.match(/^([0-9]{2}):([0-9]{2})$/)) {
command_handler.updateSample(url, sampleIdToInstall, {start: startAt, end: endAt}, botReply);
}
} else {
command_handler.updateSample(url, sampleIdToInstall, null, botReply);
}
}
}
} else if (command.startsWith('uninstall') && 'undefined' === typeof message.subtype) {
console.log("Uninstall command");
var argsUninstall = command.split('uninstall ').pop().split(' ');
if (argsUninstall.length == 2) {
var sampleIdToUninstall = argsUninstall[0];
var softpassword = argsUninstall[1];
if (softpassword == 'jierluc') {
command_handler.deleteSample(sampleIdToUninstall, botReply);
}
}
} else if (command.startsWith('send') && 'undefined' === typeof message.subtype) {
console.log("Send command");
var argsPlay = command.split('send ').pop().split(' ');
if (argsPlay.length == 1) {
var sampleIdToPlay = argsPlay[0];
command_handler.playSample(sampleIdToPlay, 'slack', message.user, function() {
});
}
} else if (command.startsWith('list') && 'undefined' === typeof message.subtype) {
console.log("List command");
command_handler.listSamples(botReply);
console.log("Help command");
} else if (command.startsWith('help') && 'undefined' === typeof message.subtype) {
command_handler.helpForSamples(botReply);
} else if (command.startsWith('add') && 'undefined' === typeof message.subtype) {
var urls = command.split(' add ').pop().split(' ');
urls.forEach(function(value) {
var url = value.replace('<','').replace('>','');
// ########## ADD ##########
item = command_handler.addUrlToPlaylist(
url,
'slack',
message.user,
function() {
if ('undefined' !== followed_items[message.ts]) {
if (followed_items[message.ts].length > 0) {
} else {
delete followed_items[message.ts];
}
}
}
);
if ('undefined' !== typeof item && null != item) {
if ('undefined' === typeof followed_items[message.ts]) {
followed_items[message.ts] = []
}
followed_items[message.ts].push(item);
}
})
} else if (command == 'play' && 'undefined' === typeof message.subtype) {
command_handler.play();
} else if (command == 'pause' && 'undefined' === typeof message.subtype) {
command_handler.pause();
} else if (command.startsWith('volume ') && 'undefined' === typeof message.subtype) {
command_handler.volume(command.split(' ').pop());
} else if (command.startsWith('playing') && 'undefined' === typeof message.subtype) {
// ########## CURRENT ##########
if ('undefined' !== typeof command_handler.playlist.playing) {
rtm.sendMessage("The music currently playing is ''" + command_handler.playlist.playing.sound.title+"'. [" + helper.hmsTimeFormat(item.sound.length) + "]", message.channel);
} else {
rtm.sendMessage("No music playing (Set a music with the command 'add')", message.channel);
}
} else if (command.startsWith('playlist') && 'undefined' === typeof message.subtype) {
// ########## PLAYLIST ##########
if (command_handler.playlist.queue.length > 0) {
rtm.sendMessage( 'Total Length : [' + helper.hmsTimeFormat(command_handler.playlist.totalLength()) + ']\n' +
command_handler.playlist.queue.map(function(item) {
return ' - ' + item.sound.title + ' (:+1:='+item.like+' / :-1:='+item.dislike+')' + '[' + helper.hmsTimeFormat(item.sound.length) + ']' + ' by <@'+item.user_id+ '>';
}).join('\n'),
message.channel
);
} else {
rtm.sendMessage("No playlist set (Set a music with the command 'add')", message.channel);
}
} else if (command.startsWith('playnext') && 'undefined' === typeof message.subtype) {
command_handler.playNext();
} else if ('undefined' !== typeof message.subtype && 'message_deleted' == message.subtype) {
if (message.previous_message.ts in followed_items) {
command_handler.deleteFromPlaylist(followed_items[message.previous_message.ts], function() {
delete followed_items[message.ts];
})
}
} else {
var argsPlay = command.split(' ').pop();
command_handler.playSample(argsPlay, 'slack', message.user, function() {});
}
}
}
})
}, 3000);
rtm.on('reaction_added', function (reaction) {
if (reaction.item.type == 'message') {
if (reaction.item.ts in followed_items) {
if (reaction.reaction == '+1') {
command_handler.addLike(followed_items[reaction.item.ts]);
} else if (reaction.reaction == '-1') {
command_handler.addDislike(followed_items[reaction.item.ts]);
}
}
}
});
rtm.on('reaction_removed', function (reaction) {
if (reaction.item.type == 'message') {
if (reaction.item.ts in followed_items) {
if (reaction.reaction == '+1') {
command_handler.removeLike(followed_items[reaction.item.ts]);
} else if (reaction.reaction == '-1') {
command_handler.removeDislike(followed_items[reaction.item.ts]);
}
}
}
});
(async () => {
// Connect to Slack
const { self, team } = await rtm.start();
})();
}
module.exports = SlackConnector;