stevetunes/main.rb
2022-05-01 17:00:08 -04:00

117 lines
2.3 KiB
Ruby

#!/usr/bin/ruby
require 'discordrb'
require 'video_info'
require 'uri'
require 'optparse'
require 'shellwords'
require './mpc_shim'
require './util'
mpc = MpcShim.new
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`
`mkdir -p tracks`
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"
`systemctl start mpd` unless `ps aux | grep mpd` != ""
mpc.reset()
end
bot = Discordrb::Commands::CommandBot.new token: options[:bot_token], prefix: '!radio '
bot.command :start do |event|
if mpc.running?
mpc.play
else
mpc.start
mpc.play
end
event.respond("starting radio")
end
bot.command :stop do |event|
mpc.stop
event.respond("stoping radio")
end
bot.command :reload do |event|
event.respond("reloading queue")
mpc.stop
mpc.clear
mpc.update
mpc.add
event.respond("queue reloaded")
nil
end
bot.command :pause do |event|
event.respond("pausing playback")
mpc.toggle
nil
end
bot.command :skip do |event|
event.respond("skipping")
mpc.next()
nil
end
bot.command :shuffle do |event|
mpc.shuffle()
event.respond("shuffled queue")
nil
end
bot.command :nowplaying do |event|
event.respond(mpc.nowplaying)
end
bot.command :initialize do |event|
event.respond("loading last 50 messages from channel")
channel = bot.channel(options[:channel_id])
messages = channel.history(50)
messages.each do |msg|
parse_songs_from_string(msg.content)
end
update_mpd(mpc)
event.respond("done loading")
end
bot.message(in: options[:channel_id],contains: "youtube.com") do |event|
parse_songs_from_string(event.content)
update_mpd(mpc)
end
puts("logging into discord")
bot.run(true)
puts("starting main loop")
loop do
timer = 15
sleep timer
if mpc.running?
current = mpc.nowplaying
status = bot.listening(current)
if status != current
puts("tried to update listening status but got #{status} back")
end
else
bot.idle
end
end