disrb 0.1.2.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/disrb/user.rb CHANGED
@@ -2,34 +2,42 @@
2
2
 
3
3
  # rubocop:disable Naming/AccessorMethodName
4
4
 
5
- # DiscordApi
6
- # The class that contains everything that interacts with the Discord API.
5
+ # Class that contains functions that allow interacting with the Discord API.
7
6
  class DiscordApi
7
+ # Returns the user object of the current user. See https://discord.com/developers/docs/resources/user#get-current-user
8
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
8
9
  def get_current_user
9
10
  url = "#{@base_url}/users/@me"
10
11
  headers = { 'Authorization': @authorization_header }
11
12
  response = DiscordApi.get(url, headers)
12
- return response unless response.status != 200
13
+ return response if response.status == 200
13
14
 
14
15
  @logger.error("Failed to get current user. Response: #{response.body}")
15
16
  response
16
17
  end
17
18
 
19
+ # Returns the user object of the specified user. See https://discord.com/developers/docs/resources/user#get-user
20
+ # @param user_id [String] The ID of the user.
21
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
18
22
  def get_user(user_id)
19
23
  url = "#{@base_url}/users/#{user_id}"
20
24
  headers = { 'Authorization': @authorization_header }
21
25
  response = DiscordApi.get(url, headers)
22
- return response unless response.status != 200
26
+ return response if response.status == 200
23
27
 
24
28
  @logger.error("Failed to get user with ID #{user_id}. Response: #{response.body}")
25
29
  response
26
30
  end
27
31
 
32
+ # Modifies the current user. See https://discord.com/developers/docs/resources/user#modify-current-user
33
+ #
34
+ # If none of the parameters are provided, the function will not proceed and return nil.
35
+ # @param username [String, nil] The new username for the current user. May cause discriminator to be randomized.
36
+ # @param avatar [String, nil] The new avatar for the current user. See https://discord.com/developers/docs/reference#image-data
37
+ # @param banner [String, nil] The new banner for the current user. See https://discord.com/developers/docs/reference#image-data
38
+ # @return [Faraday::Response, nil] The response from the Discord API as a Faraday::Response object or nil if no
39
+ # modifications were provided.
28
40
  def modify_current_user(username: nil, avatar: nil, banner: nil)
29
- if args.all?(&:nil?)
30
- @logger.warn('No modifications provided for current user. Skipping function.')
31
- return nil
32
- end
33
41
  output = {}
34
42
  output[:username] = username unless username.nil?
35
43
  output[:avatar] = avatar unless avatar.nil?
@@ -42,12 +50,20 @@ class DiscordApi
42
50
  data = JSON.generate(output)
43
51
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
44
52
  response = DiscordApi.patch(url, data, headers)
45
- return response unless response.status != 200
53
+ return response if response.status == 200
46
54
 
47
55
  @logger.error("Failed to modify current user. Response: #{response.body}")
48
56
  response
49
57
  end
50
58
 
59
+ # Returns an array of (partial) guild objects that the current user is a member of.
60
+ # See https://discord.com/developers/docs/resources/user#get-current-user-guilds
61
+ # @param before [String, nil] Get guilds before this guild ID.
62
+ # @param after [String, nil] Get guilds after this guild ID.
63
+ # @param limit [Integer, nil] Maximum number of guilds to return. 1-200 allowed, 200 default.
64
+ # @param with_counts [TrueClass, FalseClass, nil] Whether to include approximate member and presence counts in
65
+ # response.
66
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
51
67
  def get_current_user_guilds(before: nil, after: nil, limit: nil, with_counts: nil)
52
68
  query_string_hash = {}
53
69
  query_string_hash[:before] = before unless before.nil?
@@ -58,38 +74,69 @@ class DiscordApi
58
74
  url = "#{@base_url}/users/@me/guilds#{query_string}"
59
75
  headers = { 'Authorization': @authorization_header }
60
76
  response = DiscordApi.get(url, headers)
61
- if @authorization_token_type == 'Bot' && response.body.count == 200
77
+ if response.status == 200 && @authorization_token_type == 'Bot' && JSON.parse(response.body).count == 200
62
78
  @logger.warn('A bot can be in more than 200 guilds, however 200 guilds were returned.' \
63
79
  'Discord API doesn\'t allow you to fetch more than 200 guilds. Some guilds might not be listed.')
64
80
  return response
65
81
  end
66
- return response unless response.status != 200
82
+ return response if response.status == 200
67
83
 
68
84
  @logger.error("Failed to get current user's guilds. Response: #{response.body}")
69
85
  response
70
86
  end
71
87
 
88
+ # Returns a guild member object for the current user in the specified guild.
89
+ # See https://discord.com/developers/docs/resources/user#get-current-user-guild-member
90
+ # @param guild_id [String] The ID of the guild to get the current user's guild member for.
91
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
92
+ def get_current_user_guild_member(guild_id)
93
+ url = "#{@base_url}/users/@me/guilds/#{guild_id}/member"
94
+ headers = { 'Authorization': @authorization_header }
95
+ response = DiscordApi.get(url, headers)
96
+ return response unless response.status != 200
97
+
98
+ @logger.error("Failed to get current user's guild member for guild ID with ID #{guild_id}. Response: " \
99
+ "#{response.body}")
100
+ response
101
+ end
102
+
103
+ # Leaves a guild for the current user. If it succeeds, the response will have a status code of 204 (Empty Response),
104
+ # and thus the response body will be empty.
105
+ # See https://discord.com/developers/docs/resources/user#leave-guild
106
+ # @param guild_id [String] The ID of the guild to leave.
107
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
72
108
  def leave_guild(guild_id)
73
109
  url = "#{@base_url}/users/@me/guilds/#{guild_id}"
74
110
  headers = { 'Authorization': @authorization_header }
75
111
  response = DiscordApi.delete(url, headers)
76
- return response unless response.status != 204
112
+ return response if response.status == 204
77
113
 
78
114
  @logger.error("Failed to leave guild with ID #{guild_id}. Response: #{response.body}")
79
115
  response
80
116
  end
81
117
 
118
+ # Creates a DM channel with the specified user. Returns a DM channel object
119
+ # (if one already exists, it will return that channel).
120
+ # See https://discord.com/developers/docs/resources/user#create-dm
121
+ # @param recipient_id [String] The ID of the user to create a DM channel with.
122
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
82
123
  def create_dm(recipient_id)
83
124
  url = "#{@base_url}/users/@me/channels"
84
125
  data = JSON.generate({ recipient_id: recipient_id })
85
126
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
86
127
  response = DiscordApi.post(url, data, headers)
87
- return response unless response.status != 200
128
+ return response if response.status == 200
88
129
 
89
130
  @logger.error("Failed to create DM with recipient ID #{recipient_id}. Response: #{response.body}")
90
131
  response
91
132
  end
92
133
 
134
+ # Creates a group DM channel with the specified users. Returns a group DM channel object.
135
+ # See https://discord.com/developers/docs/resources/user#create-group-dm
136
+ # @param access_tokens [Array] An array of access tokens (as strings) of users that have granted your app the gdm.join
137
+ # OAuth2 scope
138
+ # @param nicks [Hash] "a dictionary of user ids to their respective nicknames" (whatever that means)
139
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
93
140
  def create_group_dm(access_tokens, nicks)
94
141
  output = {}
95
142
  output[:access_tokens] = access_tokens
@@ -98,33 +145,53 @@ class DiscordApi
98
145
  data = JSON.generate(output)
99
146
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
100
147
  response = DiscordApi.post(url, data, headers)
101
- return response unless response.status != 200
148
+ return response if response.status == 200
102
149
 
103
150
  @logger.error("Failed to create group DM. Response: #{response.body}")
104
151
  response
105
152
  end
106
153
 
154
+ # Returns an array of connection objects for the current user. Requires the connections OAuth2 scope.
155
+ # See https://discord.com/developers/docs/resources/user#get-current-user-connections
156
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
107
157
  def get_current_user_connections
108
158
  url = "#{@base_url}/users/@me/connections"
109
159
  headers = { 'Authorization': @authorization_header }
110
160
  response = DiscordApi.get(url, headers)
111
- return response unless response.status != 200
161
+ return response if response.status == 200
112
162
 
113
163
  @logger.error("Failed to get current user's connections. Response: #{response.body}")
114
164
  response
115
165
  end
116
166
 
167
+ # Returns the application role connection object for the user. Requires the role_connections.write OAuth2 scope for
168
+ # the application specified.
169
+ # See https://discord.com/developers/docs/resources/user#get-current-user-application-role-connection
170
+ # @param application_id [String] The ID of the application to get the role connection for.
171
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
117
172
  def get_current_user_application_role_connection(application_id)
118
173
  url = "#{@base_url}/users/@me/applications/#{application_id}/role-connection"
119
174
  headers = { 'Authorization': @authorization_header }
120
175
  response = DiscordApi.get(url, headers)
121
- return response unless response.status != 200
176
+ return response if response.status == 200
122
177
 
123
178
  @logger.error("Failed to get current user's application role connection for application ID #{application_id}. " \
124
179
  "Response: #{response.body}")
125
180
  response
126
181
  end
127
182
 
183
+ # Updates and returns the application role connection object for the user. Requires the role_connections.write OAuth2
184
+ # scope for the application specified.
185
+ # See https://discord.com/developers/docs/resources/user#update-current-user-application-role-connection
186
+ #
187
+ # If none of the optional parameters are provided (modifications), the function will not proceed and return nil.
188
+ # @param application_id [String] The ID of the application to update the role connection for.
189
+ # @param platform_name [String, nil] The vanity name of the platform a bot has connected (max 50 chars)
190
+ # @param platform_username [String, nil] The username on the platform a bot has connected (max 100 chars)
191
+ # @param metadata [Hash, nil] Hash mapping application role connection metadata keys to their string-ified values
192
+ # (max 100 chars) for the user on the platform a bot has connected.
193
+ # @return [Faraday::Response, nil] The response from the Discord API as a Faraday::Response object or nil if no
194
+ # modifications were provided.
128
195
  def update_current_user_application_role_connection(application_id, platform_name: nil, platform_username: nil,
129
196
  metadata: nil)
130
197
  output = {}
@@ -139,7 +206,7 @@ class DiscordApi
139
206
  data = JSON.generate(output)
140
207
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
141
208
  response = DiscordApi.put(url, data, headers)
142
- return response unless response.status != 200
209
+ return response if response.status == 200
143
210
 
144
211
  @logger.error("Failed to update current user's application role connection for application ID #{application_id}. " \
145
212
  "Response: #{response.body}")