Sha256: c7453aaedbaed933d783d062f8fc77a0713db74c4515596b8f16e08c26df12e4

Contents?: true

Size: 1.37 KB

Versions: 66

Compression:

Stored size: 1.37 KB

Contents

class QwtfDiscordBotWatcher
  include QwtfDiscordBot

  THIRTY_SECONDS = 30
  TEN_MINUTES = 10 * 60

  def run
    every(THIRTY_SECONDS) do
      QwtfDiscordBot.config.endpoints.each do |endpoint|
        address = endpoint.address
        request = QstatRequest.new(address)
        next if request.is_empty?

        request.player_names.each do |name|
          redis_key = ['watcher', address, name].join(':')

          unless seen_recently?(redis_key)
            endpoint.channel_ids.each do |channel_id|
              report_joined(
                name: name,
                channel_id: channel_id,
                server_summary: request.server_summary
              )
            end
          end

          update_last_seen_at(redis_key)
        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?(redis_key)
    redis.get(redis_key)
  end

  def update_last_seen_at(redis_key)
    redis.set(redis_key, Time.now)
    redis.expire(redis_key, TEN_MINUTES)
  end

  def report_joined(name:, channel_id:, server_summary:)
    message = "#{name} joins #{server_summary}"

    Discordrb::API::Channel.create_message(
      "Bot #{QwtfDiscordBot.config.token}",
      channel_id,
      message
    )

    puts message
  end
end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
qwtf_discord_bot-6.3.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.3.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.2.2 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.2.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.2.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.1.5 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.1.4 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.1.3 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.1.2 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.1.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.1.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.0.4 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.0.3 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.0.2 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-6.0.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-5.5.26 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-5.5.25 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-5.5.24 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-5.5.23 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-5.5.22 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb