stevetunes/main.rb

141 lines
2.8 KiB
Ruby
Raw Permalink Normal View History

2021-12-22 14:48:31 -05:00
#!/usr/bin/ruby
2021-12-19 17:43:07 -05:00
require 'discordrb'
2021-12-19 19:04:55 -05:00
require 'video_info'
2021-12-19 17:43:07 -05:00
require 'uri'
require 'optparse'
require 'shellwords'
2021-12-22 14:40:08 -05:00
require './mpc_shim'
require './util'
2021-12-19 17:43:07 -05:00
2022-08-15 22:52:41 -04:00
2021-12-19 17:43:07 -05:00
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)}
2022-08-15 22:52:41 -04:00
opt.on("-h", "--host MPD_HOST") {|o| options[:host] = o}
2021-12-19 17:43:07 -05:00
end.parse!
2022-08-15 22:52:41 -04:00
if ENV.has_key? "DISCORD_BOT_TOKEN"
options[:bot_token] = ENV["DISCORD_BOT_TOKEN"]
end
if ENV.has_key? "CHANNEL_ID"
options[:channel_id] = Integer(ENV["CHANNEL_ID"])
end
if ENV.has_key? "MPD_HOST"
options[:host] = ENV["MPD_HOST"]
end
2021-12-19 17:43:07 -05:00
if options.length == 0
puts "Need to specifiy token and channel"
exit
end
2022-08-15 22:52:41 -04:00
mpc = MpcShim.new(options[:host])
2021-12-19 17:43:07 -05:00
puts "creating spool and tracks directories"
`mkdir -p spool`
2021-12-22 14:40:08 -05:00
`mkdir -p tracks`
2021-12-19 17:43:07 -05:00
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
if mpc.running?
2022-05-01 17:10:02 -04:00
puts "starting mpd if not already running"
`systemctl start mpd` unless `ps aux | grep mpd` != ""
mpc.reset
end
2021-12-19 17:43:07 -05:00
end
2021-12-19 17:53:07 -05:00
bot = Discordrb::Commands::CommandBot.new token: options[:bot_token], prefix: '!radio '
2021-12-19 17:49:03 -05:00
bot.command :start do |event|
2022-05-01 16:42:31 -04:00
if mpc.running?
mpc.play
else
mpc.start
2022-05-01 18:33:44 -04:00
mpc.reset
2022-05-01 16:42:31 -04:00
mpc.play
end
2021-12-19 17:49:03 -05:00
event.respond("starting radio")
end
bot.command :stop do |event|
2021-12-22 14:40:08 -05:00
mpc.stop
2021-12-19 17:49:03 -05:00
event.respond("stoping radio")
end
bot.command :reload do |event|
event.respond("reloading queue")
2021-12-22 14:40:08 -05:00
mpc.stop
mpc.clear
mpc.update
mpc.add
2021-12-19 19:04:55 -05:00
event.respond("queue reloaded")
2021-12-19 17:53:07 -05:00
nil
2021-12-19 17:49:03 -05:00
end
bot.command :pause do |event|
event.respond("pausing playback")
2021-12-22 14:40:08 -05:00
mpc.toggle
2021-12-19 17:53:07 -05:00
nil
2021-12-19 17:49:03 -05:00
end
bot.command :skip do |event|
event.respond("skipping")
2021-12-22 14:40:08 -05:00
mpc.next()
2021-12-19 17:53:07 -05:00
nil
2021-12-19 17:49:03 -05:00
end
2021-12-19 19:04:55 -05:00
bot.command :shuffle do |event|
2021-12-22 14:40:08 -05:00
mpc.shuffle()
2021-12-19 19:04:55 -05:00
event.respond("shuffled queue")
nil
end
2022-05-01 16:42:31 -04:00
bot.command :nowplaying do |event|
event.respond(mpc.nowplaying)
end
2021-12-19 18:04:35 -05:00
bot.command :initialize do |event|
2021-12-19 18:08:54 -05:00
event.respond("loading last 50 messages from channel")
2021-12-19 18:04:35 -05:00
channel = bot.channel(options[:channel_id])
2021-12-19 18:08:54 -05:00
messages = channel.history(50)
2021-12-19 18:04:35 -05:00
messages.each do |msg|
2021-12-19 19:04:55 -05:00
parse_songs_from_string(msg.content)
2021-12-19 17:43:07 -05:00
end
2021-12-22 14:40:08 -05:00
update_mpd(mpc)
2021-12-19 18:08:54 -05:00
event.respond("done loading")
2021-12-19 18:04:35 -05:00
end
2021-12-19 17:43:07 -05:00
2021-12-19 18:04:35 -05:00
bot.message(in: options[:channel_id],contains: "youtube.com") do |event|
2021-12-19 19:04:55 -05:00
parse_songs_from_string(event.content)
2021-12-22 14:40:08 -05:00
update_mpd(mpc)
2021-12-19 17:43:07 -05:00
end
2022-05-01 18:39:39 -04:00
2022-05-01 16:42:31 -04:00
puts("logging into discord")
bot.run(true)
puts("starting main loop")
timer = 30
2022-05-01 18:30:17 -04:00
bot.online
bot.listening = ""
2022-05-01 16:42:31 -04:00
loop do
sleep timer
2022-05-01 17:10:02 -04:00
if !mpc.playing?
bot.online
2022-05-01 17:00:08 -04:00
current = mpc.nowplaying
2022-05-01 17:10:02 -04:00
status = bot.listening=current
2022-05-01 17:00:08 -04:00
if status != current
puts("tried to update listening status but got #{status} back")
2022-05-01 17:10:02 -04:00
elsif status == ""
puts("tried to update listening status but have empty now playing")
2022-05-01 17:00:08 -04:00
end
2022-05-01 16:51:23 -04:00
else
2022-05-01 18:30:17 -04:00
bot.listening = ""
2022-05-01 16:51:23 -04:00
bot.idle
2022-05-01 16:42:31 -04:00
end
end