qwtf_discord_bot 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ba96254cc28ceadd0b9874632b15808034d798e57f5e737e1b4f5e32fe35075
4
- data.tar.gz: cea4d4376e254c67fe280097e440e1991e9f08f627305ed8cd770355ca2bf67a
3
+ metadata.gz: d474c40842701016d9431def0c11f0802e1e9f760b8cd46906b6c1813165da69
4
+ data.tar.gz: bbee37ae5f25bbc63395ac866704994c1e6bdbfc7399ea12e7147f44c918c1f6
5
5
  SHA512:
6
- metadata.gz: 06afd5a52438eba557524c6d747da5bd34c8f3a03d644e297796a6272408cbbea6f1ceda23d9d4c8894870af98757c28ce3407f6245d0a274d7d267851770c24
7
- data.tar.gz: 83642076d836259ec4bd96538f5abc39bd308dd5ce915a9aa1b16e579cad9f3219eac16ac4f50adda2fbeaa0f72a3709c7bf5584c845acb20344b910e225c2b7
6
+ metadata.gz: 3130bb1aa93eac85706b2294f0bf607061bc3ae3f6ad17c76b83dc76fd302d69014aab4448b3f02878fdf878558b21e0a8415c6a79a9157b45b2ad55624a8184
7
+ data.tar.gz: 998f5873f02adafd23ad4bfded8728b0df7c0a7ca5507a348cf642c2291e1239fd1a61c887d7c025104bdb3f003ce6c1f658db3d85dc73b18541e55d6873d395
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qwtf_discord_bot (2.2.1)
4
+ qwtf_discord_bot (3.0.0)
5
5
  discordrb (~> 3.3)
6
6
  thor (~> 0.20)
7
7
 
@@ -0,0 +1,5 @@
1
+ ---
2
+ # Add each endpoint below the in format
3
+ # "<hostname>:<port>":
4
+ # - CHANNEL_ID_1
5
+ # - CHANNEL_ID_2
data/exe/qwtf_discord_bot CHANGED
@@ -8,17 +8,15 @@ class QwtfDiscordBotExe < Thor
8
8
  true
9
9
  end
10
10
 
11
- desc 'server', 'Responds to `!server` with server information'
12
- method_option :endpoints, type: :array, alias: '-e', default: ['localhost:27500']
11
+ desc 'server', 'Responds to user commands with server information'
13
12
  def server
14
- server_bot = QwtfDiscordBotServer.new(options[:endpoints])
13
+ server_bot = QwtfDiscordBotServer.new
15
14
  server_bot.run
16
15
  end
17
16
 
18
- desc 'watcher', 'Watches server and accounces when a player joins'
19
- method_option :endpoints, type: :array, alias: '-e', default: ['localhost:27500']
17
+ desc 'watcher', 'Watches servers and accounces when a player joins'
20
18
  def watcher
21
- watcher_bot = QwtfDiscordBotWatcher.new(options[:endpoints])
19
+ watcher_bot = QwtfDiscordBotWatcher.new
22
20
  watcher_bot.run
23
21
  end
24
22
  end
@@ -2,29 +2,30 @@ require 'qwtf_discord_bot/version'
2
2
  require 'qwtf_discord_bot/qwtf_discord_bot_server'
3
3
  require 'qwtf_discord_bot/qwtf_discord_bot_watcher'
4
4
  require 'discordrb'
5
+ require 'yaml'
5
6
 
6
7
  require 'qstat_request'
7
8
  require 'player'
8
9
  require 'team'
9
10
  require 'emoji'
10
11
  require 'roster'
12
+ require 'endpoint'
13
+
14
+ class QwtfDiscordBot # :nodoc:
15
+ ENV_VARS = %w[
16
+ QWTF_DISCORD_BOT_TOKEN
17
+ QWTF_DISCORD_BOT_CLIENT_ID
18
+ ].freeze
11
19
 
12
- class QwtfDiscordBot
13
- ENV_VARS = [
14
- 'QWTF_DISCORD_BOT_TOKEN',
15
- 'QWTF_DISCORD_BOT_CLIENT_ID',
16
- 'QWTF_DISCORD_BOT_CHANNEL_ID'
17
- ]
18
20
 
19
21
  if ENV_VARS.any? { |var| !ENV.key?(var) }
20
- raise "Environment variables not configured"
22
+ raise 'Environment variables not configured'
21
23
  end
22
24
 
23
25
  TOKEN = ENV['QWTF_DISCORD_BOT_TOKEN'].strip
24
26
  CLIENT_ID = ENV['QWTF_DISCORD_BOT_CLIENT_ID'].strip
25
- CHANNEL_ID = ENV['QWTF_DISCORD_BOT_CHANNEL_ID'].strip
26
27
 
27
- def initialize(endpoints)
28
- @endpoints = endpoints
28
+ def initialize
29
+ @endpoints = YAML.load_file('config/endpoints.yaml')
29
30
  end
30
31
  end
@@ -1,4 +1,4 @@
1
- class QwtfDiscordBotServer < QwtfDiscordBot
1
+ class QwtfDiscordBotServer < QwtfDiscordBot # :nodoc:
2
2
  def run
3
3
  bot = Discordrb::Commands::CommandBot.new(
4
4
  token: TOKEN,
@@ -7,11 +7,10 @@ class QwtfDiscordBotServer < QwtfDiscordBot
7
7
  )
8
8
 
9
9
  bot.command :server do |event, *args|
10
- return unless event.channel.id.to_s == CHANNEL_ID
11
-
12
10
  if args.empty?
13
11
  event.channel.send_message(
14
- "Provide a server address e.g. `!server sydney.fortressone.org` or use `!active` or `!all`"
12
+ 'Provide a server address e.g. `!server sydney.fortressone.org` ' \
13
+ 'or use `!active` or `!all`'
15
14
  )
16
15
  else
17
16
  endpoint = args.first
@@ -28,20 +27,20 @@ class QwtfDiscordBotServer < QwtfDiscordBot
28
27
  end
29
28
 
30
29
  bot.command :all do |event|
31
- return unless event.channel.id.to_s == CHANNEL_ID
30
+ @endpoints.each do |endpoint, channel_ids|
31
+ channel_ids.each do |channel_id|
32
+ next if event.channel.id != channel_id
32
33
 
33
- qstat_responses = @endpoints.map do |endpoint|
34
- QstatRequest.new(endpoint)
35
- end
34
+ qstat_request = QstatRequest.new(endpoint)
35
+ message = qstat_request.server_summary
36
+ embed = qstat_request.to_embed
36
37
 
37
- qstat_responses.each do |server|
38
- message = server.server_summary
39
- embed = server.to_embed
40
38
 
41
- if embed
42
- event.channel.send_embed(message, embed)
43
- else
44
- event.channel.send_message(message)
39
+ if embed
40
+ event.channel.send_embed(message, embed)
41
+ else
42
+ event.channel.send_message(message)
43
+ end
45
44
  end
46
45
  end
47
46
 
@@ -49,22 +48,16 @@ class QwtfDiscordBotServer < QwtfDiscordBot
49
48
  end
50
49
 
51
50
  bot.command :active do |event|
52
- return unless event.channel.id.to_s == CHANNEL_ID
51
+ @endpoints.each do |endpoint, channel_ids|
52
+ channel_ids.each do |channel_id|
53
+ next if event.channel.id != channel_id
53
54
 
54
- qstat_responses = @endpoints.map do |endpoint|
55
- QstatRequest.new(endpoint)
56
- end
55
+ qstat_request = QstatRequest.new(endpoint)
56
+ next if qstat_request.is_empty?
57
57
 
58
- servers_with_players = qstat_responses.select(&:has_players?)
58
+ message = qstat_request.server_summary
59
+ embed = qstat_request.to_embed
59
60
 
60
- if servers_with_players.empty?
61
- event.channel.send_message(
62
- "All ##{event.channel.name} servers are empty"
63
- )
64
- else
65
- servers_with_players.each do |server|
66
- message = server.server_summary
67
- embed = server.to_embed
68
61
 
69
62
  if embed
70
63
  event.channel.send_embed(message, embed)
@@ -4,18 +4,23 @@ class QwtfDiscordBotWatcher < QwtfDiscordBot
4
4
 
5
5
  def run
6
6
  every(THIRTY_SECONDS) do
7
- @endpoints.each do |endpoint|
7
+ @endpoints.each do |endpoint, channel_ids|
8
8
  request = QstatRequest.new(endpoint)
9
+ next if request.is_empty?
9
10
 
10
- unless request.is_empty?
11
- request.player_names.each do |name|
12
- unless seen_recently?(endpoint: endpoint, name: name)
13
- report_joined(name: name, server_summary: request.server_summary)
11
+ request.player_names.each do |name|
12
+ unless seen_recently?(endpoint: endpoint, name: name)
13
+ channel_ids.each do |channel_id|
14
+ report_joined(
15
+ name: name,
16
+ channel_id: channel_id,
17
+ server_summary: request.server_summary
18
+ )
14
19
  end
15
-
16
- history[endpoint] ||= {}
17
- history[endpoint][name] = Time.now
18
20
  end
21
+
22
+ history[endpoint] ||= {}
23
+ history[endpoint][name] = Time.now
19
24
  end
20
25
  end
21
26
  end
@@ -35,10 +40,10 @@ class QwtfDiscordBotWatcher < QwtfDiscordBot
35
40
  last_seen && (Time.now - last_seen < TEN_MINUTES)
36
41
  end
37
42
 
38
- def report_joined(name:, server_summary:)
43
+ def report_joined(name:, channel_id:, server_summary:)
39
44
  Discordrb::API::Channel.create_message(
40
45
  "Bot #{TOKEN}",
41
- CHANNEL_ID,
46
+ channel_id,
42
47
  "#{name} has joined #{server_summary}"
43
48
  )
44
49
  end
@@ -1,3 +1,3 @@
1
1
  class QwtfDiscordBot
2
- VERSION = '2.2.1'.freeze
2
+ VERSION = '3.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qwtf_discord_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sheldon Johnson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-19 00:00:00.000000000 Z
11
+ date: 2019-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb
@@ -66,6 +66,7 @@ files:
66
66
  - bin/rspec
67
67
  - bin/setup
68
68
  - bin/thor
69
+ - config/endpoints.yaml
69
70
  - emoji/blue_demoman.png
70
71
  - emoji/blue_engineer.png
71
72
  - emoji/blue_hwguy.png