Class: Dashboard

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

Instance Method Summary collapse

Constructor Details

#initialize(dashboard_config, bot) ⇒ Dashboard

Returns a new instance of Dashboard.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dashboard.rb', line 2

def initialize(dashboard_config, bot)
  @server = bot.server(dashboard_config["server_id"])
  @endpoints = dashboard_config["endpoints"]
  @messages = {}

  channel_name = dashboard_config["name"]

  old_dashboard_channel = @server.channels.find do |chan|
    chan.name == channel_name
  end

  @channel = if old_dashboard_channel
               old_messages = JSON.parse(
                 Discordrb::API::Channel.messages(
                   "Bot #{QwtfDiscordBot.config.token}",
                   old_dashboard_channel.id,
                   100
                 )
               )

               old_messages.each do |old_message|
                 sleep 2
                 old_dashboard_channel.message(old_message['id']).delete
               end

               old_dashboard_channel
             else
               @server.create_channel(channel_name).tap do |channel|
                 channel.topic = "QWTF Bot Dashboard"
               end
             end
end

Instance Method Details

#updateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dashboard.rb', line 35

def update
  @endpoints.each do |endpoint|
    qstat_request = QstatRequest.new(endpoint)

    if qstat_request.is_empty?
      if @messages[endpoint]
        @messages[endpoint].delete
        @messages.delete(endpoint)
      end

      next
    end

    embed = qstat_request.to_full_embed

    @messages[endpoint] = if @messages[endpoint]
                            @messages[endpoint].edit(nil, embed)
                          else
                            @channel.send_embed(nil, embed)
                          end
  end
end