more features, handle mpd status

This commit is contained in:
stryan 2022-05-01 16:42:31 -04:00
parent f8dde5672b
commit 5d999cc417
3 changed files with 34 additions and 4 deletions

23
main.rb
View File

@ -36,7 +36,12 @@ end
bot = Discordrb::Commands::CommandBot.new token: options[:bot_token], prefix: '!radio ' bot = Discordrb::Commands::CommandBot.new token: options[:bot_token], prefix: '!radio '
bot.command :start do |event| bot.command :start do |event|
mpc.play if mpc.running?
mpc.play
else
mpc.start
mpc.play
end
event.respond("starting radio") event.respond("starting radio")
end end
@ -73,6 +78,10 @@ bot.command :shuffle do |event|
nil nil
end end
bot.command :nowplaying do |event|
event.respond(mpc.nowplaying)
end
bot.command :initialize do |event| bot.command :initialize do |event|
event.respond("loading last 50 messages from channel") event.respond("loading last 50 messages from channel")
channel = bot.channel(options[:channel_id]) channel = bot.channel(options[:channel_id])
@ -89,5 +98,13 @@ bot.message(in: options[:channel_id],contains: "youtube.com") do |event|
parse_songs_from_string(event.content) parse_songs_from_string(event.content)
update_mpd(mpc) update_mpd(mpc)
end end
puts("logging into discord")
bot.run bot.run(true)
puts("starting main loop")
loop do
timer = 15
sleep timer
if mpc.running?
bot.listening(mpc.nowplaying)
end
end

View File

@ -43,6 +43,19 @@ class MpcShim
return $?.success? return $?.success?
end end
def start()
`systemctl start mpd` unless `ps aux | grep mpd` != ""
`mpc ${host_arg} repeat`
end
def running?()
return `ps aux | grep mpd` == ""
end
def nowplaying()
return `mpc ${host_arg} | head -n 1`
end
def initialize(hostname=:localhost) def initialize(hostname=:localhost)
if hostname != :localhost if hostname != :localhost
@host_arg = "--host #{hostname} " @host_arg = "--host #{hostname} "

View File

@ -24,7 +24,7 @@ def parse_songs_from_string(message)
urls.each do |url| urls.each do |url|
if sanity_check_video(url) if sanity_check_video(url)
res = `youtube-dl --ignore-errors --output \"spool/%(title)s.%(ext)s\" --extract-audio --audio-format mp3 #{url}` res = `youtube-dl --ignore-errors --output \"spool/%(title)s.%(ext)s\" --extract-audio --audio-format mp3 #{url}`
if res if res
puts "Downloaded #{url}" puts "Downloaded #{url}"
else else
puts "error downloading #{url}" puts "error downloading #{url}"