jon/systems/quote.rb

65 lines
2.0 KiB
Ruby
Raw Normal View History

require 'dotenv'
2024-10-04 09:09:06 +03:00
require 'sqlite3'
require 'discordrb'
Dotenv.load("#{__dir__}/../.env")
2024-10-04 09:09:06 +03:00
module QuoteSystem
extend Discordrb::Commands::CommandContainer
db = SQLite3::Database.new("jon.sqlite")
command :addquote 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, quote])
nil
end
command :quote do |event, *message|
category = message[0]
if category == nil
event.channel.send_message("You haven't specified a category from where to fetch a random quote!")
return
end
quotes = db.execute("SELECT * FROM quotes WHERE category = ?", [category])
quote = quotes.sample
event.channel.send_message("##{quote[0]}: #{quote[3]}")
nil
end
command :quotecategories do |event, *message|
2024-10-04 09:09:06 +03:00
categories = db.execute("SELECT DISTINCT category FROM quotes")
event.channel.send_message("**Categories**: #{categories[0..].join(' ')}")
nil
end
command :showquote 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
2024-10-04 11:35:37 +03:00
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
2024-10-04 09:09:06 +03:00
end