Sha256: 7d0c756c3e0a6b017a9c7f1f130c397d48e5e1ce8986e81b52029b6551e6fead

Contents?: true

Size: 974 Bytes

Versions: 5

Compression:

Stored size: 974 Bytes

Contents

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

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

      unless request.is_empty?
        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

5 entries across 5 versions & 1 rubygems

Version Path
qwtf_discord_bot-1.0.8 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-1.0.7 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-1.0.6 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-1.0.5 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-1.0.4 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb