Sha256: 2fb63d16a16fd5c3ea9ee73be92d152888d665ece4883a8c8ceee69967b0aac2

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

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

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

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

          history[address] ||= {}
          history[address][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?(endpoint:, name:)
    last_seen = history[endpoint] && history[endpoint][name]
    last_seen && (Time.now - last_seen < TEN_MINUTES)
  end

  def report_joined(name:, channel_id:, 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

4 entries across 4 versions & 1 rubygems

Version Path
qwtf_discord_bot-4.0.3 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-4.0.2 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-4.0.1 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb
qwtf_discord_bot-4.0.0 lib/qwtf_discord_bot/qwtf_discord_bot_watcher.rb