-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiscord.rb
More file actions
53 lines (43 loc) · 1.27 KB
/
discord.rb
File metadata and controls
53 lines (43 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'discordrb/webhooks'
class FakeDiscord
def initialize(logger)
@logger = logger
end
def update(str)
@logger.info "[FakeDiscord]: #{str}"
end
class DiscordClient
def initialize(logger)
@logger = logger
@discord = if ENV['ENVIRONMENT'] != 'test' && env_keys_exist?
Discordrb::Webhooks::Client.new(url: ENV['DISCORD_WEBHOOK_URL']).freeze
end
end
def env_keys_exist?
ENV['DISCORD_WEBHOOK_URL']
end
def send(clinic)
@logger.info "[DiscordClient] Sending message for #{clinic.title} (#{clinic.new_appointments} new appointments)"
text = clinic.discord_text
if text.is_a?(Array)
text.each { |t| @discord.execute do |builder|
builder.content = text
end
}
end
@discord.execute do |builder|
builder.content = text
end
end
rescue => e
@logger.error "[DiscordClient] error: #{e}"
raise e unless ENV['ENVIRONMENT'] == 'production' || ENV['ENVIRONMENT'] == 'staging'
Sentry.capture_exception(e)
end
def post(clinics)
clinics.filter(&:should_discord_message?).each do |clinic|
send(clinic)
clinic.save_message_time
end
end
end