Class: Agents::DiscordAgent

Inherits:
Agent
  • Object
show all
Includes:
FormConfigurable
Defined in:
lib/huginn_discord_agent/discord_agent.rb

Instance Method Summary collapse

Instance Method Details

#checkObject



120
121
122
# File 'lib/huginn_discord_agent/discord_agent.rb', line 120

def check
  trigger_action
end

#default_optionsObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/huginn_discord_agent/discord_agent.rb', line 59

def default_options
  {
    'content' => '',
    'type' => 'send_message',
    'bot_token' => '',
    'debug' => 'false',
    'channel_id' => '',
    'emit_events' => 'true',
    'expected_receive_period_in_days' => '7'
  }
end

#receive(incoming_events) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/huginn_discord_agent/discord_agent.rb', line 111

def receive(incoming_events)
  incoming_events.each do |event|
    interpolate_with(event) do
      log event
      trigger_action
    end
  end
end

#validate_optionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/huginn_discord_agent/discord_agent.rb', line 79

def validate_options
  errors.add(:base, "type has invalid value: should be 'send_message'") if interpolated['type'].present? && !%w(send_message).include?(interpolated['type'])

  unless options['channel_id'].present? || !['send_message'].include?(options['type'])
    errors.add(:base, "channel_id is a required field")
  end

  if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
    errors.add(:base, "if provided, emit_events must be true or false")
  end

  unless options['content'].present? || !['send_message'].include?(options['type'])
    errors.add(:base, "content is a required field")
  end

  unless options['bot_token'].present? || !['send_message'].include?(options['type'])
    errors.add(:base, "bot_token is a required field")
  end

  if options.has_key?('debug') && boolify(options['debug']).nil?
    errors.add(:base, "if provided, debug must be true or false")
  end

  unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
    errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
  end
end

#working?Boolean



107
108
109
# File 'lib/huginn_discord_agent/discord_agent.rb', line 107

def working?
  event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
end