jon/bot.rb
2024-10-03 09:04:32 +03:00

57 lines
1.9 KiB
Ruby

require 'discordrb'
require 'dotenv'
require 'sqlite3'
Dotenv.load
bot = Discordrb::Commands::CommandBot.new token: ENV['TOKEN'], client_id: ENV['CLIENT_ID'], prefix: ENV['PREFIX']
db = SQLite3::Database.new("jon.sqlite")
bot.message do |event|
if ENV['FORBIDDEN_WORDS'].split(",").any? { |word| event.message.content.downcase.include?(word.downcase)}
event.author.pm("Garfield, watch your tone!")
end
end
bot.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
bot.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
bot.command :categories do |event, *message|
categories = db.execute("SELECT DISTINCT category FROM quotes")
event.channel.send_message("**Categories**: #{categories[0..].join(' ')}")
nil
end
bot.command :help do |event, *message|
event.author.pm("You've asked for help. In order not to flood the channel where you sent the command, I have sent the list of commands here, in your PMs. Enjoy! \n\n**Quote system**\n```;quote <category> - displays a quote from the category\n;addquote <category> <quote> - adds a quote into a category\n;categories - shows all categories```")
event.message.delete
nil
end
bot.run