Class: DList::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/dblista/stats.rb

Overview

Class for updating bot statistics

Examples:

Hook bot stats to DList

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, bot = nil) ⇒ Stats

Returns a new instance of Stats.

Parameters:

  • token (String)

    DList bot token

  • bot (Discordrb::Bot) (defaults to: nil)

    discordrb bot for auto-sending statistics

Raises:


17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#tokenObject

Returns the value of attribute token


13
14
15
# File 'lib/dblista/stats.rb', line 13

def token
  @token
end

Instance Method Details

#hook_bot(bot) ⇒ Thread

Hooks discordrb bot to stats client instance

Parameters:

  • bot (Discordrb::Bot)

    the bot

Returns:

  • (Thread)

    stat updating thread


32
33
34
35
36
37
38
39
# 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) ⇒ Boolean

Sends statistics to DList

Parameters:

  • members (Integer)

    member count

  • servers (Integer)

    server count

Returns:

  • (Boolean)

    true if operation succeded


46
47
48
49
50
51
52
# File 'lib/dblista/stats.rb', line 46

def update_stats(members, servers)
  DList._post('/bots/stats', {
                  'servers' => servers,
                  'members' => members
                }, @token)
  true
end