Class: ChatSDK::Discord::ApiClient
- Inherits:
-
Object
- Object
- ChatSDK::Discord::ApiClient
- Defined in:
- lib/chat_sdk/discord/api_client.rb
Constant Summary collapse
- BASE_URL =
"https://discord.com"- API_PREFIX =
"/api/v10"
Instance Method Summary collapse
-
#add_reaction(channel_id, message_id, emoji) ⇒ Object
Reactions.
-
#create_dm(user_id) ⇒ Object
DMs.
-
#create_message(channel_id, content: nil, embeds: nil, components: nil) ⇒ Object
Messages.
- #delete_message(channel_id, message_id) ⇒ Object
- #edit_message(channel_id, message_id, content: nil, embeds: nil, components: nil) ⇒ Object
-
#get_messages(channel_id, limit: 50, before: nil) ⇒ Object
Messages history.
-
#initialize(bot_token:) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #remove_reaction(channel_id, message_id, emoji) ⇒ Object
-
#trigger_typing(channel_id) ⇒ Object
Typing.
-
#upload_file(channel_id, io, filename) ⇒ Object
File upload.
Constructor Details
#initialize(bot_token:) ⇒ ApiClient
Returns a new instance of ApiClient.
9 10 11 |
# File 'lib/chat_sdk/discord/api_client.rb', line 9 def initialize(bot_token:) @bot_token = bot_token end |
Instance Method Details
#add_reaction(channel_id, message_id, emoji) ⇒ Object
Reactions
35 36 37 38 |
# File 'lib/chat_sdk/discord/api_client.rb', line 35 def add_reaction(channel_id, , emoji) encoded = ERB::Util.url_encode(emoji) request(:put, "#{API_PREFIX}/channels/#{channel_id}/messages/#{}/reactions/#{encoded}/@me") end |
#create_dm(user_id) ⇒ Object
DMs
46 47 48 |
# File 'lib/chat_sdk/discord/api_client.rb', line 46 def create_dm(user_id) request(:post, "#{API_PREFIX}/users/@me/channels", {"recipient_id" => user_id}) end |
#create_message(channel_id, content: nil, embeds: nil, components: nil) ⇒ Object
Messages
14 15 16 17 18 19 20 |
# File 'lib/chat_sdk/discord/api_client.rb', line 14 def (channel_id, content: nil, embeds: nil, components: nil) body = {} body["content"] = content if content body["embeds"] = if body["components"] = components if components request(:post, "#{API_PREFIX}/channels/#{channel_id}/messages", body) end |
#delete_message(channel_id, message_id) ⇒ Object
30 31 32 |
# File 'lib/chat_sdk/discord/api_client.rb', line 30 def (channel_id, ) request(:delete, "#{API_PREFIX}/channels/#{channel_id}/messages/#{}") end |
#edit_message(channel_id, message_id, content: nil, embeds: nil, components: nil) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/chat_sdk/discord/api_client.rb', line 22 def (channel_id, , content: nil, embeds: nil, components: nil) body = {} body["content"] = content if content body["embeds"] = if body["components"] = components if components request(:patch, "#{API_PREFIX}/channels/#{channel_id}/messages/#{}", body) end |
#get_messages(channel_id, limit: 50, before: nil) ⇒ Object
Messages history
51 52 53 54 55 |
# File 'lib/chat_sdk/discord/api_client.rb', line 51 def (channel_id, limit: 50, before: nil) path = "#{API_PREFIX}/channels/#{channel_id}/messages?limit=#{limit}" path += "&before=#{before}" if before request(:get, path) end |
#remove_reaction(channel_id, message_id, emoji) ⇒ Object
40 41 42 43 |
# File 'lib/chat_sdk/discord/api_client.rb', line 40 def remove_reaction(channel_id, , emoji) encoded = ERB::Util.url_encode(emoji) request(:delete, "#{API_PREFIX}/channels/#{channel_id}/messages/#{}/reactions/#{encoded}/@me") end |
#trigger_typing(channel_id) ⇒ Object
Typing
58 59 60 |
# File 'lib/chat_sdk/discord/api_client.rb', line 58 def trigger_typing(channel_id) request(:post, "#{API_PREFIX}/channels/#{channel_id}/typing") end |
#upload_file(channel_id, io, filename) ⇒ Object
File upload
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/chat_sdk/discord/api_client.rb', line 63 def upload_file(channel_id, io, filename) conn = Faraday.new(url: BASE_URL) do |f| f.request :multipart f.response :json f.adapter :net_http end payload = { "file[0]" => Faraday::Multipart::FilePart.new(io, "application/octet-stream", filename) } response = conn.post("#{API_PREFIX}/channels/#{channel_id}/messages", payload) do |req| req.headers["Authorization"] = "Bot #{@bot_token}" end handle_response(response) end |