discordrb 1.5.4 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of discordrb might be problematic. Click here for more details.

@@ -0,0 +1,54 @@
1
+ require 'discordrb/events/generic'
2
+
3
+ module Discordrb::Events
4
+ # Raised when a user is banned
5
+ class UserBanEvent
6
+ # @return [User] the user that was banned
7
+ attr_reader :user
8
+
9
+ # @return [Server] the server from which the user was banned
10
+ attr_reader :server
11
+
12
+ # @!visibility private
13
+ def initialize(data, bot)
14
+ @user = bot.user(data['user']['id'].to_i)
15
+ @server = bot.server(data['guild_id'].to_i)
16
+ @bot = bot
17
+ end
18
+ end
19
+
20
+ # Event handler for {UserBanEvent}
21
+ class UserBanEventHandler < EventHandler
22
+ def matches?(event)
23
+ # Check for the proper event type
24
+ return false unless event.is_a? UserBanEvent
25
+
26
+ [
27
+ matches_all(@attributes[:user], event.user) do |a, e|
28
+ if a.is_a? String
29
+ a == e.name
30
+ elsif a.is_a? Integer
31
+ a == e.id
32
+ elsif a == :bot
33
+ e.bot?
34
+ else
35
+ a == e
36
+ end
37
+ end,
38
+ matches_all(@attributes[:server], event.server) do |a, e|
39
+ if a.is_a? String
40
+ a == e.name
41
+ elsif a.is_a? Integer
42
+ a == e.id
43
+ else
44
+ a == e
45
+ end
46
+ end
47
+ ].reduce(true, &:&)
48
+ end
49
+ end
50
+
51
+ # Raised when a user is unbanned from a server
52
+ class UserUnbanEvent < UserBanEvent; end
53
+ class UserUnbanEventHandler < UserBanEventHandler; end
54
+ end
@@ -25,18 +25,18 @@ module Discordrb::Events
25
25
 
26
26
  [
27
27
  matches_all(@attributes[:type], event.type) do |a, e|
28
- if a.is_a? String
29
- a == e.name
30
- else
31
- a == e
32
- end
28
+ a == if a.is_a? String
29
+ e.name
30
+ else
31
+ e
32
+ end
33
33
  end,
34
34
  matches_all(@attributes[:name], event.name) do |a, e|
35
- if a.is_a? String
36
- a == e.to_s
37
- else
38
- a == e
39
- end
35
+ a == if a.is_a? String
36
+ e.to_s
37
+ else
38
+ e
39
+ end
40
40
  end
41
41
  ].reduce(true, &:&)
42
42
  end
@@ -25,18 +25,18 @@ module Discordrb::Events
25
25
 
26
26
  [
27
27
  matches_all(@attributes[:type], event.type) do |a, e|
28
- if a.is_a? String
29
- a == e.name
30
- else
31
- a == e
32
- end
28
+ a == if a.is_a? String
29
+ e.name
30
+ else
31
+ e
32
+ end
33
33
  end,
34
34
  matches_all(@attributes[:name], event.name) do |a, e|
35
- if a.is_a? String
36
- a == e.to_s
37
- else
38
- a == e
39
- end
35
+ a == if a.is_a? String
36
+ e.to_s
37
+ else
38
+ e
39
+ end
40
40
  end
41
41
  ].reduce(true, &:&)
42
42
  end
@@ -27,18 +27,18 @@ module Discordrb::Events
27
27
 
28
28
  [
29
29
  matches_all(@attributes[:type], event.type) do |a, e|
30
- if a.is_a? String
31
- a == e.name
32
- else
33
- a == e
34
- end
30
+ a == if a.is_a? String
31
+ e.name
32
+ else
33
+ e
34
+ end
35
35
  end,
36
36
  matches_all(@attributes[:name], event.name) do |a, e|
37
- if a.is_a? String
38
- a == e.to_s
39
- else
40
- a == e
41
- end
37
+ a == if a.is_a? String
38
+ e.to_s
39
+ else
40
+ e
41
+ end
42
42
  end
43
43
  ].reduce(true, &:&)
44
44
  end
@@ -23,11 +23,11 @@ module Discordrb::Events
23
23
 
24
24
  [
25
25
  matches_all(@attributes[:name], event.name) do |a, e|
26
- if a.is_a? String
27
- a == e.to_s
28
- else
29
- a == e
30
- end
26
+ a == if a.is_a? String
27
+ e.to_s
28
+ else
29
+ e
30
+ end
31
31
  end
32
32
  ].reduce(true, &:&)
33
33
  end
@@ -24,11 +24,11 @@ module Discordrb::Events
24
24
 
25
25
  [
26
26
  matches_all(@attributes[:name], event.name) do |a, e|
27
- if a.is_a? String
28
- a == e.to_s
29
- else
30
- a == e
31
- end
27
+ a == if a.is_a? String
28
+ e.to_s
29
+ else
30
+ e
31
+ end
32
32
  end
33
33
  ].reduce(true, &:&)
34
34
  end
@@ -23,11 +23,11 @@ module Discordrb::Events
23
23
 
24
24
  [
25
25
  matches_all(@attributes[:name], event.name) do |a, e|
26
- if a.is_a? String
27
- a == e.to_s
28
- else
29
- a == e
30
- end
26
+ a == if a.is_a? String
27
+ e.to_s
28
+ else
29
+ e
30
+ end
31
31
  end
32
32
  ].reduce(true, &:&)
33
33
  end
@@ -12,7 +12,6 @@ module Discordrb::Events
12
12
 
13
13
  def init_server(data, bot)
14
14
  @server = bot.server(data['id'].to_i)
15
- return unless @server
16
15
  end
17
16
  end
18
17
 
@@ -24,13 +23,13 @@ module Discordrb::Events
24
23
 
25
24
  [
26
25
  matches_all(@attributes[:server], event.server) do |a, e|
27
- if a.is_a? String
28
- a == e.name
29
- elsif a.is_a? Fixnum
30
- a == e.id
31
- else
32
- a == e
33
- end
26
+ a == if a.is_a? String
27
+ e.name
28
+ elsif a.is_a? Fixnum
29
+ e.id
30
+ else
31
+ e
32
+ end
34
33
  end
35
34
  ].reduce(true, &:&)
36
35
  end
@@ -39,11 +39,11 @@ module Discordrb::Events
39
39
 
40
40
  [
41
41
  matches_all(@attributes[:username], event.user.name) do |a, e|
42
- if a.is_a? String
43
- a == e.to_s
44
- else
45
- a == e
46
- end
42
+ a == if a.is_a? String
43
+ e.to_s
44
+ else
45
+ e
46
+ end
47
47
  end
48
48
  ].reduce(true, &:&)
49
49
  end
@@ -95,4 +95,52 @@ module Discordrb::Events
95
95
 
96
96
  class PrivateMessageEvent < MessageEvent; end
97
97
  class PrivateMessageEventHandler < MessageEventHandler; end
98
+
99
+ # A subset of MessageEvent that only contains a message ID and a channel
100
+ class MessageIDEvent
101
+ # @return [Integer] the ID associated with this event
102
+ attr_reader :id
103
+
104
+ # @return [Channel] the channel in which this event occurred
105
+ attr_reader :channel
106
+
107
+ # @!visibility private
108
+ def initialize(data, bot)
109
+ @id = data['id'].to_i
110
+ @channel = bot.channel(data['channel_id'].to_i)
111
+ @bot = bot
112
+ end
113
+ end
114
+
115
+ # Event handler for {MessageIDEvent}
116
+ class MessageIDEventHandler < EventHandler
117
+ def matches?(event)
118
+ # Check for the proper event type
119
+ return false unless event.is_a? MessageIDEvent
120
+
121
+ [
122
+ matches_all(@attributes[:id], event.id) do |a, e|
123
+ a.resolve_id == e.resolve_id
124
+ end,
125
+ matches_all(@attributes[:in], event.channel) do |a, e|
126
+ if a.is_a? String
127
+ # Make sure to remove the "#" from channel names in case it was specified
128
+ a.delete('#') == e.name
129
+ elsif a.is_a? Integer
130
+ a == e.id
131
+ else
132
+ a == e
133
+ end
134
+ end
135
+ ].reduce(true, &:&)
136
+ end
137
+ end
138
+
139
+ # Raised when a message is edited
140
+ class MessageEditEvent < MessageIDEvent; end
141
+ class MessageEditEventHandler < MessageIDEventHandler; end
142
+
143
+ # Raised when a message is deleted
144
+ class MessageDeleteEvent < MessageIDEvent; end
145
+ class MessageDeleteEventHandler < MessageIDEventHandler; end
98
146
  end
@@ -21,20 +21,20 @@ module Discordrb::Events
21
21
 
22
22
  [
23
23
  matches_all(@attributes[:from], event.user) do |a, e|
24
- if a.is_a? String
25
- a == e.name
26
- elsif a.is_a? Fixnum
27
- a == e.id
28
- else
29
- a == e
30
- end
24
+ a == if a.is_a? String
25
+ e.name
26
+ elsif a.is_a? Fixnum
27
+ e.id
28
+ else
29
+ e
30
+ end
31
31
  end,
32
32
  matches_all(@attributes[:status], event.status) do |a, e|
33
- if a.is_a? String
34
- a == e.to_s
35
- else
36
- a == e
37
- end
33
+ a == if a.is_a? String
34
+ e.to_s
35
+ else
36
+ e
37
+ end
38
38
  end
39
39
  ].reduce(true, &:&)
40
40
  end
@@ -47,11 +47,7 @@ module Discordrb::Events
47
47
  def initialize(data, bot)
48
48
  @user = bot.user(data['user']['id'].to_i)
49
49
 
50
- if data['game']
51
- @game = data['game']['name']
52
- else
53
- @game = nil
54
- end
50
+ @game = data['game'] ? data['game']['name'] : nil
55
51
 
56
52
  @server = bot.server(data['guild_id'].to_i)
57
53
  end
@@ -65,20 +61,20 @@ module Discordrb::Events
65
61
 
66
62
  [
67
63
  matches_all(@attributes[:from], event.user) do |a, e|
68
- if a.is_a? String
69
- a == e.name
70
- elsif a.is_a? Fixnum
71
- a == e.id
72
- else
73
- a == e
74
- end
64
+ a == if a.is_a? String
65
+ e.name
66
+ elsif a.is_a? Fixnum
67
+ e.id
68
+ else
69
+ e
70
+ end
75
71
  end,
76
72
  matches_all(@attributes[:game], event.game) do |a, e|
77
- if a.is_a? String
78
- a == e.name
79
- else
80
- a == e
81
- end
73
+ a == if a.is_a? String
74
+ e.name
75
+ else
76
+ e
77
+ end
82
78
  end
83
79
  ].reduce(true, &:&)
84
80
  end
@@ -31,13 +31,13 @@ module Discordrb::Events
31
31
  end
32
32
  end,
33
33
  matches_all(@attributes[:from], event.user) do |a, e|
34
- if a.is_a? String
35
- a == e.name
36
- elsif a.is_a? Fixnum
37
- a == e.id
38
- else
39
- a == e
40
- end
34
+ a == if a.is_a? String
35
+ e.name
36
+ elsif a.is_a? Fixnum
37
+ e.id
38
+ else
39
+ e
40
+ end
41
41
  end,
42
42
  matches_all(@attributes[:after], event.timestamp) { |a, e| a > e },
43
43
  matches_all(@attributes[:before], event.timestamp) { |a, e| a < e }
@@ -30,50 +30,50 @@ module Discordrb::Events
30
30
 
31
31
  [
32
32
  matches_all(@attributes[:from], event.user) do |a, e|
33
- if a.is_a? String
34
- a == e.name
35
- elsif a.is_a? Fixnum
36
- a == e.id
37
- else
38
- a == e
39
- end
33
+ a == if a.is_a? String
34
+ e.name
35
+ elsif a.is_a? Integer
36
+ e.id
37
+ else
38
+ e
39
+ end
40
40
  end,
41
41
  matches_all(@attributes[:mute], event.mute) do |a, e|
42
- if a.is_a? Boolean
43
- a == e.to_s
44
- else
45
- a == e
46
- end
42
+ a == if a.is_a?(TrueClass) || a.is_a?(FalseClass)
43
+ e.to_s
44
+ else
45
+ e
46
+ end
47
47
  end,
48
48
  matches_all(@attributes[:deaf], event.deaf) do |a, e|
49
- if a.is_a? Boolean
50
- a == e.to_s
51
- else
52
- a == e
53
- end
49
+ a == if a.is_a?(TrueClass) || a.is_a?(FalseClass)
50
+ e.to_s
51
+ else
52
+ e
53
+ end
54
54
  end,
55
55
  matches_all(@attributes[:self_mute], event.self_mute) do |a, e|
56
- if a.is_a? Boolean
57
- a == e.to_s
58
- else
59
- a == e
60
- end
56
+ a == if a.is_a?(TrueClass) || a.is_a?(FalseClass)
57
+ e.to_s
58
+ else
59
+ e
60
+ end
61
61
  end,
62
62
  matches_all(@attributes[:self_deaf], event.self_deaf) do |a, e|
63
- if a.is_a? Boolean
64
- a == e.to_s
65
- else
66
- a == e
67
- end
63
+ a == if a.is_a?(TrueClass) || a.is_a?(FalseClass)
64
+ e.to_s
65
+ else
66
+ e
67
+ end
68
68
  end,
69
69
  matches_all(@attributes[:channel], event.channel) do |a, e|
70
- if a.is_a? String
71
- a == e.name
72
- elsif a.is_a? Fixnum
73
- a == e.id
74
- else
75
- a == e
76
- end
70
+ a == if a.is_a? String
71
+ e.name
72
+ elsif a.is_a? Fixnum
73
+ e.id
74
+ else
75
+ e
76
+ end
77
77
  end
78
78
  ].reduce(true, &:&)
79
79
  end