initial
This commit is contained in:
commit
ca4a8fd0fa
67
main.rb
Normal file
67
main.rb
Normal file
@ -0,0 +1,67 @@
|
||||
require 'discordrb'
|
||||
require 'uri'
|
||||
require 'optparse'
|
||||
require 'shellwords'
|
||||
|
||||
options = {}
|
||||
OptionParser.new do |opt|
|
||||
opt.on("-t", "--token DISCORD_BOT_TOKEN") {|o| options[:bot_token] = o}
|
||||
opt.on("-c", "--channel CHANNEL_ID") {|o| options[:channel_id] = Integer(o)}
|
||||
end.parse!
|
||||
|
||||
if options.length == 0
|
||||
puts "Need to specifiy token and channel"
|
||||
exit
|
||||
end
|
||||
|
||||
puts "creating spool and tracks directories"
|
||||
system("mkdir -p spool")
|
||||
system("mkdir -p tracks")
|
||||
|
||||
mpd = false
|
||||
if File.exists?("/usr/bin/mpd")
|
||||
if !File.exists?("/usr/bin/mpc")
|
||||
puts "Bailing since mpd exists but not mpc (which is needed to control mpd)"
|
||||
exit
|
||||
end
|
||||
puts "starting mpd if not already running"
|
||||
system("systemctl start mpd") unless `ps aux | grep mpd` != ""
|
||||
system("mpc clear")
|
||||
system("mpc add /")
|
||||
mpd = true
|
||||
end
|
||||
|
||||
bot = Discordrb::Bot.new token: options[:bot_token]
|
||||
bot.message(in: options[:channel_id],contains: "youtube.com") do |event|
|
||||
event.respond("got message")
|
||||
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
|
||||
|
||||
end
|
||||
|
||||
bot.run
|
Loading…
Reference in New Issue
Block a user