qwtf_discord_bot 3.0.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d474c40842701016d9431def0c11f0802e1e9f760b8cd46906b6c1813165da69
4
- data.tar.gz: bbee37ae5f25bbc63395ac866704994c1e6bdbfc7399ea12e7147f44c918c1f6
3
+ metadata.gz: 53b110507d7aea432897ecfd3ee11c4837df5282b88941163e4fb8dd9b67aa83
4
+ data.tar.gz: ffe4cefa3aa20ba3f091f157152b083929584a2331be84bb80d20cf3ada2786c
5
5
  SHA512:
6
- metadata.gz: 3130bb1aa93eac85706b2294f0bf607061bc3ae3f6ad17c76b83dc76fd302d69014aab4448b3f02878fdf878558b21e0a8415c6a79a9157b45b2ad55624a8184
7
- data.tar.gz: 998f5873f02adafd23ad4bfded8728b0df7c0a7ca5507a348cf642c2291e1239fd1a61c887d7c025104bdb3f003ce6c1f658db3d85dc73b18541e55d6873d395
6
+ metadata.gz: f0701391ba3c5b82dfcef5188af67aeb70db9cb97503c449a4940fd9358512ff65f575fdebac353175b95fcbaac7b3f208cadc0f992921e954f6f1ada6830f5c
7
+ data.tar.gz: a144dbdef8a1f08260501051e3667069a9f821d8718ec7f3f59dad1d8ab6b6a75fc2ae849e5ba0ae6b54f325421262a066ffbba9053deab9cccd8bc8b03d53b5
data/.gitignore CHANGED
@@ -14,3 +14,4 @@ assets/
14
14
  qstat
15
15
  *.gem
16
16
  *.swp
17
+ config.yaml
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qwtf_discord_bot (3.0.0)
4
+ qwtf_discord_bot (4.0.0)
5
5
  discordrb (~> 3.3)
6
6
  thor (~> 0.20)
7
7
 
data/README.md CHANGED
@@ -16,45 +16,55 @@ A Discord bot for checking the status of QuakeWorld Team Fortress servers
16
16
 
17
17
  ## Usage
18
18
 
19
- Create a bot on discord.
20
-
21
- Edit the `.env.example` file, update with your bot's `client_id` and `token` and save as `.env`.
19
+ - Create a bot on discord.
20
+ - Create a `config.yaml` file containing your bots credentials and server endpoints. E.G.
21
+
22
+ ```yaml
23
+ ---
24
+ token: "dhjksahdkjhhur43hu4hu5b4b5k34j12b4kj3b4kjb4j32kb4kjb4kb3k2b"
25
+ client_id: "873298379487294398"
26
+ endpoints:
27
+ - "sydney.fortressone.org":
28
+ - channel_ids:
29
+ - 832749832749873298
30
+ - 798437748937298448
31
+ - "sydney.fortressone.org:27501":
32
+ - channel_ids:
33
+ - 590204247224745986
34
+ - "dallas.fortressone.org":
35
+ - channel_ids:
36
+ - 480928490328409328
37
+ ```
22
38
 
23
- $ source .env
39
+ - Set the `QWTF_DISCORD_BOT_CONFIG_FILE` environment variable or default to
40
+ `config.yaml` in the present working directory
24
41
 
42
+ ```sh
43
+ $ export QWTF_DISCORD_BOT_CONFIG_FILE="config.yaml"
44
+ ```
25
45
 
26
- ### List commands
27
46
 
28
- $ bundle exec exe/qwtf-discord-bot help
29
47
 
30
- There are two features:
48
+ ### Commands
31
49
 
50
+ There are two modules:
32
51
 
33
- ### Server
34
52
 
35
- This responds to `!servers` messages by providing information about your game
36
- server. Defaults to the hostname command line argument, but will accept a
37
- hostname from the user. I.E. `!server fortressone.org`
53
+ #### Server
38
54
 
39
- ![screenshot of bot responding to !server command](server_screenshot.png)
55
+ $ qwtf-discord-bot server
40
56
 
41
- ```
42
- Usage:
43
- qwtf_discord_bot server
44
-
45
- Options:
46
- [--endpoints=one two three]
47
- # Default: ["localhost:27500"]
48
- ```
57
+ This responds to discord messages:
58
+ - `!server`
59
+ - `!active`
60
+ - `!all`
49
61
 
50
- E.G.
62
+ ![screenshot of bot responding to !server command](server_screenshot.png)
51
63
 
52
- $ qwtf-discord-bot server --endpoints \
53
- sydney.fortressone.org:27500 \
54
- sydney.fortressone.org:27501
55
64
 
65
+ #### Watcher
56
66
 
57
- ### Watcher
67
+ $ qwtf-discord-bot watcher
58
68
 
59
69
  This watches the game server and announces if anyone has joined the server. It
60
70
  polls the server once every 30 seconds and will only report a player joining if
@@ -62,19 +72,6 @@ they haven't been connected for more than ten minutes.
62
72
 
63
73
  ![screenshot of bot reporting player joining server](watcher_screenshot.png)
64
74
 
65
- ```
66
- Usage:
67
- qwtf_discord_bot watcher
68
-
69
- Options:
70
- [--endpoints=one two three]
71
- # Default: ["localhost:27500"]
72
- ```
73
-
74
- E.G.
75
-
76
- $ bundle exec exe/qwtf-discord-bot watcher --endpoints sydney.fortressone.org:27501
77
-
78
75
 
79
76
  ## License
80
77
 
@@ -0,0 +1,43 @@
1
+ class Config
2
+ def initialize(config)
3
+ @config = YAML.load_file(config)
4
+ end
5
+
6
+ def token
7
+ @token ||= @config["token"]
8
+ end
9
+
10
+ def client_id
11
+ @client_id ||= @config["client_id"]
12
+ end
13
+
14
+ def endpoints
15
+ @endpoints ||= @config["endpoints"].map do |endpoint|
16
+ Endpoint.new(endpoint)
17
+ end
18
+ end
19
+ end
20
+
21
+ class Endpoint
22
+ def initialize(config)
23
+ @config = config
24
+ end
25
+
26
+ def address
27
+ @config.keys.first
28
+ end
29
+
30
+ def channel_ids
31
+ channel_ids = []
32
+
33
+ @config.values.each do |settings|
34
+ settings.each do |setting|
35
+ setting["channel_ids"].each do |channel_id|
36
+ channel_ids << channel_id
37
+ end
38
+ end
39
+ end
40
+
41
+ channel_ids
42
+ end
43
+ end
@@ -4,28 +4,20 @@ require 'qwtf_discord_bot/qwtf_discord_bot_watcher'
4
4
  require 'discordrb'
5
5
  require 'yaml'
6
6
 
7
+ require 'config'
7
8
  require 'qstat_request'
8
9
  require 'player'
9
10
  require 'team'
10
11
  require 'emoji'
11
12
  require 'roster'
12
- require 'endpoint'
13
13
 
14
14
  class QwtfDiscordBot # :nodoc:
15
- ENV_VARS = %w[
16
- QWTF_DISCORD_BOT_TOKEN
17
- QWTF_DISCORD_BOT_CLIENT_ID
18
- ].freeze
19
-
20
-
21
- if ENV_VARS.any? { |var| !ENV.key?(var) }
22
- raise 'Environment variables not configured'
23
- end
24
-
25
- TOKEN = ENV['QWTF_DISCORD_BOT_TOKEN'].strip
26
- CLIENT_ID = ENV['QWTF_DISCORD_BOT_CLIENT_ID'].strip
15
+ CONFIG_FILE = ENV['QWTF_DISCORD_BOT_CONFIG_FILE'] || "#{Dir.pwd}/config.yaml"
27
16
 
28
17
  def initialize
29
- @endpoints = YAML.load_file('config/endpoints.yaml')
18
+ @config = Config.new(CONFIG_FILE)
19
+ @token = @config.token
20
+ @client_id = @config.client_id
21
+ @endpoints = @config.endpoints
30
22
  end
31
23
  end
@@ -1,8 +1,8 @@
1
1
  class QwtfDiscordBotServer < QwtfDiscordBot # :nodoc:
2
2
  def run
3
3
  bot = Discordrb::Commands::CommandBot.new(
4
- token: TOKEN,
5
- client_id: CLIENT_ID,
4
+ token: @token,
5
+ client_id: @client_id,
6
6
  prefix: '!'
7
7
  )
8
8
 
@@ -27,11 +27,11 @@ class QwtfDiscordBotServer < QwtfDiscordBot # :nodoc:
27
27
  end
28
28
 
29
29
  bot.command :all do |event|
30
- @endpoints.each do |endpoint, channel_ids|
31
- channel_ids.each do |channel_id|
32
- next if event.channel.id != channel_id
30
+ @endpoints.each do |endpoint|
31
+ endpoint.channel_ids.each do |channel_id|
32
+ next unless event.channel.id == channel_id
33
33
 
34
- qstat_request = QstatRequest.new(endpoint)
34
+ qstat_request = QstatRequest.new(endpoint.address)
35
35
  message = qstat_request.server_summary
36
36
  embed = qstat_request.to_embed
37
37
 
@@ -48,11 +48,11 @@ class QwtfDiscordBotServer < QwtfDiscordBot # :nodoc:
48
48
  end
49
49
 
50
50
  bot.command :active do |event|
51
- @endpoints.each do |endpoint, channel_ids|
52
- channel_ids.each do |channel_id|
53
- next if event.channel.id != channel_id
51
+ @endpoints.each do |endpoint|
52
+ endpoint.channel_ids.each do |channel_id|
53
+ next unless event.channel.id == channel_id
54
54
 
55
- qstat_request = QstatRequest.new(endpoint)
55
+ qstat_request = QstatRequest.new(endpoint.address)
56
56
  next if qstat_request.is_empty?
57
57
 
58
58
  message = qstat_request.server_summary
@@ -4,13 +4,14 @@ class QwtfDiscordBotWatcher < QwtfDiscordBot
4
4
 
5
5
  def run
6
6
  every(THIRTY_SECONDS) do
7
- @endpoints.each do |endpoint, channel_ids|
8
- request = QstatRequest.new(endpoint)
7
+ @endpoints.each do |endpoint|
8
+ address = endpoint.address
9
+ request = QstatRequest.new(address)
9
10
  next if request.is_empty?
10
11
 
11
12
  request.player_names.each do |name|
12
- unless seen_recently?(endpoint: endpoint, name: name)
13
- channel_ids.each do |channel_id|
13
+ unless seen_recently?(endpoint: address, name: name)
14
+ endpoint.channel_ids.each do |channel_id|
14
15
  report_joined(
15
16
  name: name,
16
17
  channel_id: channel_id,
@@ -19,8 +20,8 @@ class QwtfDiscordBotWatcher < QwtfDiscordBot
19
20
  end
20
21
  end
21
22
 
22
- history[endpoint] ||= {}
23
- history[endpoint][name] = Time.now
23
+ history[address] ||= {}
24
+ history[address][name] = Time.now
24
25
  end
25
26
  end
26
27
  end
@@ -42,7 +43,7 @@ class QwtfDiscordBotWatcher < QwtfDiscordBot
42
43
 
43
44
  def report_joined(name:, channel_id:, server_summary:)
44
45
  Discordrb::API::Channel.create_message(
45
- "Bot #{TOKEN}",
46
+ "Bot #{@token}",
46
47
  channel_id,
47
48
  "#{name} has joined #{server_summary}"
48
49
  )
@@ -1,3 +1,3 @@
1
1
  class QwtfDiscordBot
2
- VERSION = '3.0.0'.freeze
2
+ VERSION = '4.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qwtf_discord_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sheldon Johnson
@@ -66,7 +66,6 @@ files:
66
66
  - bin/rspec
67
67
  - bin/setup
68
68
  - bin/thor
69
- - config/endpoints.yaml
70
69
  - emoji/blue_demoman.png
71
70
  - emoji/blue_engineer.png
72
71
  - emoji/blue_hwguy.png
@@ -86,6 +85,7 @@ files:
86
85
  - emoji/red_soldier.png
87
86
  - emoji/red_spy.png
88
87
  - exe/qwtf_discord_bot
88
+ - lib/config.rb
89
89
  - lib/emoji.rb
90
90
  - lib/player.rb
91
91
  - lib/qstat_request.rb
@@ -1,5 +0,0 @@
1
- ---
2
- # Add each endpoint below the in format
3
- # "<hostname>:<port>":
4
- # - CHANNEL_ID_1
5
- # - CHANNEL_ID_2