Class: Discordrb::Events::MessageEventHandler

Inherits:
EventHandler
  • Object
show all
Defined in:
lib/discordrb/events/message.rb

Overview

Event handler for MessageEvent

Instance Method Summary collapse

Methods inherited from EventHandler

#call, #initialize, #match, #matches_all

Constructor Details

This class inherits a constructor from Discordrb::Events::EventHandler

Instance Method Details

#matches?(event) ⇒ Boolean



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/discordrb/events/message.rb', line 32

def matches?(event)
  # Check for the proper event type
  return false unless event.is_a? MessageEvent

  [
    matches_all(@attributes[:starting_with] || @attributes[:start_with], event.content) do |a, e|
      if a.is_a? String
        e.start_with? a
      elsif a.is_a? Regexp
        (e =~ a) == 0
      end
    end,
    matches_all(@attributes[:ending_with] || @attributes[:end_with], event.content) do |a, e|
      if a.is_a? String
        e.end_with? a
      elsif a.is_a? Regexp
        a.match(e) ? e.end_with?(a.match(e)[-1]) : false
      end
    end,
    matches_all(@attributes[:containing] || @attributes[:contains], event.content) do |a, e|
      if a.is_a? String
        e.include? a
      elsif a.is_a? Regexp
        (e =~ a)
      end
    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? Fixnum
        a == e.id
      else
        a == e
      end
    end,
    matches_all(@attributes[:from], event.author) do |a, e|
      if a.is_a? String
        a == e.name
      elsif a.is_a? Fixnum
        a == e.id
      elsif a == :bot
        e.bot?
      else
        a == e
      end
    end,
    matches_all(@attributes[:with_text] || @attributes[:content], event.content) do |a, e|
      if a.is_a? String
        e == a
      elsif a.is_a? Regexp
        a.match(e) ? (e == (a.match(e)[0])) : false
      end
    end,
    matches_all(@attributes[:after], event.timestamp) { |a, e| a > e },
    matches_all(@attributes[:before], event.timestamp) { |a, e| a < e },
    matches_all(@attributes[:private], event.channel.private?) { |a, e| !e == !a }
  ].reduce(true, &:&)
end