class DList::Stats

Class for updating bot statistics

@example Hook bot stats to DList

bot = Discordrb::Bot.new token: 'TOKEN'
dbl = DList::Stats.new 'DList_TOKEN', bot

Attributes

token[RW]

Public Class Methods

new(token, bot = nil) click to toggle source

@param token [String] DList bot token @param bot [Discordrb::Bot] discordrb bot for auto-sending statistics

# File lib/dblista/stats.rb, line 17
def initialize(token, bot = nil)
  raise DList::Error, DList::Errors::TOKEN_NOT_PROVIDED unless token

  @token = token
  if bot&.connected?
    hook_bot(bot)
  elsif bot
    bot.ready { hook_bot(bot) }
  end
end

Public Instance Methods

hook_bot(bot) click to toggle source

Hooks discordrb bot to stats client instance

@param bot [Discordrb::Bot] the bot @return [Thread] stat updating thread

# File lib/dblista/stats.rb, line 32
def hook_bot(bot)
  @thread.exit if @thread&.alive?
  @thread = Thread.new do
    update_stats(bot.users.size, bot.servers.size)
    sleep 10 * 60
  end
  @thread
end
update_stats(members, servers) click to toggle source

Sends statistics to DList

@param members [Integer] member count @param servers [Integer] server count @return [Boolean] true if operation succeded

# File lib/dblista/stats.rb, line 46
def update_stats(members, servers)
  DList._post('/bots/stats', {
                  'servers' => servers,
                  'members' => members
                }, @token)
  true
end