Improved quoting

Quotes will now contain their source.
Ran rubocop.
master
s00ner 2024-07-15 13:45:23 -04:00
parent ebc76fe8a3
commit 1b1df5dd41
2 changed files with 42 additions and 13 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'discordrb'
gem 'rufus-scheduler'

53
bot.rb
View File

@ -1,34 +1,61 @@
#!/bin/env ruby
# frozen_string_literal: true
require 'discordrb'
require 'rufus-scheduler'
# fetches a random quote from quotes
# fetches a random quote from quotes
def random_quote(quotes)
quote = "**Quote of the Day**"
quote << "```"
quotes.sample.split("\n")[2..].each { | line | quote << line.delete("^") + "\n" }
quote << "```"
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
token = File.read("./token").strip
# 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
quotes = File.read("Game Files/blurbsx.txt").split("##")
quotes.delete_at(0) # Remove empty first item
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(852541842511495210, random_quote(quotes))
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
end
bot.run