twitchbot 0.0.2 → 0.0.3

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: 4eb24be58e5d670664da6a08c56cf3007ed3477b08c0d3e73bae57d7ee4bac29
4
- data.tar.gz: 1ae2d56adbb8bdd41529888c3abb7d7b9f509147beddedc0fe385e54104c5b73
3
+ metadata.gz: 4d0c016d500046220ef061dc024c9a98f201c59cc89e9536f09752b3f82df330
4
+ data.tar.gz: c1228859476211eaf4f8d4b4ac099fd56e21c181474c3a557a68b3daf6790c79
5
5
  SHA512:
6
- metadata.gz: a1354939320f1d974ab95107db9385e801aebf0f6b3c090d0f6a7d33f9db6c0a17bccd8e94179b6ecf8112d6f0a20623c7d3ce81a74f36c497c7170596dcdb64
7
- data.tar.gz: ac3048f9253dcac655ec2a99787057a3ff9e3a8937af159576d11087d784b1a61d2d168fba7d701ff362630eea4294610056bf99e2eb1979bfe27263eceb9b71
6
+ metadata.gz: 7f7cbee9b6ed3575acc0e9d77b0dbb40c426e6529f9cb829c41bea583793c00d085e26fc399f72c284bb61dea7a166f17be1ca39acff8cca1baaf3745a781010
7
+ data.tar.gz: a93d6c30bc1d1630b96cf1a92bcbd34d79f5432a868306d08ab9c9e17779496f746bdf01e90783aeccd3eeaef4fc11e545fdcf215629609448c77ce7dd744219
data/lib/twitchbot.rb CHANGED
@@ -3,3 +3,4 @@ require 'twitchbot/bot'
3
3
  require 'twitchbot/plugin'
4
4
  require 'twitchbot/message_plugin'
5
5
  require 'twitchbot/timed_plugin'
6
+ require 'twitchbot/whisper_plugin'
@@ -36,7 +36,7 @@ module Twitchbot
36
36
 
37
37
  # Add a whisper to the specified user to the message queue
38
38
  def send_whisper(user, message)
39
- @bot.message_queue.push("PRIVMSG jtv :/w #{user} :#{message}")
39
+ @bot.message_queue.push("PRIVMSG #jtv :/w #{user} #{message}")
40
40
  end
41
41
 
42
42
  # Method that provides a shortcut to grab the first message in an event
@@ -36,23 +36,26 @@ module Twitchbot
36
36
  @target = target
37
37
  @payload = payload
38
38
 
39
- if message?
39
+ if message? || whisper?
40
40
  @payload.slice! 0, 1
41
41
  @channel = @handler.bot.channel
42
- /bits=(?<bits>\d+)/ =~ @tags
43
- @bits = bits.nil? ? 0 : bits.to_i
44
- /display-name=(?<display_name>\w+)/ =~ @tags
45
- /user-id=(?<user_id>[a-zA-Z0-9\-]+)/ =~ @tags
46
- /badges=(?<badges>[a-zA-Z\/,0-9\-]+)/ =~ @tags
47
- badges = badges || ''
42
+ @display_name = @tags[/display-name=(\w+)/, 1]
43
+ @user_id = @tags[/user-id=(?<user_id>\d+)/, 1]
48
44
  /:(?<user>\w+)/ =~ @sender
49
45
  if @channel.users.key? user
50
- @channel.users[user].update_attributes display_name, user_id, badges
46
+ @channel.users[user].update_attributes @display_name, @user_id
51
47
  else
52
- @channel.users[user] = User.new user, display_name, user_id, badges
48
+ @channel.users[user] = User.new user, @display_name, @user_id
53
49
  end
54
50
  @user = @channel.users[user]
55
51
  end
52
+
53
+ if message?
54
+ /bits=(?<bits>\d+)/ =~ @tags
55
+ @bits = bits.nil? ? 0 : bits.to_i
56
+ /badges=(?<badges>[a-zA-Z\/,0-9\-]+)/ =~ @tags
57
+ @user.update_badges badges || ''
58
+ end
56
59
  end
57
60
 
58
61
  # Method to determine if the IRC message includes any tags from the +:twitch.tv/tags+ capability
@@ -65,9 +68,29 @@ module Twitchbot
65
68
  @command.eql? 'PRIVMSG'.freeze
66
69
  end
67
70
 
71
+ # Method to determine if the IRC message is a whisper to the bot
72
+ def whisper?
73
+ @command.eql? 'WHISPER'.freeze
74
+ end
75
+
68
76
  # Method to respond to the IRC message target with a private message
69
77
  def respond(message)
70
- @handler.bot.message_queue.push("PRIVMSG #{@target} :#{message}")
78
+ if message?
79
+ send_channel message
80
+ end
81
+ if whisper?
82
+ send_whisper @user, message
83
+ end
84
+ end
85
+
86
+ # Method to send a message to the joined [Channel]
87
+ def send_channel(message)
88
+ @handler.send_channel message
89
+ end
90
+
91
+ # Method to send a whisper to the specified [User]
92
+ def send_whisper(user, message)
93
+ @handler.send_whisper user, message
71
94
  end
72
95
 
73
96
  # Method to determine if the IRC message is a PING challenge
@@ -9,6 +9,7 @@ module Twitchbot
9
9
  def message(handler)
10
10
  handler.messages.each do |message|
11
11
  if message.message?
12
+ # TODO: Extract this block from WhisperPlugin and MessagePlugin
12
13
  prefix = handler.bot.command_prefix
13
14
  _, _command, arguments = message.payload.partition(
14
15
  /#{Regexp.escape prefix}\S+/
@@ -16,7 +17,7 @@ module Twitchbot
16
17
  command = _command.delete prefix
17
18
  commands = COMMANDS[self.class]
18
19
  if !command.nil? && !commands[command].nil?
19
- send(commands[command], message, arguments)
20
+ send(commands[command], message, arguments.lstrip)
20
21
  end
21
22
  end
22
23
  end
@@ -8,18 +8,26 @@ module Twitchbot
8
8
  # @return [Hash] Collection of known badges for the user
9
9
  attr_reader :badges
10
10
 
11
- def initialize(name, display_name = nil, id = nil, badges = nil)
11
+ def initialize(name, display_name = nil, id = nil)
12
12
  @name = name
13
13
  @display_name = display_name
14
14
  @id = id
15
- @badges = badges.nil? ? nil : process_badges(badges)
16
15
  end
17
16
 
18
17
  # Method to update the main attributes of a user
19
- def update_attributes(display_name, id, badges)
18
+ def update_attributes(display_name, id)
20
19
  @display_name = display_name
21
20
  @user_id = id
22
- @badges = process_badges(badges)
21
+ end
22
+
23
+ # Method to process the string representation of badges into a Hash so that
24
+ # we can query it for specific badges and levels of the badges
25
+ def update_badges(badge)
26
+ @badges = {}
27
+ badge.split(',').each do |_badge|
28
+ type, value = _badge.split '/'
29
+ @badges[type] = value.to_i
30
+ end
23
31
  end
24
32
 
25
33
  # Method to grab the best representation of a user
@@ -27,6 +35,11 @@ module Twitchbot
27
35
  @display_name || @name
28
36
  end
29
37
 
38
+ # Method to determine if the user is the broadcaster of the channel
39
+ def streamer?
40
+ @badges.key? 'broadcaster'
41
+ end
42
+
30
43
  # Method to determine if the user is a moderator of the channel
31
44
  def mod?
32
45
  @badges.key? 'moderator'
@@ -62,17 +75,6 @@ module Twitchbot
62
75
  @badges.key? 'bits-leader'
63
76
  end
64
77
 
65
- # Method to process the string representation of badges into a Hash so that
66
- # we can query it for specific badges and levels of the badges
67
- def process_badges(badges)
68
- badge = {}
69
- badges.split(',').each do |_badge|
70
- type, value = _badge.split '/'
71
- badge[type] = value.to_i
72
- end
73
- badge
74
- end
75
-
76
78
  def to_s
77
79
  name
78
80
  end
@@ -1,3 +1,3 @@
1
1
  module Twitchbot
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
@@ -0,0 +1,48 @@
1
+ module Twitchbot
2
+ module WhisperPlugin
3
+
4
+ COMMANDS = {}
5
+
6
+ # Method that can be overriden to react to the eventmachine +:open+ event
7
+ def open(handler) end
8
+
9
+ def message(handler)
10
+ handler.messages.each do |message|
11
+ if message.whisper?
12
+ # TODO: Extract this block from WhisperPlugin and MessagePlugin
13
+ prefix = handler.bot.command_prefix
14
+ _, _command, arguments = message.payload.partition(
15
+ /#{Regexp.escape prefix}\S+/
16
+ )
17
+ command = _command.delete prefix
18
+ commands = COMMANDS[self.class]
19
+ if !command.nil? && !commands[command].nil?
20
+ send(commands[command], message, arguments.lstrip)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ # Method that can be overriden to react to the eventmachine +:error+ event
27
+ def error(handler) end
28
+
29
+ # Method that can be overriden to react to the eventmachine +:close+ event
30
+ def close(handler) end
31
+
32
+ # Define a class method called register on each class that includes this
33
+ # module, which allows the user to add methods to the +COMMANDS+ constant
34
+ #
35
+ # def self.register(command:, method:)
36
+ # COMMANDS[base] = {} if COMMANDS[base].nil?
37
+ # COMMANDS[base][params[:command]] = params[:method]
38
+ # end
39
+ def self.included(klass)
40
+ klass.instance_eval do
41
+ define_singleton_method 'register' do |params|
42
+ COMMANDS[klass] = {} if COMMANDS[klass].nil?
43
+ COMMANDS[klass][params[:command]] = params[:method]
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitchbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Ray Shisler III
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-20 00:00:00.000000000 Z
11
+ date: 2019-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,6 +106,7 @@ files:
106
106
  - lib/twitchbot/timed_plugin.rb
107
107
  - lib/twitchbot/user.rb
108
108
  - lib/twitchbot/version.rb
109
+ - lib/twitchbot/whisper_plugin.rb
109
110
  homepage: https://github.com/craysiii/twitchbot
110
111
  licenses:
111
112
  - MIT