twitch-bot 4.0.1 → 5.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: 8e4f03406f61c87e0daa72edc415d45fe13acfe5b33514c75a2028c119d52212
4
- data.tar.gz: 2e5336cff0e7304f91725d05ec7ff5e813ad54a0127554d0f3349d72d435d5d7
3
+ metadata.gz: 62d03f6a524a016a3c7472f473699677c203961d5c0db80d8345da5a2e48397d
4
+ data.tar.gz: c9a11d3c030bf1a9debf6bb66b06a18b80d814347b123dc41f070d4cd5670064
5
5
  SHA512:
6
- metadata.gz: e634d550cbd5b2dfb8f80216f11284c015aa9e6e78cb9a08557af848b92f1505f8ac66c70270ce5329aa6eccc2bc3b2f8fba39b690032f5b7e7c8d0941a5fe1f
7
- data.tar.gz: cc44b72bb4656a743321e5da2596da419e8eb078e759bea9742cfffa5a2047156caae7588a4cc3a3fcb7e302999a92b00c07a7531a9105d22137bc5a44268d04
6
+ metadata.gz: 34fe79b2b493e373bf79665dd001bda15e6c4bc87e6c70ba2a6bb0a34a6190b1800450ffcdfa850f167c29e8ec25a5a1bee2837116a9d1b93313f213b170475e
7
+ data.tar.gz: 9cdf0dde5c84e3fac9f5e1a37d8aaddddd284086e900390dac97f384c364a11a138cbad0bd098a96eaf5020a99b749a38149111890c9f798d169b46f77f895f1
File without changes
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog Twitch::Bot
2
2
 
3
+ ## v5.0.0
4
+
5
+ - [BREAKING] Complex values like arrays and hashes get serialized as JSON before
6
+ being stored in Redis.
7
+
8
+ ## v4.1.1
9
+
10
+ - [NEW] `CommandHandler` by default responds to `:user_message` events.
11
+
12
+ ## v4.1.0
13
+
14
+ * [NEW] New `CommandHandler` class that simplifies building chat commands.
15
+
3
16
  ## v4.0.1
4
17
 
5
18
  * [FIXED] Fixed test crash due to incomplete DotEnv initialization.
@@ -11,7 +11,7 @@ module Twitch
11
11
  # Represent the event triggered when quitting the client loop.
12
12
  class StopEvent < Twitch::Bot::Event
13
13
  def initialize
14
- @type = :stop
14
+ super(type: :stop)
15
15
  end
16
16
  end
17
17
 
@@ -22,7 +22,7 @@ module Twitch
22
22
  attr_reader :channel, :config, :memory
23
23
 
24
24
  def initialize(
25
- channel: nil, config:, &block
25
+ config:, channel: nil, &block
26
26
  )
27
27
  @config = config
28
28
  @channel = Twitch::Bot::Channel.new(channel) if channel
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Twitch
4
+ module Bot
5
+ # Base class for implementing chat commands
6
+ class CommandHandler < EventHandler
7
+ def self.handled_events
8
+ [:user_message]
9
+ end
10
+
11
+ def initialize(event:, client:)
12
+ super
13
+ @command_aliases = []
14
+ end
15
+
16
+ def call
17
+ if event.command? && command_aliases.include?(event.command)
18
+ handle_command
19
+ end
20
+ end
21
+
22
+ def command_alias(command_alias)
23
+ @command_aliases << command_alias
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :command_aliases
29
+
30
+ def handle_command
31
+ raise NotImplementedError
32
+ end
33
+ end
34
+ end
35
+ end
@@ -6,6 +6,21 @@ module Twitch
6
6
  class EventHandler
7
7
  attr_reader :event, :client
8
8
 
9
+ #
10
+ # Return a list of event types this handler subscribes to
11
+ #
12
+ # @return [Array] list of event types
13
+ #
14
+ def self.handled_events
15
+ []
16
+ end
17
+
18
+ #
19
+ # Inititalize an event handler object
20
+ #
21
+ # @parameter event The latest event of a subscribed type
22
+ # @parameter client The current chat client object
23
+ #
9
24
  def initialize(event:, client:)
10
25
  @event = event
11
26
  @client = client
@@ -19,15 +34,6 @@ module Twitch
19
34
  def call
20
35
  raise "Unhandled #{event.type}"
21
36
  end
22
-
23
- #
24
- # Return a list of event types this handler can handle
25
- #
26
- # @return [Array] event type list
27
- #
28
- def self.handled_events
29
- []
30
- end
31
37
  end
32
38
  end
33
39
  end
@@ -16,7 +16,7 @@ module Twitch
16
16
  end
17
17
 
18
18
  def self.logger
19
- @logger ||= ::Logger.new(STDOUT)
19
+ @logger ||= ::Logger.new($stdout)
20
20
  end
21
21
  end
22
22
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "redis"
4
+ require "json"
4
5
 
5
6
  module Twitch
6
7
  module Bot
@@ -13,11 +14,12 @@ module Twitch
13
14
  end
14
15
 
15
16
  def store(key, value)
16
- redis.set(key, value)
17
+ redis.set(key, value.to_json)
17
18
  end
18
19
 
19
20
  def retrieve(key)
20
- redis.get(key)
21
+ value = redis.get(key)
22
+ JSON.parse(value)
21
23
  end
22
24
 
23
25
  private
@@ -31,11 +33,9 @@ module Twitch
31
33
 
32
34
  def redis_config_url
33
35
  config = client.config
34
- if config.setting("redis_host")
35
- host = config.setting("redis_host") || "localhost"
36
- port = config.setting("redis_port") || 6379
37
- "redis://#{host}:#{port}"
38
- end
36
+ host = config.setting("redis_host") || "localhost"
37
+ port = config.setting("redis_port") || 6379
38
+ "redis://#{host}:#{port}"
39
39
  end
40
40
  end
41
41
  end
@@ -10,7 +10,7 @@ module Twitch
10
10
  def initialize(text:, user:)
11
11
  @text = text
12
12
  @user = user
13
- @type = :user_message
13
+ super(type: :user_message)
14
14
  end
15
15
 
16
16
  def command_name?(check_command)
@@ -5,8 +5,8 @@ module Twitch
5
5
  module Message
6
6
  # This class is the abstract base class for IRC events.
7
7
  class Base < Twitch::Bot::Event
8
- def initialize(_message)
9
- @type = :unknown
8
+ def initialize(type: :unknown)
9
+ super(type: type)
10
10
  end
11
11
  end
12
12
 
@@ -16,7 +16,7 @@ module Twitch
16
16
 
17
17
  def initialize(message)
18
18
  @message = message
19
- @type = :not_supported
19
+ super(type: :not_supported)
20
20
  end
21
21
  end
22
22
 
@@ -26,7 +26,7 @@ module Twitch
26
26
 
27
27
  def initialize(hostname:)
28
28
  @hostname = hostname
29
- @type = :ping
29
+ super(type: :ping)
30
30
  end
31
31
  end
32
32
 
@@ -37,21 +37,21 @@ module Twitch
37
37
  def initialize(user:, mode:)
38
38
  @user = user
39
39
  @mode = mode
40
- @type = :mode
40
+ super(type: :mode)
41
41
  end
42
42
  end
43
43
 
44
44
  # This class stores the details of an Authenticated event.
45
45
  class Authenticated < Base
46
46
  def initialize
47
- @type = :authenticated
47
+ super(type: :authenticated)
48
48
  end
49
49
  end
50
50
 
51
51
  # This class stores the details of a Join event.
52
52
  class Join < Base
53
53
  def initialize
54
- @type = :join
54
+ super(type: :join)
55
55
  end
56
56
  end
57
57
 
@@ -61,7 +61,7 @@ module Twitch
61
61
 
62
62
  def initialize(user:)
63
63
  @user = user
64
- @type = :subscription
64
+ super(type: :subscription)
65
65
  end
66
66
  end
67
67
 
@@ -71,7 +71,7 @@ module Twitch
71
71
 
72
72
  def initialize(user:)
73
73
  @user = user
74
- @type = :login_failed
74
+ super(type: :login_failed)
75
75
  end
76
76
  end
77
77
 
@@ -82,7 +82,7 @@ module Twitch
82
82
  def initialize(status:, channel:)
83
83
  @status = status
84
84
  @channel = channel
85
- @type = :slow_mode
85
+ super(type: :slow_mode)
86
86
  end
87
87
 
88
88
  def enabled?
@@ -96,7 +96,7 @@ module Twitch
96
96
 
97
97
  def initialize(status:)
98
98
  @status = status
99
- @type = :followers_only_mode
99
+ super(type: :followers_only_mode)
100
100
  end
101
101
  end
102
102
 
@@ -107,7 +107,7 @@ module Twitch
107
107
  def initialize(status:, channel:)
108
108
  @status = status
109
109
  @channel = channel
110
- @type = :subs_only_mode
110
+ super(type: :subs_only_mode)
111
111
  end
112
112
  end
113
113
 
@@ -118,7 +118,7 @@ module Twitch
118
118
  def initialize(status:, channel:)
119
119
  @status = status
120
120
  @channel = channel
121
- @type = :r9k_mode
121
+ super(type: :r9k_mode)
122
122
  end
123
123
  end
124
124
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Twitch
4
4
  module Bot
5
- VERSION = "4.0.1"
5
+ VERSION = "5.0.0"
6
6
  end
7
7
  end
data/lib/twitch/bot.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "bot/config"
8
8
  require_relative "bot/logger"
9
9
  require_relative "bot/event"
10
10
  require_relative "bot/event_handler"
11
+ require_relative "bot/command_handler"
11
12
  require_relative "bot/default_handlers"
12
13
  require_relative "bot/message"
13
14
  require_relative "bot/irc_message"
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Twitch::Bot::CommandHandler do
4
+ it "responds to user_message" do
5
+ expect(described_class.handled_events).to include(:user_message)
6
+ end
7
+
8
+ it "responds to a known command" do
9
+ config = Twitch::Bot::Config.new
10
+ client = Twitch::Bot::Client.new(config: config)
11
+ message = Twitch::Bot::Message::UserMessage.new(
12
+ text: "!mycommand test",
13
+ user: "testuser",
14
+ )
15
+ handler = described_class.new(event: message, client: client)
16
+ handler.command_alias("mycommand")
17
+ allow(handler).to receive(:handle_command)
18
+
19
+ handler.call
20
+
21
+ expect(handler).to have_received(:handle_command)
22
+ end
23
+ end
@@ -9,5 +9,26 @@ RSpec.describe Twitch::Bot::Memory::Hash do
9
9
 
10
10
  expect(mem.retrieve("foo")).to eq "bar"
11
11
  end
12
+
13
+ it "persists an Array" do
14
+ mem = described_class.new(client: nil)
15
+
16
+ mem.store("foo", [1, 2])
17
+
18
+ result = mem.retrieve("foo")
19
+ expect(result).to be_an Array
20
+ expect(result[0]).to eq 1
21
+ expect(result[1]).to eq 2
22
+ end
23
+
24
+ it "persists a Hash" do
25
+ mem = described_class.new(client: nil)
26
+
27
+ mem.store("foo", { "bar" => "baz" })
28
+
29
+ result = mem.retrieve("foo")
30
+ expect(result).to be_a Hash
31
+ expect(result["bar"]).to eq "baz"
32
+ end
12
33
  end
13
34
  end
@@ -40,5 +40,44 @@ RSpec.describe Twitch::Bot::Memory::Redis do
40
40
 
41
41
  expect(mem.retrieve("foo")).to eq "bar"
42
42
  end
43
+
44
+ it "persists an Array" do
45
+ config = Twitch::Bot::Config.new(
46
+ settings: {
47
+ bot_user: "testuser",
48
+ },
49
+ )
50
+ client = Twitch::Bot::Client.new(
51
+ config: config,
52
+ channel: "testchannel",
53
+ )
54
+ mem = described_class.new(client: client)
55
+
56
+ mem.store("foo", [1, 2])
57
+
58
+ result = mem.retrieve("foo")
59
+ expect(result).to be_an Array
60
+ expect(result[0]).to eq 1
61
+ expect(result[1]).to eq 2
62
+ end
63
+
64
+ it "persists a Hash" do
65
+ config = Twitch::Bot::Config.new(
66
+ settings: {
67
+ bot_user: "testuser",
68
+ },
69
+ )
70
+ client = Twitch::Bot::Client.new(
71
+ config: config,
72
+ channel: "testchannel",
73
+ )
74
+ mem = described_class.new(client: client)
75
+
76
+ mem.store("foo", { "bar" => "baz" })
77
+
78
+ result = mem.retrieve("foo")
79
+ expect(result).to be_a Hash
80
+ expect(result["bar"]).to eq "baz"
81
+ end
43
82
  end
44
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitch-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jochen Lillich
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-05 00:00:00.000000000 Z
11
+ date: 2022-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -174,7 +174,7 @@ extensions: []
174
174
  extra_rdoc_files: []
175
175
  files:
176
176
  - ".editorconfig"
177
- - ".env.dist"
177
+ - ".env.sample"
178
178
  - ".github/workflows/main.yml"
179
179
  - ".gitignore"
180
180
  - ".rspec"
@@ -194,6 +194,7 @@ files:
194
194
  - lib/twitch/bot/adapter/terminal.rb
195
195
  - lib/twitch/bot/channel.rb
196
196
  - lib/twitch/bot/client.rb
197
+ - lib/twitch/bot/command_handler.rb
197
198
  - lib/twitch/bot/config.rb
198
199
  - lib/twitch/bot/default_handlers.rb
199
200
  - lib/twitch/bot/event.rb
@@ -209,6 +210,7 @@ files:
209
210
  - spec/spec_helper.rb
210
211
  - spec/twitch/bot/adapter/terminal_spec.rb
211
212
  - spec/twitch/bot/client_spec.rb
213
+ - spec/twitch/bot/command_handler_spec.rb
212
214
  - spec/twitch/bot/config_spec.rb
213
215
  - spec/twitch/bot/memory/hash_spec.rb
214
216
  - spec/twitch/bot/memory/redis_spec.rb
@@ -219,7 +221,7 @@ homepage: https://github.com/geewiz/twitch-bot
219
221
  licenses:
220
222
  - MIT
221
223
  metadata: {}
222
- post_install_message:
224
+ post_install_message:
223
225
  rdoc_options: []
224
226
  require_paths:
225
227
  - lib
@@ -235,7 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
237
  version: '0'
236
238
  requirements: []
237
239
  rubygems_version: 3.0.3
238
- signing_key:
240
+ signing_key:
239
241
  specification_version: 4
240
242
  summary: twitch-bot is a Twitch chat client that uses Twitch IRC that can be used
241
243
  as a Twitch chat bot engine.
@@ -243,6 +245,7 @@ test_files:
243
245
  - spec/spec_helper.rb
244
246
  - spec/twitch/bot/adapter/terminal_spec.rb
245
247
  - spec/twitch/bot/client_spec.rb
248
+ - spec/twitch/bot/command_handler_spec.rb
246
249
  - spec/twitch/bot/config_spec.rb
247
250
  - spec/twitch/bot/memory/hash_spec.rb
248
251
  - spec/twitch/bot/memory/redis_spec.rb