stevetunes/main.rb

141 lines
2.8 KiB
Ruby
Raw Normal View History

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