stevetunes/main.rb

122 lines
2.5 KiB
Ruby
Raw 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
2021-12-22 14:40:08 -05:00
mpc = MpcShim.new
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)}
end.parse!
if options.length == 0
puts "Need to specifiy token and channel"
exit
end
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
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 16:42:31 -04:00
puts("logging into discord")
bot.run(true)
puts("starting main loop")
timer = 30
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
bot.idle
2022-05-01 16:42:31 -04:00
end
end