Class: Clacky::Channel::Adapters::Discord::ApiClient
- Inherits:
-
Object
- Object
- Clacky::Channel::Adapters::Discord::ApiClient
- Defined in:
- lib/clacky/server/channel/adapters/discord/api_client.rb
Defined Under Namespace
Classes: ApiError
Constant Summary collapse
- DEFAULT_HOMEPAGE_URL =
"https://discord.com"- BASE_URL =
"#{DEFAULT_HOMEPAGE_URL}/api/v10/"
Class Method Summary collapse
-
.user_agent ⇒ Object
Discord requires User-Agent of the form “DiscordBot ($url, $versionNumber)”.
Instance Method Summary collapse
- #download(url) ⇒ Object
- #edit_message(channel_id, message_id, content) ⇒ Object
-
#initialize(bot_token:) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #me ⇒ Object
- #send_file(channel_id, path, name: nil) ⇒ Object
- #send_message(channel_id, content, reply_to: nil) ⇒ Object
Constructor Details
#initialize(bot_token:) ⇒ ApiClient
Returns a new instance of ApiClient.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/clacky/server/channel/adapters/discord/api_client.rb', line 16 def initialize(bot_token:) @bot_token = bot_token @conn = Faraday.new(url: BASE_URL) do |f| f.headers["Authorization"] = "Bot #{@bot_token}" f.headers["User-Agent"] = self.class.user_agent f.request :multipart f.response :raise_error f.adapter Faraday.default_adapter end end |
Class Method Details
.user_agent ⇒ Object
Discord requires User-Agent of the form “DiscordBot ($url, $versionNumber)”. Requests with an invalid UA may be blocked at Cloudflare.
62 63 64 65 |
# File 'lib/clacky/server/channel/adapters/discord/api_client.rb', line 62 def self.user_agent url = (Clacky::BrandConfig.load.homepage_url rescue nil) || DEFAULT_HOMEPAGE_URL "DiscordBot (#{url}, #{Clacky::VERSION})" end |
Instance Method Details
#download(url) ⇒ Object
55 56 57 58 |
# File 'lib/clacky/server/channel/adapters/discord/api_client.rb', line 55 def download(url) res = Faraday.get(url) { body: res.body, content_type: res.headers["content-type"] } end |
#edit_message(channel_id, message_id, content) ⇒ Object
37 38 39 |
# File 'lib/clacky/server/channel/adapters/discord/api_client.rb', line 37 def (channel_id, , content) request(:patch, "channels/#{channel_id}/messages/#{}", { content: content.to_s }) end |
#me ⇒ Object
27 28 29 |
# File 'lib/clacky/server/channel/adapters/discord/api_client.rb', line 27 def me request(:get, "users/@me") end |
#send_file(channel_id, path, name: nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/clacky/server/channel/adapters/discord/api_client.rb', line 41 def send_file(channel_id, path, name: nil) raise ArgumentError, "File not found: #{path}" unless File.exist?(path) filename = name || File.basename(path) payload = { attachments: [{ id: 0, filename: filename }] } io = Faraday::UploadIO.new(path, detect_mime(path), filename) res = @conn.post("channels/#{channel_id}/messages") do |req| req.body = { "payload_json" => JSON.generate(payload), "files[0]" => io } end parse_json(res.body) rescue Faraday::Error => e raise_api_error(e) end |
#send_message(channel_id, content, reply_to: nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/clacky/server/channel/adapters/discord/api_client.rb', line 31 def (channel_id, content, reply_to: nil) payload = { content: content.to_s } payload[:message_reference] = { message_id: reply_to.to_s } if reply_to request(:post, "channels/#{channel_id}/messages", payload) end |