diff --git a/main.rb b/main.rb index 73b1669..516441e 100644 --- a/main.rb +++ b/main.rb @@ -3,6 +3,36 @@ require 'uri' require 'optparse' require 'shellwords' +def parse_songs_from_string(mpd,message) + urls = URI.extract(message) + if urls != "" + puts "Found #{urls}; downloading" + urls.each do |url| + res = `youtube-dl --ignore-errors --output \"spool/%(title)s.%(ext)s\" --extract-audio --audio-format mp3 #{url}` + if res + puts "Downloaded #{url}" + else + puts "error downloading #{url}" + end + end + if mpd + puts "telling mpd to refresh" + new_songs = Dir["spool/*.mp3"] + system("mv spool/*.mp3 tracks/") + system("mpc update") + new_songs.each do |song| + s = song.split("/")[1] + res = system("mpc add #{Shellwords.shellescape(s)}") + if res + puts "added #{s} to mpd" + else + puts "error adding #{s} to mpd" + end + end + end + end +end + options = {} OptionParser.new do |opt| opt.on("-t", "--token DISCORD_BOT_TOKEN") {|o| options[:bot_token] = o} @@ -65,36 +95,18 @@ bot.command :skip do |event| nil end +bot.command :initialize do |event| + event.respond("loading last 20 songs from channel") + channel = bot.channel(options[:channel_id]) + messages = channel.history(20) + messages.each do |msg| + parse_songs_from_string(mpd,msg.content) + end +end + bot.message(in: options[:channel_id],contains: "youtube.com") do |event| - urls = URI.extract(event.content) - if urls != "" - puts "Found #{urls}; downloading" - urls.each do |url| - res = `youtube-dl --ignore-errors --output \"spool/%(title)s.%(ext)s\" --extract-audio --audio-format mp3 #{url}` - if res - puts "Downloaded #{url}" - else - puts "error downloading #{url}" - end - end - if mpd - puts "telling mpd to refresh" - new_songs = Dir["spool/*.mp3"] - system("mv spool/*.mp3 tracks/") - system("mpc update") - new_songs.each do |song| - s = song.split("/")[1] - res = system("mpc add #{Shellwords.shellescape(s)}") - if res - puts "added #{s} to mpd" - else - puts "error adding #{s} to mpd" - end - end - end - end - + parse_songs_from_string(mpd,event.content) end bot.run