# @author Wiley Bui (https://github.com/wileybui)

require "dotenv"
require "discordrb"
require "json"

Dir["commands/*.rb", "logger/*.rb", "games/design_patterns/*.rb",
    "games/*.rb", "database/*.rb"].each { |file| require_relative file }

# read env file
Dotenv.load("config/env.yml")

# initializes singleton classes
$logger = LoggerSingleton.instance()
game_manager = Games::GameManagerSingleton.instance()

# initialize bot, authorization/ranks, command, & logger classes
bot = Discordrb::Bot.new token: ENV["BOT_TOKEN"]
authorization = Database::UsersRanksSingleton.instance()
commands_bot_admin = Commands::BotAdmin.new(bot)
commands_bot_regular = Commands::BotRegular.new(bot)

bot.message(contains: /^(.*)$/i) { |event|
  if not event.author.bot_account?
    puts "Received from #{event.user.username}: #{event.message}"

    # notifies all observers the user's input
    game_manager.notify(event)

    author_id = event.author.id

    if (authorization.has_bot_regular_rank?(author_id))
      commands_bot_regular.listen(event)
    end

    if (authorization.has_bot_admin_rank?(author_id))
      commands_bot_admin.listen(event)
    end
  end
}

bot.run
