stevefolder/main.rb

118 lines
3.1 KiB
Ruby
Raw Normal View History

2022-02-04 18:48:09 -05:00
#!/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")
end
bot.message(private: true) do |event|
(mem,au) = event.author
i = 1
if mem == nil
sender = au.username
else
sender = mem.username
end
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
end
logger.info("Starting bot")
bot.run(true)
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 or t.hour > 2
next
end
sid,msg = folder.harass_steve()
if msg == nil
logger.info("no submissions ready")
next
end
steve = bot.users[sid]
logger.info("sending submission #{msg.id}")
if msg.local
steve.send_file(File.open(msg.content,'r'))
else
steve.pm(msg.content)
end
end