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

#after_call(event) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/discordrb/events/message.rb', line 214

def after_call(event)
  if event.file.nil?
    event.send_message(event.saved_message) unless event.saved_message.empty?
  else
    event.send_file(event.file, caption: event.saved_message)
  end
end

#matches?(event) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/discordrb/events/message.rb', line 152

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) && (e =~ a).zero?
      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? Integer
        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? Integer
        a == e.id
      elsif a == :bot
        e.current_bot?
      else
        a == e
      end
    end,
    matches_all(@attributes[:with_text] || @attributes[:content] || @attributes[:exact_text], event.content) do |a, e|
      if a.is_a? String
        e == a
      elsif a.is_a? Regexp
        match = a.match(e)
        match ? (e == match[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