Sha256: 5b1d4b1e4a933f6ea64cd54e5b507e1d072c5c34617e4e51e5821e1c3b1688a4

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

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

  def run
    every(THIRTY_SECONDS) do
      @endpoints.each do |endpoint|
        request = QstatRequest.new(endpoint)

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

            history[endpoint] ||= {}
            history[endpoint][name] = Time.now
          end
        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?(endpoint:, name:)
    last_seen = history[endpoint] && history[endpoint][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-2.2.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-2.2.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-2.1.3 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-2.1.2 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-2.1.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-2.1.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-2.0.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-2.0.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb