Class: QwtfDiscordBotServer

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

Instance Method Summary collapse

Instance Method Details

#runObject



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/qwtf_discord_bot/qwtf_discord_bot_server.rb', line 2

def run
  bot = Discordrb::Commands::CommandBot.new(
    token: QwtfDiscordBot.config.token,
    client_id: QwtfDiscordBot.config.client_id,
    help_command: false,
    prefix: '!'
  )

  bot.command :help do |event, *args|
    "Server commands: `!active`, `!all`, `!server <address>`"
  end

  bot.command :server do |event, *args|
    if args.empty?
      message = 'Provide a server address e.g. `!server ' \
        'sydney.fortressone.org` or use `!active` or `!all`'
      event.channel.send_message(message)
      puts message
    else
      endpoint = args.first
      qstat_response = QstatRequest.new(endpoint)
      message = qstat_response.server_summary
      embed = qstat_response.to_embed

      if embed
        event.channel.send_embed(message, embed)
        puts message
      else
        event.channel.send_message(message)
        puts message
      end
    end
  end

  bot.command :all do |event|
    endpoints_for_this_channel = QwtfDiscordBot.config.endpoints.select do |endpoint|
      endpoint.channel_ids.any? do |channel_id|
        event.channel.id == channel_id
      end
    end

    if endpoints_for_this_channel.empty?
      message = 'There are no servers associated with this channel'
      event.channel.send_message(message)
      puts message
    else
      endpoints_for_this_channel.each do |endpoint|
        qstat_request = QstatRequest.new(endpoint.address)
        message = qstat_request.server_summary
        embed = qstat_request.to_embed

        if embed
          event.channel.send_embed(message, embed)
          puts message
        else
          event.channel.send_message(message)
          puts message
        end
      end
    end

    return nil
  end

  bot.command :active do |event|
    endpoints_for_this_channel = QwtfDiscordBot.config.endpoints.select do |endpoint|
      endpoint.channel_ids.any? do |channel_id|
        event.channel.id == channel_id
      end
    end

    if endpoints_for_this_channel.empty?
      message = 'There are no servers associated with this channel'
      event.channel.send_message(message)
      puts message
    else
      qstat_requests = endpoints_for_this_channel.map do |endpoint|
        QstatRequest.new(endpoint.address)
      end

      servers_with_players = qstat_requests.reject do |server|
        server.is_empty?
      end

      if servers_with_players.empty?
        message = "All ##{event.channel.name} servers are empty"
        event.channel.send_message(message)
        puts message
      else
        servers_with_players.each do |server|
          message = server.server_summary
          embed = server.to_embed

          if embed
            event.channel.send_embed(message, embed)
            puts message
          else
            event.channel.send_message(message)
            puts message
          end
        end
      end
    end
    return nil
  end

  bot.run
end