Repository navigation
Expand file tree
/
Copy pathmessageHandler.js
More file actions
113 lines (111 loc) · 3.62 KB
/
Copy pathmessageHandler.js
File metadata and controls
113 lines (111 loc) · 3.62 KB
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
var Player = require('./player');
var config = require('./config');
var request = require('request');
var html_strip = require('htmlstrip-native').html_strip;
module.exports = function(connection, inputStream) {
var redis = require('redis');
var client = redis.createClient(config.get("redisOpts"));
client.subscribe("cyrclebot:commands", function() {
client.on("message", function(channel, command) {
MessageHandler({message: {message: command}});
});
});
var player = new Player(connection, inputStream);
var MessageHandler = function(data) {
if(!data.message.message || data.message.message === "")
return;
var msg = html_strip(data.message.message);
if(msg == "stop") {
player.stop();
}
else if(msg == "play" && !player.currentStream) {
player.next();
}
else if(msg == "skip") {
player.next();
}
else if(msg == "clear") {
player.clearQueue();
}
else if(msg == "shuffle") {
console.log("Shuffle");
player.shuffle();
}
else if(msg.indexOf("say") === 0) {
var line = msg.split("say")[1];
//player.say(line);
}
else if(msg.indexOf("vol") === 0) {
var newNum = msg.split(/volume|vol/)[1];
if(newNum) {
var vol = parseInt(newNum);
if(!isNaN(vol) && vol > 0 && vol < 101) {
player.setVolume(vol);
}
}
}
else {
var youtubeID = ytVidId(msg);
if(youtubeID) {
player.addToQueue(youtubeID);
}
else {
var youtubePlaylistID = ytPlaylistId(msg);
if(youtubePlaylistID) {
player.addPlaylistToQueue(youtubePlaylistID);
}
else { // not a youtube url, maybe it's a raw content file?
var simul = false;
/*
if(msg.indexOf("s ") === 0) {
simul = true;
msg = msg.split("s ")[1];
}*/
request.head(msg, function(error, response, body) {
if(error) {
console.log("Got error from attempt to identify via HEAD: %o", error);
console.log("Couldn't identify a contentType for url, no faucet assigned");
return;
}
console.log("Got HEAD response: %o", response.headers);
var type = response.headers["content-type"];
switch(type) {
case "video/mp4":
case "video/webm":
case "video/ogg":
case "application/ogg":
case "audio/mpeg":
case "audio/webm":
case "audio/ogg":
case "audio/wave":
case "audio/wav":
case "audio/x-wav":
case "audio/x-pn-wav":
case "\"application/octet-stream\";": // newgrounds identifies mp3s with this
console.log("Identified content type of %s", type);
player.addArbitraryMedia(msg, simul);
break;
default:
console.log("Got a contentType we don't know, returning null (%s)", type);
}
});
};
}
}
};
return MessageHandler;
};
/**
* JavaScript function to match (and return) the video Id
* of any valid Youtube Url, given as input string.
* @author: Stephan Schmitz <eyecatchup@gmail.com>
* @url: http://stackoverflow.com/a/10315969/624466
*/
function ytVidId(url) {
var p = /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?/;
return (url.match(p)) ? RegExp.$1 : false;
}
function ytPlaylistId(url) {
var p = /^.*(youtu.be\/|list=)([^#\&\?<]*)/;
return (url.match(p)) ? RegExp.$2 : false;
}