Sha256: d66ac0c482fceaba09f0de3a6e03f4769b00bcaab420840428ee43c2b24454ab

Contents?: true

Size: 973 Bytes

Versions: 8

Compression:

Stored size: 973 Bytes

Contents

class QwtfDiscordBotWatcher < QwtfDiscordBot
  THIRTY_SECONDS = 30
  TEN_MINUTES = 10 * 60

  def run
    every(THIRTY_SECONDS) do
      request = QstatRequest.new(endpoint)

      if request.has_players?
        request.player_names.each do |name|
          unless seen_recently?(name)
            report_joined(name: name, server_summary: request.server_summary)
          end

          history[name] = Time.now
        end
      end
    end
  end

  def every(n_seconds)
    loop do
      before = Time.now
      yield
      interval = n_seconds - (Time.now - before)
      sleep(interval) if interval > 0
    end
  end

  def seen_recently?(name)
    last_seen = history[name]
    last_seen && (Time.now - last_seen < TEN_MINUTES)
  end

  def report_joined(name:, server_summary:)
    Discordrb::API::Channel.create_message(
      "Bot #{TOKEN}",
      CHANNEL_ID,
      "#{name} has joined #{server_summary}"
    )
  end

  def history
    @history ||= {}
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
qwtf_discord_bot-1.0.3 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-1.0.2 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-1.0.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-1.0.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-0.5.9 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-0.5.8 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-0.5.7 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-0.5.6 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb