Last active
December 5, 2015 23:50
-
-
Save vixus0/4ae522db04998dbeb07d to your computer and use it in GitHub Desktop.
DJ Fatman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Play youtube audio to mumble. | |
*/ | |
(function() { | |
var cmdre = /!(\w+) *(.*)/; | |
var conre = /connect ([A-Za-z0-9.:]+); *password (.*)/; | |
var file = '/tmp/mjtmp'; | |
var queue = []; | |
var help = { | |
'play': 'Play first YouTube result. Queues if something already playing.', | |
'stop': 'Stops song and clears queue.', | |
'next': 'Skip current song.', | |
'queue': 'Display current song queue.', | |
'vol': 'Set the current volume', | |
'say': 'Speak like Stephen Hawking', | |
'help': 'Display this message.', | |
'test': 'SPOOKY SPOOKY SKELETONS' | |
}; | |
function message(text) { | |
piepan.Self.Channel.Send(text, false); | |
} | |
function delFile() { | |
piepan.Process.New(function (ok, out) { | |
if (ok) console.log('Removed '+file); | |
}, 'rm', file); | |
} | |
function getYoutubeInfo(data) { | |
var seconds = data.duration; | |
var minutes = Math.floor(seconds / 60).toFixed(0).toString(); | |
seconds = (seconds % 60).toFixed(0); | |
var duration = minutes + ":"; | |
if (seconds < 10) { | |
duration += "0"; | |
} | |
duration += seconds; | |
return { | |
id: data.id, | |
title: data.title, | |
duration: duration, | |
}; | |
} | |
var actions = {}; | |
function playSong(info) { | |
var url = info.id; | |
console.log('[play] Attempting url '+url); | |
piepan.Process.New(function(ok, out) { | |
if (ok) { | |
message('Playing: '+info.title+' requested by '+info.who); | |
console.log('[play] File downloaded, playing.'); | |
piepan.Audio.Stop(); | |
piepan.Audio.Play({filename: file, callback: endSong}); | |
console.log('[play] Playing '+file); | |
} else { | |
console.log('[play] Error: '+out); | |
message('Could not play.'); | |
} | |
}, 'youtube-dl', '--no-continue', '--no-part', '-f', 'bestaudio', '-o', file, url); | |
} | |
function endSong() { | |
if (queue.length > 0) { | |
playSong(queue.shift()); | |
} | |
} | |
actions.play = function(terms, sender) { | |
if (sender.Muted || sender.Deafened) { | |
message('Ignoring '+sender.Name); | |
return; | |
} | |
piepan.Process.New(function (ok, out) { | |
if (ok) { | |
console.log('[search] Curl request successful.'); | |
var first = out.split('\n')[0]; | |
var parts = first.split(' '); | |
var info = { | |
who : sender.Name, | |
id : parts[0], | |
title : parts.slice(1).join(' ') | |
}; | |
if (info.id) { | |
if (piepan.Audio.IsPlaying()) { | |
queue.push(info); | |
message(info.who+' queued '+info.title); | |
} else { | |
playSong(info); | |
} | |
} | |
} else { | |
console.log('[search] Error'); | |
} | |
}, './yturls', terms); | |
} | |
actions.stop = function () { | |
if (piepan.Audio.IsPlaying()) { | |
piepan.Audio.Stop(); | |
delFile(); | |
queue = []; | |
message('Stopped'); | |
console.log('[stop] queue cleared'); | |
} else { | |
message('Nothing playing!'); | |
} | |
} | |
actions.next = function () { | |
endSong(); | |
} | |
actions.queue = function () { | |
for (var song in queue) { | |
message('['+song.who+'] '+song.title); | |
} | |
} | |
actions.vol = function (vol) { | |
var f = parseFloat(vol); | |
if (f) { | |
var v = Math.max(0.0, Math.min(1.0, f)); | |
piepan.Audio.SetVolume(v); | |
message('Volume: '+piepan.Audio.Volume().toFixed(2).toString()); | |
console.log('[volume] Change: '+vol); | |
} | |
} | |
actions.say = function (text) { | |
piepan.Process.New(function(ok, out) { | |
if (ok) { | |
if (!piepan.Audio.IsPlaying()) { | |
piepan.Audio.Play({filename: '/tmp/mjtmp'}) | |
} | |
} | |
}, 'espeak', '-w', '/tmp/mjtmp', text); | |
} | |
actions.help = function () { | |
message('Available commands:'); | |
for (var key in this) { | |
if (key in help) { | |
message(key+': '+help[key]); | |
} | |
} | |
} | |
actions.test = function() { | |
console.log('[test] play'); | |
piepan.Audio.Play({filename: 'test'}); | |
} | |
// Say hello! | |
piepan.On('connect', function(e) { | |
var name = piepan.Self.Name; | |
message(name+' is in the building. Type !help for commands.'); | |
console.log(name+' connected.'); | |
}); | |
// Deal with requests | |
piepan.On('message', function(e) { | |
if (e.Sender) { | |
matches = e.Message.match(cmdre); | |
if (!matches) { | |
// Try a connectstring match | |
con_matches = e.Message.match(conre); | |
if (con_matches) { | |
server = con_matches[1]; | |
password = con_matches[2]; | |
message('<a href="steam://connect/'+server+'/'+password+'">CONNECT TO SERVER</a>'); | |
} | |
return; | |
} | |
var cmd = matches[1]; | |
var args = matches[2]; | |
if (cmd in actions) { | |
console.log('[req] '+e.Sender.Name+' : '+cmd+' : '+args); | |
actions[cmd](args, e.Sender); | |
} else { | |
message('No command: '+cmd); | |
} | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment