stevefolder/main.rb
2022-02-11 16:33:17 -05:00

126 lines
3.4 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
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
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
end
bot.message(private: true, contains: "!folder help") do |event|
event.respond("Commands: rise,fall,help,status")
end
bot.message(private: true, contains: "!folder status") do |event|
event.respond("Stevefolder has #{folder.num_subs} submissions")
end
bot.message(private: true) do |event|
sender = event.author.id
i = 1
event.message.attachments.each do |file|
res = MimeMagic.by_path(file.filename)
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))
end
else
folder.submit(Submission.new(url,false,sender))
end
event.respond("Submission #{i} accepted")
i= i+1
end
folder.update()
end
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)))
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