Class: DList::Stats
- Inherits:
-
Object
- Object
- DList::Stats
- Defined in:
- lib/dblista/stats.rb
Overview
Class for updating bot statistics
Instance Attribute Summary collapse
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
-
#hook_bot(bot) ⇒ Thread
Hooks discordrb bot to stats client instance.
-
#initialize(token, bot = nil) ⇒ Stats
constructor
A new instance of Stats.
-
#update_stats(members, servers) ⇒ Boolean
Sends statistics to DList.
Constructor Details
#initialize(token, bot = nil) ⇒ Stats
Returns a new instance of Stats.
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
#token ⇒ Object
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
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
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 |