Class: OnyxCord::Events::MessageEventHandler

Inherits:
EventHandler show all
Defined in:
lib/onyxcord/events/message/base.rb

Overview

Event handler for MessageEvent

Instance Method Summary collapse

Methods inherited from EventHandler

#call, #match, #matches_all

Instance Method Details

#after_call(event) ⇒ Object



294
295
296
297
298
299
300
# File 'lib/onyxcord/events/message/base.rb', line 294

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, filename: event.filename, spoiler: event.file_spoiler)
  end
end

#matches?(event) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/onyxcord/events/message/base.rb', line 215

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|
      case a
      when String
        e.start_with? a
      when Regexp
        (e =~ a)&.zero?
      end
    end,
    matches_all(@attributes[:ending_with] || @attributes[:end_with], event.content) do |a, e|
      case a
      when String
        e.end_with? a
      when Regexp
        !(e =~ Regexp.new("#{a}$")).nil?
      end
    end,
    matches_all(@attributes[:containing] || @attributes[:contains], event.content) do |a, e|
      case a
      when String
        e.include? a
      when Regexp
        (e =~ a)
      end
    end,
    matches_all(@attributes[:in], event.channel) do |a, e|
      case a
      when String
        # Make sure to remove the "#" from channel names in case it was specified
        a.delete('#') == e.name
      when Integer
        a == e.id
      else
        a == e
      end
    end,
    matches_all(@attributes[:from], event.message) do |a, e|
      # Resolve the author in the block in order to prevent resolving the author even when the attribute is `nil`
      e = e.author
      case a
      when String
        a == e.name
      when Integer
        a == e.id
      when :bot
        e.current_bot?
      else
        a == e
      end
    end,
    matches_all(@attributes[:with_text] || @attributes[:content] || @attributes[:exact_text], event.content) do |a, e|
      case a
      when String
        e == a
      when Regexp
        match = a.match(e)
        match ? (e == match[0]) : false
      end
    end,
    matches_all(@attributes[:type] || @attributes[:message_type], event.message.type) do |a, e|
      case a
      when String, Symbol
        OnyxCord::Message::TYPES[a.to_sym] == e
      when Integer
        a == e
      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 },
    matches_all(@attributes[:server], event.server) { |a, e| a&.resolve_id == e&.resolve_id }
  ].reduce(true, &:&)
end