jon/systems/quote.rb

73 lines
2.4 KiB
Ruby

require 'dotenv'
require 'sqlite3'
require 'discordrb'
Dotenv.load("#{__dir__}/../.env")
module QuoteSystem
extend Discordrb::Commands::CommandContainer
db = SQLite3::Database.new("jon.sqlite")
command(:quoteadd, aliases: [:addquote, :qa]) do |event, *message|
if message[0] == nil
event.channel.send_message("You haven't specified a category for your quote.")
return
end
if message[1] == nil
event.channel.send_message("I can't quote air!")
return
end
category = message[0]
quote = message[1..].join(" ")
event.channel.send_message("I'm adding the quote '#{quote}' under the category #{category}")
db.execute("INSERT INTO quotes (author, category, quote) VALUES (?, ?, ?)", [event.author.id, category.downcase, quote])
nil
end
command(:quote, aliases: [:q]) do |event, *message|
category = message[0]
if category == nil
quotes = db.execute("SELECT * FROM quotes")
quote = quotes.sample
event.channel.send_message("##{quote[0]} <#{quote[2]}>: #{quote[3]}")
else
category = category.downcase
quotes = db.execute("SELECT * FROM quotes WHERE category = ?", [category])
if quotes == []
event.channel.send_message("Uh, this is awkward. The category you're trying to get a quote from doesn't exist.")
else
quote = quotes.sample
event.channel.send_message("##{quote[0]}: #{quote[3]}")
end
end
nil
end
command(:quotecategories, aliases: [:qc]) do |event, *message|
categories = db.execute("SELECT category FROM quotes GROUP BY category ORDER BY COUNT(*) DESC;")
event.channel.send_message("**Categories**: #{categories[0..].join(' ')}")
nil
end
command(:showquote, aliases: [:sq, :quoteshow]) do |event, *message|
quote = db.execute("SELECT * FROM quotes WHERE id = ?", [message[0]])
if quote == nil
event.channel.send_message("Quote doesn't exist")
return
end
event.channel.send_message("##{quote[0][0]} <#{quote[0][2]}>: #{quote[0][3]}")
nil
end
command :delquote do |event, *message|
if event.author.id != ENV['ADMIN'].to_i
event.channel.send_message("You really thought you could do this?")
return
end
for quote in message do
db.execute('DELETE FROM quotes WHERE "id"=?;', [quote])
end
event.channel.send_message("Job complete.")
nil
end
end