Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reaction messages #728

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/discordrb/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def message_update(attributes = {}, &block)
# This **event** is raised when somebody reacts to a message.
# @param attributes [Hash] The event's attributes.
# @option attributes [String, Integer] :emoji Matches the ID of the emoji that was reacted with, or its name.
# @option attributes [String, Integer, User] :from Matches the user who added the reaction.
# @option attributes [String, Integer, Message] :message Matches the message to which the reaction was added.
# @option attributes [String, Integer, Channel] :in Matches the channel the reaction was added in.
# @yield The block is executed when the event is raised.
# @yieldparam event [ReactionAddEvent] The event that was raised.
# @return [ReactionAddEventHandler] The event handler that was registered.
Expand All @@ -137,6 +140,9 @@ def reaction_add(attributes = {}, &block)
# @param attributes [Hash] The event's attributes.
# @option attributes [String, Integer] :emoji Matches the ID of the emoji that was removed from the reactions, or
# its name.
# @option attributes [String, Integer, User] :from Matches the user who removed the reaction.
# @option attributes [String, Integer, Message] :message Matches the message to which the reaction was removed.
# @option attributes [String, Integer, Channel] :in Matches the channel the reaction was removed in.
# @yield The block is executed when the event is raised.
# @yieldparam event [ReactionRemoveEvent] The event that was raised.
# @return [ReactionRemoveEventHandler] The event handler that was registered.
Expand All @@ -146,6 +152,9 @@ def reaction_remove(attributes = {}, &block)

# This **event** is raised when somebody removes all reactions from a message.
# @param attributes [Hash] The event's attributes.
# @option attributes [Hash] The event's attributes.
# @option attributes [String, Integer, Message] :message Matches the message to which the reactions were removed.
# @option attributes [String, Integer, Channel] :in Matches the channel the reactions were removed in.
# @yield The block is executed when the event is raised.
# @yieldparam event [ReactionRemoveAllEvent] The event that was raised.
# @return [ReactionRemoveAllEventHandler] The event handler that was registered.
Expand Down
46 changes: 45 additions & 1 deletion lib/discordrb/events/reactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class ReactionEvent < Event
# @return [Emoji] the emoji that was reacted with.
attr_reader :emoji

# @!visibility private
attr_reader :message_id

def initialize(data, bot)
@bot = bot

Expand Down Expand Up @@ -61,6 +64,30 @@ def matches?(event)
else
e == a
end
end,
matches_all(@attributes[:message], event.message_id) do |a, e|
a == e
end,
matches_all(@attributes[:in], event.channel) do |a, e|
if a.is_a? String
# Make sure to remove the "#" from channel names in case it was specified
a.delete('#') == e.name
elsif a.is_a? Integer
a == e.id
else
a == e
end
end,
matches_all(@attributes[:from], event.user) do |a, e|
if a.is_a? String
a == e.name
elsif a.is_a? Integer
a == e
elsif a == :bot
e.current_bot?
else
a == e
end
end
].reduce(true, &:&)
end
Expand All @@ -82,6 +109,9 @@ class ReactionRemoveEventHandler < ReactionEventHandler; end
class ReactionRemoveAllEvent < Event
include Respondable

# @!visibility private
attr_reader :message_id

def initialize(data, bot)
@bot = bot

Expand All @@ -107,7 +137,21 @@ def matches?(event)
return false unless event.is_a? ReactionRemoveAllEvent

# No attributes yet as there is no property available on the event that doesn't involve doing a resolution request
[].reduce(true, &:&)
[
matches_all(@attributes[:message], event.message_id) do |a, e|
a == e
end,
matches_all(@attributes[:in], event.channel) do |a, e|
if a.is_a? String
# Make sure to remove the "#" from channel names in case it was specified
a.delete('#') == e.name
elsif a.is_a? Integer
a == e.id
else
a == e
end
end
].reduce(true, &:&)
end
end
end