stevefolder/main.rb

169 lines
4.2 KiB
Ruby
Raw Normal View History

2022-02-04 18:48:09 -05:00
#!/usr/bin/ruby
2022-04-25 19:54:54 -04:00
#require 'bundler/setup'
2022-02-04 18:48:09 -05:00
require 'discordrb'
require 'yaml'
require 'mimemagic'
require 'down'
require 'fileutils'
require 'time'
require 'logger'
require 'astro/moon'
require './folder.rb'
2022-02-11 16:33:24 -05:00
2022-02-04 18:48:09 -05:00
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
2022-07-03 16:23:34 -04:00
responded = false
2022-02-11 16:33:24 -05:00
2022-02-11 16:39:43 -05:00
def shut_down(bot,folder)
if bot.connected?
2022-02-11 16:33:24 -05:00
bot.stop
end
folder.close
end
2022-02-04 18:48:09 -05:00
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
2022-07-03 16:23:34 -04:00
responded = true
2022-02-04 18:48:09 -05:00
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
2022-07-03 16:23:34 -04:00
responded = true
2022-02-04 18:48:09 -05:00
end
bot.message(private: true, contains: "!folder help") do |event|
2022-02-05 15:32:14 -05:00
event.respond("Commands: rise,fall,help,status")
2022-07-03 16:23:34 -04:00
responded = true
2022-02-05 15:32:14 -05:00
end
bot.message(private: true, contains: "!folder status") do |event|
event.respond("Stevefolder has #{folder.num_subs} submissions")
2022-07-03 16:23:34 -04:00
responded = true
2022-02-04 18:48:09 -05:00
end
bot.message(private: true) do |event|
2022-07-03 16:23:34 -04:00
if responded
responded = false
break
end
2022-02-05 15:47:00 -05:00
sender = event.author.id
2022-02-04 18:48:09 -05:00
i = 1
2022-04-25 19:54:54 -04:00
updated = false
2022-02-04 18:48:09 -05:00
event.message.attachments.each do |file|
res = MimeMagic.by_path(file.filename)
2022-04-25 19:54:54 -04:00
updated = true
2022-02-04 18:48:09 -05:00
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))
2022-04-25 19:54:54 -04:00
event.respond("Submission #{i} accepted")
updated = true
2022-03-30 12:36:03 -04:00
else
event.respond("Submission denied; link goes to a piece of media but I can't figure out what it is")
2022-02-04 18:48:09 -05:00
end
else
folder.submit(Submission.new(url,false,sender))
2022-03-30 12:36:03 -04:00
event.respond("Submission #{i} accepted")
2022-04-25 19:54:54 -04:00
updated = true
2022-03-30 12:36:03 -04:00
end
2022-02-04 18:48:09 -05:00
i= i+1
end
2022-04-25 19:54:54 -04:00
if updated
folder.update()
else
event.respond("Submission denied: I don't know what this is")
end
2022-02-04 18:48:09 -05:00
end
2022-02-11 16:33:24 -05:00
#set up signals
2022-02-11 16:39:43 -05:00
Signal.trap("INT") {
shut_down(bot,folder)
2022-02-11 16:33:24 -05:00
exit
}
2022-02-04 18:48:09 -05:00
2022-02-11 16:33:24 -05:00
# Trap `Kill `
Signal.trap("TERM") {
2022-02-11 16:39:43 -05:00
shut_down(bot,folder)
2022-02-11 16:33:24 -05:00
exit
}
2022-02-04 18:48:09 -05:00
2022-02-07 12:33:52 -05:00
logger.info("Logging into discord")
2022-02-04 18:48:09 -05:00
bot.run(true)
2022-02-07 12:33:52 -05:00
logger.info("Starting bot main loop")
2022-02-04 18:48:09 -05:00
loop do
timer = Astro::Moon.phase.phase * (3600 * (1 + rand(3)))
2022-04-01 11:31:32 -04:00
if timer < 900 #lower limit of 15 minutes
timer = 900
end
2022-02-04 18:48:09 -05:00
logger.info("sleeping for #{timer} seconds")
sleep timer
t = Time.new
2022-02-04 19:22:06 -05:00
if t.hour < 9 and t.hour > 2
logger.info("skipping send cuz it's late")
2022-02-04 18:48:09 -05:00
next
end
sid,msg = folder.harass_steve()
if msg == nil
logger.info("no submissions ready")
next
end
2022-02-11 16:33:17 -05:00
steve = bot.user(sid)
2022-02-07 10:37:01 -05:00
if steve == nil
logger.warn("Tried to get Steve #{sid} but failed, skipping")
next
end
2022-02-04 18:48:09 -05:00
logger.info("sending submission #{msg.id}")
if msg.local
steve.send_file(File.open(msg.content,'r'))
2022-02-04 19:22:06 -05:00
File.delete(msg.content)
2022-02-04 18:48:09 -05:00
else
steve.pm(msg.content)
end
2022-02-05 15:32:14 -05:00
folder.update()
2022-02-04 18:48:09 -05:00
end