Make event handlers optional to define
Check if event handlers have been defined before attempting to call them. This prevents the bot from crashing in the case that an event handler hasn't been defined.
This commit is contained in:
		
							parent
							
								
									f6b5ee4390
								
							
						
					
					
						commit
						8402bbdc80
					
				
					 1 changed files with 6 additions and 6 deletions
				
			
		| 
						 | 
					@ -70,13 +70,13 @@ module Ebooks
 | 
				
			||||||
      @stream.on_event(:follow) do |event|
 | 
					      @stream.on_event(:follow) do |event|
 | 
				
			||||||
        next if event[:source][:screen_name] == @username
 | 
					        next if event[:source][:screen_name] == @username
 | 
				
			||||||
        log "Followed by #{event[:source][:screen_name]}"
 | 
					        log "Followed by #{event[:source][:screen_name]}"
 | 
				
			||||||
        @on_follow.call(event[:source])
 | 
					        @on_follow.call(event[:source]) if @on_follow
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      @stream.on_direct_message do |dm|
 | 
					      @stream.on_direct_message do |dm|
 | 
				
			||||||
        next if dm[:sender][:screen_name] == @username # Don't reply to self
 | 
					        next if dm[:sender][:screen_name] == @username # Don't reply to self
 | 
				
			||||||
        log "DM from @#{dm[:sender][:screen_name]}: #{dm[:text]}"
 | 
					        log "DM from @#{dm[:sender][:screen_name]}: #{dm[:text]}"
 | 
				
			||||||
        @on_message.call(dm)
 | 
					        @on_message.call(dm) if @on_message
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      @stream.userstream do |ev|
 | 
					      @stream.userstream do |ev|
 | 
				
			||||||
| 
						 | 
					@ -110,9 +110,9 @@ module Ebooks
 | 
				
			||||||
        # - Or soft-retweeted by somebody else
 | 
					        # - Or soft-retweeted by somebody else
 | 
				
			||||||
        if mentions.map(&:downcase).include?(@username.downcase) && !ev[:retweeted_status] && !ev[:text].start_with?('RT ')
 | 
					        if mentions.map(&:downcase).include?(@username.downcase) && !ev[:retweeted_status] && !ev[:text].start_with?('RT ')
 | 
				
			||||||
          log "Mention from @#{ev[:user][:screen_name]}: #{ev[:text]}"
 | 
					          log "Mention from @#{ev[:user][:screen_name]}: #{ev[:text]}"
 | 
				
			||||||
          @on_mention.call(ev, meta)
 | 
					          @on_mention.call(ev, meta) if @on_mention
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
          @on_timeline.call(ev, meta)
 | 
					          @on_timeline.call(ev, meta) if @on_timeline
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue