Class: Rubord::REST
- Inherits:
-
Object
- Object
- Rubord::REST
- Defined in:
- lib/rubord/structs/rest.rb
Constant Summary collapse
- BASE_URL =
"https://discord.com/api/v10"- MAX_RETRIES =
Default retry configuration
3- INITIAL_RETRY_DELAY =
0.5
Instance Method Summary collapse
- #ban_guild_member(guild_id, user_id, delete_message_days: 0, reason: nil) ⇒ Object
- #build_message_body(content: nil, embeds: nil, components: nil, flags: nil) ⇒ Object
- #delete_message(channel_id, message_id) ⇒ Object
- #edit_message(channel_id, message_id, **opts) ⇒ Object
- #get_application ⇒ Object
- #get_application_emojis(application_id) ⇒ Object
- #get_channel(channel_id) ⇒ Object
- #get_guild(guild_id) ⇒ Object
- #get_guild_member(guild_id, user_id) ⇒ Object
- #get_message(channel_id, message_id) ⇒ Object
- #get_user(user_id) ⇒ Object
- #handle_response(res) ⇒ Object
-
#initialize(token) ⇒ REST
constructor
A new instance of REST.
- #interaction_edit(application_id, token, **opts) ⇒ Object
- #interaction_followup(application_id, token, **opts) ⇒ Object
- #interactions_response(interaction_id, interaction_token, type: nil, **opts) ⇒ Object
- #kick_guild_member(guild_id, user_id, reason: nil) ⇒ Object
- #reply_message(channel_id, message_id, **opts) ⇒ Object
- #request(method, path, body: nil, retries: MAX_RETRIES) ⇒ Object
-
#send_message(channel_id, **opts) ⇒ Object
MÉTODOS PÚBLICOS DA API ==========.
- #unban_guild_member(guild_id, user_id, reason: nil) ⇒ Object
Constructor Details
#initialize(token) ⇒ REST
Returns a new instance of REST.
15 16 17 18 19 |
# File 'lib/rubord/structs/rest.rb', line 15 def initialize(token) @token = token @rate_limiter = RateLimiter.new @http_pool = {} # Simple HTTP connection pool end |
Instance Method Details
#ban_guild_member(guild_id, user_id, delete_message_days: 0, reason: nil) ⇒ Object
72 73 74 75 76 |
# File 'lib/rubord/structs/rest.rb', line 72 def ban_guild_member(guild_id, user_id, delete_message_days: 0, reason: nil) path = "/guilds/#{guild_id}/bans/#{user_id}?delete_message_days=#{}" path += "&reason=#{URI.encode(reason)}" if reason request(:put, path) end |
#build_message_body(content: nil, embeds: nil, components: nil, flags: nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rubord/structs/rest.rb', line 115 def (content: nil, embeds: nil, components: nil, flags: nil) body = {} resolved_flags = case flags when Array Rubord::MessageFlags.combine(*flags) when Symbol Rubord::MessageFlags.combine(flags) when Integer flags else nil end is_components_v2 = resolved_flags && (resolved_flags & Rubord::MessageFlags::COMPONENTS_V2 != 0) if is_components_v2 if content || raise ArgumentError, "content/embeds cannot be used with Components V2. Use Rubord::Text or other components." end else body[:content] = content if content body[:embeds] = Array().map(&:to_h) if end body[:components] = Array(components).map(&:to_h) if components body[:flags] = resolved_flags if resolved_flags body end |
#delete_message(channel_id, message_id) ⇒ Object
42 43 44 |
# File 'lib/rubord/structs/rest.rb', line 42 def (channel_id, ) request(:delete, "/channels/#{channel_id}/messages/#{}") end |
#edit_message(channel_id, message_id, **opts) ⇒ Object
37 38 39 40 |
# File 'lib/rubord/structs/rest.rb', line 37 def (channel_id, , **opts) body = (**opts) request(:patch, "/channels/#{channel_id}/messages/#{}", body: body) end |
#get_application ⇒ Object
84 85 86 |
# File 'lib/rubord/structs/rest.rb', line 84 def get_application request(:get, "/oauth2/applications/@me") end |
#get_application_emojis(application_id) ⇒ Object
88 89 90 |
# File 'lib/rubord/structs/rest.rb', line 88 def get_application_emojis(application_id) request(:get, "/applications/#{application_id}/emojis") end |
#get_channel(channel_id) ⇒ Object
46 47 48 |
# File 'lib/rubord/structs/rest.rb', line 46 def get_channel(channel_id) request(:get, "/channels/#{channel_id}") end |
#get_guild(guild_id) ⇒ Object
58 59 60 |
# File 'lib/rubord/structs/rest.rb', line 58 def get_guild(guild_id) request(:get, "/guilds/#{guild_id}") end |
#get_guild_member(guild_id, user_id) ⇒ Object
62 63 64 |
# File 'lib/rubord/structs/rest.rb', line 62 def get_guild_member(guild_id, user_id) request(:get, "/guilds/#{guild_id}/members/#{user_id}") end |
#get_message(channel_id, message_id) ⇒ Object
50 51 52 |
# File 'lib/rubord/structs/rest.rb', line 50 def (channel_id, ) request(:get, "/channels/#{channel_id}/messages/#{}") end |
#get_user(user_id) ⇒ Object
54 55 56 |
# File 'lib/rubord/structs/rest.rb', line 54 def get_user(user_id) request(:get, "/users/#{user_id}") end |
#handle_response(res) ⇒ Object
149 150 151 |
# File 'lib/rubord/structs/rest.rb', line 149 def handle_response(res) parse_response(res) end |
#interaction_edit(application_id, token, **opts) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/rubord/structs/rest.rb', line 102 def interaction_edit(application_id, token, **opts) data = (**opts) body = { data: data } request(:patch, "/webhooks/#{application_id}/#{token}/messages/@original", body: body) end |
#interaction_followup(application_id, token, **opts) ⇒ Object
110 111 112 113 |
# File 'lib/rubord/structs/rest.rb', line 110 def interaction_followup(application_id, token, **opts) body = (**opts) request(:post, "/webhooks/#{application_id}/#{token}", body: body) end |
#interactions_response(interaction_id, interaction_token, type: nil, **opts) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/rubord/structs/rest.rb', line 92 def interactions_response(interaction_id, interaction_token, type: nil, **opts) data = (**opts) body = { type: type, data: data } request(:post, "/interactions/#{interaction_id}/#{interaction_token}/callback", body: body) end |
#kick_guild_member(guild_id, user_id, reason: nil) ⇒ Object
66 67 68 69 70 |
# File 'lib/rubord/structs/rest.rb', line 66 def kick_guild_member(guild_id, user_id, reason: nil) path = "/guilds/#{guild_id}/members/#{user_id}" path += "?reason=#{URI.encode(reason)}" if reason request(:delete, path) end |
#reply_message(channel_id, message_id, **opts) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/rubord/structs/rest.rb', line 28 def (channel_id, , **opts) body = (**opts) body[:message_reference] = { message_id: , channel_id: channel_id } request(:post, "/channels/#{channel_id}/messages", body: body) end |
#request(method, path, body: nil, retries: MAX_RETRIES) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/rubord/structs/rest.rb', line 153 def request(method, path, body: nil, retries: MAX_RETRIES) route = extract_route(method, path) uri = URI("#{BASE_URL}#{path}") # Wait for rate limits @rate_limiter.wait_global @rate_limiter.wait_for(route) req = build_request(method, uri) req.body = JSON.generate(body) if body begin res = send_http_request(uri, req) # Handle rate limit responses if res.code.to_i == 429 return handle_rate_limit_response(res, route, method, path, body, retries) end # Handle other errors unless res.is_a?(Net::HTTPSuccess) raise_api_error(res) end # Update rate limit info update_rate_limit_info(route, res) # Parse and return response return parse_response(res) rescue Net::OpenTimeout, Net::ReadTimeout => e return handle_timeout(e, method, path, body, retries) rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e return handle_connection_error(e, method, path, body, retries) rescue => e # Handle any other unexpected errors puts "[REST Error] Unexpected error: #{e.class}: #{e.}" raise e end end |
#send_message(channel_id, **opts) ⇒ Object
MÉTODOS PÚBLICOS DA API ==========
23 24 25 26 |
# File 'lib/rubord/structs/rest.rb', line 23 def (channel_id, **opts) body = (**opts) request(:post, "/channels/#{channel_id}/messages", body: body) end |
#unban_guild_member(guild_id, user_id, reason: nil) ⇒ Object
78 79 80 81 82 |
# File 'lib/rubord/structs/rest.rb', line 78 def unban_guild_member(guild_id, user_id, reason: nil) path = "/guilds/#{guild_id}/bans/#{user_id}" path += "?reason=#{URI.encode(reason)}" if reason request(:delete, path) end |