stevefolder/main.rb

169 lines
4.2 KiB
Ruby
Executable File

#!/usr/bin/ruby
#require 'bundler/setup'
require 'discordrb'
require 'yaml'
require 'mimemagic'
require 'down'
require 'fileutils'
require 'time'
require 'logger'
require 'astro/moon'
require './folder.rb'
logger = Logger.new(STDOUT)
logger.level = Logger::INFO
config_obj = YAML::load_file( './config.yaml' )
logger.info("loaded config")
bot = Discordrb::Commands::CommandBot.new token: config_obj["bot_token"], prefix: "!folder"
folder = Folder.new
responded = false
def shut_down(bot,folder)
if bot.connected?
bot.stop
end
folder.close
end
config_obj['steves'].each do |steve|
folder.add_steve(steve)
end
bot.message(private: true,contains: "!folder fall") do |event|
if folder.steve?(event.user)
folder.curse(event.user)
mem = bot.member(config_obj['server_id'],event.user.id)
mem.set_nick("Geoff","This Steve has seperated himself from the Ideal Steve")
event.respond("May Snen have Mercy on your Soul")
else
event.respond("Oh that you could be so blessed as to have that option")
end
responded = true
end
bot.message(private: true,contains: "!folder rise") do |event|
if folder.steve?(event.user)
folder.bless(event.user)
mem = bot.member(config_obj['server_id'],event.user.id)
mem.set_nick("","Steve-ness restored")
event.respond("Welcome back to the winning team")
else
event.respond("Fat chance, non-Steve")
end
responded = true
end
bot.message(private: true, contains: "!folder help") do |event|
event.respond("Commands: rise,fall,help,status")
responded = true
end
bot.message(private: true, contains: "!folder status") do |event|
event.respond("Stevefolder has #{folder.num_subs} submissions")
responded = true
end
bot.message(private: true) do |event|
if responded
responded = false
break
end
sender = event.author.id
i = 1
updated = false
event.message.attachments.each do |file|
res = MimeMagic.by_path(file.filename)
updated = true
if res != nil
if res.image? or res.video?
download = Down.download(file.url)
FileUtils.mv(download.path, "./spool/#{download.original_filename}")
folder.submit(Submission.new("./spool/#{download.original_filename}",true,sender))
event.respond("Submission #{i} accepted")
i = i + 1
else
event.respond("File type #{res} not supported yet")
end
else
event.respond("Submission denied; attached file but I can't figure out the file type")
end
end
urls = URI.extract(event.content)
urls.each do |url|
mime_guess = MimeMagic.by_extension(url)
if mime_guess != nil
if mime_guess.image? or mime_guess.video?
download = Down.download(file.url)
FileUtils.mv(download.path, "./spool/#{download.original_filename}")
folder.submit(Submission.new("./spool/#{download.original_filename}",true,sender))
event.respond("Submission #{i} accepted")
updated = true
else
event.respond("Submission denied; link goes to a piece of media but I can't figure out what it is")
end
else
folder.submit(Submission.new(url,false,sender))
event.respond("Submission #{i} accepted")
updated = true
end
i= i+1
end
if updated
folder.update()
else
event.respond("Submission denied: I don't know what this is")
end
end
#set up signals
Signal.trap("INT") {
shut_down(bot,folder)
exit
}
# Trap `Kill `
Signal.trap("TERM") {
shut_down(bot,folder)
exit
}
logger.info("Logging into discord")
bot.run(true)
logger.info("Starting bot main loop")
loop do
timer = Astro::Moon.phase.phase * (3600 * (1 + rand(3)))
if timer < 900 #lower limit of 15 minutes
timer = 900
end
logger.info("sleeping for #{timer} seconds")
sleep timer
t = Time.new
if t.hour < 9 and t.hour > 2
logger.info("skipping send cuz it's late")
next
end
sid,msg = folder.harass_steve()
if msg == nil
logger.info("no submissions ready")
next
end
steve = bot.user(sid)
if steve == nil
logger.warn("Tried to get Steve #{sid} but failed, skipping")
next
end
logger.info("sending submission #{msg.id}")
if msg.local
steve.send_file(File.open(msg.content,'r'))
File.delete(msg.content)
else
steve.pm(msg.content)
end
folder.update()
end