datalinks/bot.rb

62 lines
1.6 KiB
Ruby
Executable File

#!/bin/env ruby
# frozen_string_literal: true
require 'discordrb'
require 'rufus-scheduler'
# fetches a random quote from quotes
def random_quote(quotes)
entry = quotes.keys.sample
quote = "**Quote of the Day**\n"
quote += "From the #{quotes[entry][:tech]}: #{entry}\n"
quote += '```'
quote += quotes[entry][:quote]
"#{quote}```"
end
# map quote types
def get_quote_type(tech)
case tech
when /TECH/
'Technology'
when /HELPFAC/
'Base Facility'
when /PROJECT/
'Secret Project'
when /OPENING/
nil
end
end
# preserves whitespace intention for quotes, expects array of strings, one line per element
def make_pretty_quote(quote)
quote.map { |line| line.gsub('^^', " \n").gsub('^', "\n") }.join(' ').strip
end
token = File.read('./token').strip
bot = Discordrb::Bot.new token: token
raw_quotes = File.read('Game Files/blurbsx.txt').split('##')
raw_quotes.delete_at(0) # Remove empty first item
scheduler = Rufus::Scheduler.new
start_time = Rufus::Scheduler.parse('12:30') # what time of day the message should send
# start tomorrow if start_time has passed
start_time += Rufus::Scheduler.parse('1d') if start_time < Time.now
quotes = {}
raw_quotes.each do |quote|
quote = quote.split("\n")
tech_and_text = { tech: get_quote_type(quote[1]), quote: make_pretty_quote(quote[2..]) }
quotes.store(quote[0], tech_and_text)
end
quotes.freeze
scheduler.every '1d', first_at: start_time do
bot.send_message(852_541_842_511_495_210, random_quote(quotes))
end
bot.message(with_text: 'Ping!') do |event|
event.respond random_quote(quotes)
pp bot
end
bot.run