From 5d999cc4174f676c1243bd1114679cf685ba6f82 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 1 May 2022 16:42:31 -0400 Subject: [PATCH] more features, handle mpd status --- main.rb | 23 ++++++++++++++++++++--- mpc_shim.rb | 13 +++++++++++++ util.rb | 2 +- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/main.rb b/main.rb index f1d831e..c47d203 100644 --- a/main.rb +++ b/main.rb @@ -36,7 +36,12 @@ end bot = Discordrb::Commands::CommandBot.new token: options[:bot_token], prefix: '!radio ' bot.command :start do |event| - mpc.play + if mpc.running? + mpc.play + else + mpc.start + mpc.play + end event.respond("starting radio") end @@ -73,6 +78,10 @@ bot.command :shuffle do |event| nil end +bot.command :nowplaying do |event| + event.respond(mpc.nowplaying) +end + bot.command :initialize do |event| event.respond("loading last 50 messages from channel") 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) update_mpd(mpc) end - -bot.run +puts("logging into discord") +bot.run(true) +puts("starting main loop") +loop do + timer = 15 + sleep timer + if mpc.running? + bot.listening(mpc.nowplaying) + end +end diff --git a/mpc_shim.rb b/mpc_shim.rb index a656905..06faa7d 100644 --- a/mpc_shim.rb +++ b/mpc_shim.rb @@ -43,6 +43,19 @@ class MpcShim return $?.success? 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) if hostname != :localhost @host_arg = "--host #{hostname} " diff --git a/util.rb b/util.rb index 9dd55cb..4b91d18 100644 --- a/util.rb +++ b/util.rb @@ -24,7 +24,7 @@ def parse_songs_from_string(message) urls.each do |url| if sanity_check_video(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}" else puts "error downloading #{url}"