disrb 0.1.2.2 → 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/logger.rb CHANGED
@@ -1,79 +1,116 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Logger2 (class Logger already exists in Ruby)
4
- # Logging handler with some cool styling
3
+ # Logging handler with some cool styling (called Logger2 because Logger is a built-in Ruby class)
4
+ #
5
+ # When you create an instance of this class, you need to set the verbosity level, and when you run the instance methods,
6
+ # it will follow the set verbosity level. Example: you create a Logger2 instance with verbosity level 3 (warning),
7
+ # only the methods "fatal_error", "error" and "warn" will print to the console. The class methods (start with s_)
8
+ # will always print to the console.
5
9
  class Logger2
10
+ # Creates a new Logger2 instance.
11
+ #
12
+ # verbosity_level can be set to:
13
+ # - 0: No logging.
14
+ # - 1: Fatal errors only.
15
+ # - 2: Fatal errors and errors.
16
+ # - 3: All of the above and warnings.
17
+ # - 4: All of the above and information messages.
18
+ # - 5: All of the above and debug messages.
19
+ # @param verbosity_level [Integer] The verbosity level for the logger to follow.
20
+ # @return [Logger2] Logger2 instance.
6
21
  def initialize(verbosity_level)
7
22
  @verbosity_level = verbosity_level
8
23
  end
9
24
 
25
+ def self.base(acolor1, acolor2, acolor3, name, message)
26
+ caller = caller_locations[1]
27
+ file = caller.path
28
+ line = caller.lineno
29
+ location = "#{file}:#{line}"
30
+ name = name.ljust(14, ' ')
31
+ acolors = [acolor1, acolor2, acolor3].join(';')
32
+ "\033[1;38;2;255;255;255;48;2;#{acolors}m | #{name} \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
33
+ " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} at #{location} \033[0m\033[1;38;2;255;255;255;48;2;#{acolors}m" \
34
+ " \033[0m \e[38;2;#{acolors}m#{message}\e[0m"
35
+ end
36
+
37
+ # Logs a fatal error to the console if the verbosity level is set to 1 or higher.
38
+ # @param message [String] The message to log.
39
+ # @return [nil]
10
40
  def fatal_error(message)
11
41
  return unless @verbosity_level >= 1
12
42
 
13
- puts("\033[1;38;2;255;255;255;48;2;192;57;43m | FATAL ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
14
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;192;57;43m \033[0m " \
15
- "\e[38;2;192;57;43m#{message}\e[0m")
43
+ puts(Logger2.base(192, 57, 43, 'FATAL ERROR', message))
16
44
  end
17
45
 
46
+ # Logs an error to the console if the verbosity level is set to 2 or higher.
47
+ # @param message [String] The message to log.
48
+ # @return [nil]
18
49
  def error(message)
19
50
  return unless @verbosity_level >= 2
20
51
 
21
- puts("\033[1;38;2;255;255;255;48;2;243;156;18m | ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
22
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;243;156;18m \033[0m " \
23
- "\e[38;2;243;156;18m#{message}\e[0m")
52
+ puts(Logger2.base(243, 156, 18, 'ERROR', message))
24
53
  end
25
54
 
55
+ # Logs a debug message to the console if the verbosity level is set to 5.
56
+ # @param message [String] The message to log.
57
+ # @return [nil]
26
58
  def debug(message)
27
59
  return unless @verbosity_level == 5
28
60
 
29
- puts("\033[1;38;2;255;255;255;48;2;155;89;182m | DEBUG \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
30
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;155;89;182m \033[0m " \
31
- "\e[38;2;155;89;182m#{message}\e[0m")
61
+ puts(Logger2.base(155, 89, 182, 'DEBUG', message))
32
62
  end
33
63
 
64
+ # Logs a warning to the console if the verbosity level is set to 3 or higher.
65
+ # @param message [String] The message to log.
66
+ # @return [nil]
34
67
  def warn(message)
35
68
  return unless @verbosity_level >= 3
36
69
 
37
- puts("\033[1;38;2;255;255;255;48;2;241;196;15m | WARNING \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
38
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;241;196;15m \033[0m " \
39
- "\e[38;2;241;196;15m#{message}\e[0m")
70
+ puts(Logger2.base(241, 196, 15, 'WARNING', message))
40
71
  end
41
72
 
73
+ # Logs an info message to the console if the verbosity level is set to 4 or higher.
74
+ # @param message [String] The message to log.
75
+ # @return [nil]
42
76
  def info(message)
43
77
  return unless @verbosity_level >= 4
44
78
 
45
- puts("\033[1;38;2;255;255;255;48;2;76;175;80m | INFORMATION \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
46
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;76;175;80m \033[0m " \
47
- "\e[38;2;76;175;80m#{message}\e[0m")
79
+ puts(Logger2.base(76, 175, 80, 'INFORMATION', message))
48
80
  end
49
81
 
82
+ # Logs a fatal error to the console
83
+ # @param message [String] The message to log.
84
+ # @return [nil]
50
85
  def self.s_fatal_error(message)
51
- puts("\033[1;38;2;255;255;255;48;2;192;57;43m | FATAL ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
52
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;192;57;43m \033[0m " \
53
- "\e[38;2;192;57;43m#{message}\e[0m")
86
+ puts(base(192, 57, 43, 'FATAL ERROR', message))
54
87
  end
55
88
 
89
+ # Logs an error to the console
90
+ # @param message [String] The message to log.
91
+ # @return [nil]
56
92
  def self.s_error(message)
57
- puts("\033[1;38;2;255;255;255;48;2;243;156;18m | ERROR \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
58
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;243;156;18m \033[0m " \
59
- "\e[38;2;243;156;18m#{message}\e[0m")
93
+ puts(base(243, 156, 18, 'ERROR', message))
60
94
  end
61
95
 
96
+ # Logs a debug message to the console
97
+ # @param message [String] The message to log.
98
+ # @return [nil]
62
99
  def self.s_debug(message)
63
- puts("\033[1;38;2;255;255;255;48;2;155;89;182m | DEBUG \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
64
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;155;89;182m \033[0m " \
65
- "\e[38;2;155;89;182m#{message}\e[0m")
100
+ puts(base(155, 89, 182, 'DEBUG', message))
66
101
  end
67
102
 
103
+ # Logs a warning to the console
104
+ # @param message [String] The message to log.
105
+ # @return [nil]
68
106
  def self.s_warn(message)
69
- puts("\033[1;38;2;255;255;255;48;2;241;196;15m | WARNING \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
70
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;241;196;15m \033[0m " \
71
- "\e[38;2;241;196;15m#{message}\e[0m")
107
+ puts(base(241, 196, 15, 'WARNING', message))
72
108
  end
73
109
 
110
+ # Logs an info message to the console
111
+ # @param message [String] The message to log.
112
+ # @return [nil]
74
113
  def self.s_info(message)
75
- puts("\033[1;38;2;255;255;255;48;2;76;175;80m | INFORMATION \033[0m\033[38;2;255;255;255;48;2;44;62;80m" \
76
- " #{Time.now.strftime('%Y-%m-%d %H:%M:%S')} \033[0m\033[1;38;2;255;255;255;48;2;76;175;80m \033[0m " \
77
- "\e[38;2;76;175;80m#{message}\e[0m")
114
+ puts(base(76, 175, 80, 'INFORMATION', message))
78
115
  end
79
116
  end
data/lib/disrb/message.rb CHANGED
@@ -1,8 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # DiscordApi
4
- # The class that contains everything that interacts with the Discord API.
3
+ # Class that contains functions that allow interacting with the Discord API.
5
4
  class DiscordApi
5
+ # Gets the messages in a channel. Returns an array of message objects from newest to oldest on success.
6
+ # See https://discord.com/developers/docs/resources/message#get-channel-messages
7
+ #
8
+ # The before, after, and around parameters are mutually exclusive. Only one of them can be specified.
9
+ # If more than one of these are specified, all of these will be set to nil and an error will be logged
10
+ # (depends on the verbosity level set).
11
+ # @param channel_id [String] The ID of the channel to get messages from.
12
+ # @param around [String, nil] Gets messages around the specified message ID.
13
+ # @param before [String, nil] Gets messages before the specified message ID.
14
+ # @param after [String, nil] Gets messages after the specified message ID.
15
+ # @param limit [Integer, nil] The maximum number of messages to return. Default 50.
16
+ # @return [Faraday::Response] The response from the Discord API as a Farday::Response object.
6
17
  def get_channel_messages(channel_id, around: nil, before: nil, after: nil, limit: nil)
7
18
  options = { around: around, before: before, after: after }
8
19
  specified_keys = options.reject { |_k, v| v.nil? }.keys
@@ -29,6 +40,11 @@ class DiscordApi
29
40
  response
30
41
  end
31
42
 
43
+ # Gets a specific message from a channel. Returns a message object on success.
44
+ # See https://discord.com/developers/docs/resources/message#get-channel-message
45
+ # @param channel_id [String] The ID of the channel to get the message from.
46
+ # @param message_id [String] The ID of the message to get.
47
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
32
48
  def get_channel_message(channel_id, message_id)
33
49
  url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}"
34
50
  headers = { 'Authorization': @authorization_header }
@@ -40,11 +56,35 @@ class DiscordApi
40
56
  response
41
57
  end
42
58
 
59
+ # Creates a message in a channel. Returns the created message object on success.
60
+ # See https://discord.com/developers/docs/resources/message#create-message
61
+ # One of content, embeds, sticker_ids, components or poll must be provided. If none of these are provided,
62
+ # the function will log a warning (depends on the verbosity level set) and return nil
63
+ # @param channel_id [String] The ID of the channel to create the message in
64
+ # @param content [String, nil] Message contents (up to 2000 characters)
65
+ # @param nonce [String, Integer, nil] Can be used to verify if a message was sent (up to 25 characters). The value
66
+ # will appear in the message object,
67
+ # @param tts [TrueClass, FalseClass, nil] Whether the message is a TTS message
68
+ # @param embeds [Array, nil] Up to 10 rich embeds (up to 6000 characters)
69
+ # @param allowed_mentions [Hash, nil] Allowed mentions object
70
+ # @param message_reference [Hash, nil] Message reference object for replies/forwards
71
+ # @param components [Array, nil] An array of Components to include with the message
72
+ # @param sticker_ids [Array, nil] IDs of up to 3 stickers in the server to send in the message
73
+ # @param _files [nil] WORK IN PROGRESS
74
+ # @param attachments [Array, nil] Attachments objects with filename and description. Practically useless due to
75
+ # uploading files not being implemented yet.
76
+ # @param flags [Integer, nil] Message flags combined as a bitfield.
77
+ # @param enforce_nonce [TrueClass, FalseClass, nil] If true and a nonce is set, the nonce's uniqueness will be
78
+ # checked, if a message with the same nonce already exists from the same author, that message will be returned
79
+ # and no new message will be created.
80
+ # @param poll [Hash, nil] A poll object
81
+ # @return [Faraday::Response, nil] The response from the Discord API as a Faraday::Response object, or nil if none of
82
+ # content, embeds, sticker_ids, components or poll were provided.
43
83
  def create_message(channel_id, content: nil, nonce: nil, tts: nil, embeds: nil, allowed_mentions: nil,
44
- message_reference: nil, components: nil, sticker_ids: nil, files: nil, attachments: nil,
84
+ message_reference: nil, components: nil, sticker_ids: nil, _files: nil, attachments: nil,
45
85
  flags: nil, enforce_nonce: nil, poll: nil)
46
- if content.nil? && embeds.nil? && sticker_ids.nil? && components.nil? && files.nil? && poll.nil?
47
- @logger.warn('No content, embeds, sticker ids, components, files or poll provided. Skipping function.')
86
+ if content.nil? && embeds.nil? && sticker_ids.nil? && components.nil? && poll.nil?
87
+ @logger.warn('No content, embeds, sticker_ids, components or poll provided. Skipping function.')
48
88
  return
49
89
  end
50
90
  output = {}
@@ -56,7 +96,7 @@ class DiscordApi
56
96
  output[:message_reference] = message_reference unless message_reference.nil?
57
97
  output[:components] = components unless components.nil?
58
98
  output[:sticker_ids] = sticker_ids unless sticker_ids.nil?
59
- output[:files] = files unless files.nil?
99
+ # output[:files] = files unless files.nil?
60
100
  output[:attachments] = attachments unless attachments.nil?
61
101
  output[:flags] = flags unless flags.nil?
62
102
  output[:enforce_nonce] = enforce_nonce unless enforce_nonce.nil?
@@ -71,6 +111,11 @@ class DiscordApi
71
111
  response
72
112
  end
73
113
 
114
+ # Crossposts a message in an Announcement Channel to all following channels. Returns the crossposted message object on
115
+ # success. See https://discord.com/developers/docs/resources/message#crosspost-message
116
+ # @param channel_id [String] The ID of the channel the message to crosspost is located
117
+ # @param message_id [String] The ID of the message to crosspost
118
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
74
119
  def crosspost_message(channel_id, message_id)
75
120
  url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/crosspost"
76
121
  headers = { 'Authorization': @authorization_header }
@@ -82,55 +127,88 @@ class DiscordApi
82
127
  response
83
128
  end
84
129
 
85
- def create_reaction(channel_id, message_id, emoji_id)
86
- url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}/@me"
130
+ # Create a reaction for the specified message. Returns no content on success.
131
+ # See https://discord.com/developers/docs/resources/message#create-reaction
132
+ # @param channel_id [String] The ID of the channel the message is located in
133
+ # @param message_id [String] The ID of the message to create the reaction for
134
+ # @param emoji [String] URL encoded emoji to react with, or name:id format for custom emojis
135
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
136
+ def create_reaction(channel_id, message_id, emoji)
137
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/@me"
87
138
  headers = { 'Authorization': @authorization_header }
88
139
  response = DiscordApi.put(url, nil, headers)
89
140
  return response if response.status == 204
90
141
 
91
- @logger.error("Failed to create reaction with emoji ID #{emoji_id} in channel with ID #{channel_id} " \
142
+ @logger.error("Failed to create reaction with emoji ID #{emoji} in channel with ID #{channel_id} " \
92
143
  "for message with ID #{message_id}. Response: #{response.body}")
93
144
  response
94
145
  end
95
146
 
96
- def delete_own_reaction(channel_id, message_id, emoji_id)
97
- url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}/@me"
147
+ # Deletes a reaction with the specified emoji for the current user in the specified message. Returns no content on
148
+ # success. See https://discord.com/developers/docs/resources/message#delete-own-reaction
149
+ # @param channel_id [String] The ID of the channel the message is located in
150
+ # @param message_id [String] The ID of the message to delete the reaction for
151
+ # @param emoji [String] URL encoded emoji to delete, or name:id format for custom emojis
152
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
153
+ def delete_own_reaction(channel_id, message_id, emoji)
154
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/@me"
98
155
  headers = { 'Authorization': @authorization_header }
99
156
  response = DiscordApi.delete(url, headers)
100
157
  return response if response.status == 204
101
158
 
102
- @logger.error("Failed to delete own reaction with emoji ID #{emoji_id} in channel with ID #{channel_id} " \
159
+ @logger.error("Failed to delete own reaction with emoji ID #{emoji} in channel with ID #{channel_id} " \
103
160
  "for message with ID #{message_id}. Response: #{response.body}")
104
161
  response
105
162
  end
106
163
 
107
- def delete_user_reaction(channel_id, message_id, emoji_id, user_id)
108
- url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}/#{user_id}"
164
+ # Deletes a reaction with the specified emoji for a user in the specified message. Returns no content on success.
165
+ # See https://discord.com/developers/docs/resources/message#delete-user-reaction
166
+ # @param channel_id [String] The ID of the channel the message is located in
167
+ # @param message_id [String] The ID of the message to delete the reaction for
168
+ # @param emoji [String] URL encoded emoji to delete, or name:id format for custom emojis
169
+ # @param user_id [String] The ID of the user to delete the reaction for
170
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
171
+ def delete_user_reaction(channel_id, message_id, emoji, user_id)
172
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/#{user_id}"
109
173
  headers = { 'Authorization': @authorization_header }
110
174
  response = DiscordApi.delete(url, headers)
111
175
  return response if response.status == 204
112
176
 
113
- @logger.error("Failed to delete user reaction with emoji ID #{emoji_id} in channel with ID #{channel_id} " \
177
+ @logger.error("Failed to delete user reaction with emoji ID #{emoji} in channel with ID #{channel_id} " \
114
178
  "for message with ID #{message_id} by user with ID #{user_id}. Response: #{response.body}")
115
179
  response
116
180
  end
117
181
 
118
- def get_reactions(channel_id, message_id, emoji_id, type: nil, after: nil, limit: nil)
182
+ # Gets a list of users that reacted with the specified emoji to the specified message. Returns an array of user
183
+ # objects on success. See https://discord.com/developers/docs/resources/message#get-reactions
184
+ # @param channel_id [String] The ID of the channel the message is located in
185
+ # @param message_id [String] The ID of the message to get reactions for
186
+ # @param emoji [String] URL encoded emoji to get reactions for, or name:id format for custom emojis
187
+ # @param type [Integer, nil] Type of reaction to return.
188
+ # @param after [String, nil] Get users after this user ID
189
+ # @param limit [Integer, nil] Maximum number of users to return (1-100).
190
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
191
+ def get_reactions(channel_id, message_id, emoji, type: nil, after: nil, limit: nil)
119
192
  query_string_hash = {}
120
193
  query_string_hash[:type] = type unless type.nil?
121
194
  query_string_hash[:after] = after unless after.nil?
122
195
  query_string_hash[:limit] = limit unless limit.nil?
123
196
  query_string = DiscordApi.handle_query_strings(query_string_hash)
124
- url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}#{query_string}"
197
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}#{query_string}"
125
198
  headers = { 'Authorization': @authorization_header }
126
199
  response = DiscordApi.get(url, headers)
127
200
  return response if response.status == 200
128
201
 
129
- @logger.error("Failed to get reactions for emoji with ID #{emoji_id} in channel with ID #{channel_id} " \
202
+ @logger.error("Failed to get reactions for emoji with ID #{emoji} in channel with ID #{channel_id} " \
130
203
  "for message with ID #{message_id}. Response: #{response.body}")
131
204
  response
132
205
  end
133
206
 
207
+ # Deletes all reactions on a message. Returns no content on success.
208
+ # See https://discord.com/developers/docs/resources/message#delete-all-reactions
209
+ # @param channel_id [String] The ID of the channel the message is located in
210
+ # @param message_id [String] The ID of the message to delete reactions for
211
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
134
212
  def delete_all_reactions(channel_id, message_id)
135
213
  url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions"
136
214
  headers = { 'Authorization': @authorization_header }
@@ -141,22 +219,45 @@ class DiscordApi
141
219
  ". Response: #{response.body}")
142
220
  end
143
221
 
144
- def delete_all_reactions_for_emoji(channel_id, message_id, emoji_id)
145
- url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}"
222
+ # Deletes all reactions with the specified emoji on a message. Returns no content on success.
223
+ # See https://discord.com/developers/docs/resources/message#delete-all-reactions-for-emoji
224
+ # @param channel_id [String] The ID of the channel the message is located in
225
+ # @param message_id [String] The ID of the message to delete reactions for
226
+ # @param emoji [String] URL encoded emoji to delete, or name:id format for custom emojis
227
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
228
+ def delete_all_reactions_for_emoji(channel_id, message_id, emoji)
229
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}"
146
230
  headers = { 'Authorization': @authorization_header }
147
231
  response = DiscordApi.delete(url, headers)
148
232
  return response if response.status == 204
149
233
 
150
- @logger.error("Failed to delete all reactions for emoji with ID #{emoji_id} in channel with ID #{channel_id} for " \
234
+ @logger.error("Failed to delete all reactions for emoji with ID #{emoji} in channel with ID #{channel_id} for " \
151
235
  "message with ID #{message_id}. Response: #{response.body}")
152
236
  end
153
237
 
238
+ # Edits a message. Returns the edited message object on success.
239
+ # See https://discord.com/developers/docs/resources/message#edit-message
240
+ #
241
+ # If none of the optional parameters are provided (modifications), the function will not proceed and return nil.
242
+ # Since the files parameter is WIP, providing only files will also cause the function to not proceed.
243
+ # @param channel_id [String] The ID of the channel the message is located in
244
+ # @param message_id [String] The ID of the message to edit
245
+ # @param content [String, nil] Message contents (up to 2000 characters)
246
+ # @param embeds [Array, nil] Up to 10 rich embeds (up to 6000 characters)
247
+ # @param flags [Integer, nil] Message flags combined as an integer.
248
+ # @param allowed_mentions [Hash, nil] Allowed mentions object
249
+ # @param components [Array, nil] An array of Components to include with the message
250
+ # @param files [nil] WORK IN PROGRESS
251
+ # @param attachments [Array, nil] Attachments objects with filename and description. Practically useless due to
252
+ # uploading files not being implemented yet.
253
+ # @return [Faraday::Response, nil] The response from the Discord API as a Faraday::Response object, or nil if no
254
+ # modifications were provided.
154
255
  def edit_message(channel_id, message_id, content: nil, embeds: nil, flags: nil, allowed_mentions: nil,
155
- components: nil, files: nil, payload_json: nil, attachments: nil)
156
- if args[2..].all?(&:nil?)
256
+ components: nil, files: nil, attachments: nil)
257
+ if args[2..].all?(&:nil?) || (args[2..].delete(:files).all?(&:nil?) && !files.nil?)
157
258
  @logger.warn("No modifications provided for message with ID #{message_id} in channel with ID #{channel_id}. " \
158
- 'Skipping function.')
159
- return nil
259
+ 'The files parameter is WIP. Skipping function.')
260
+ return
160
261
  end
161
262
  output = {}
162
263
  output[:content] = content unless content.nil?
@@ -164,8 +265,7 @@ class DiscordApi
164
265
  output[:flags] = flags unless flags.nil?
165
266
  output[:allowed_mentions] = allowed_mentions unless allowed_mentions.nil?
166
267
  output[:components] = components unless components.nil?
167
- output[:files] = files unless files.nil?
168
- output[:payload_json] = payload_json unless payload_json.nil?
268
+ # output[:files] = files unless files.nil?
169
269
  output[:attachments] = attachments unless attachments.nil?
170
270
  url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}"
171
271
  data = JSON.generate(output)
@@ -178,6 +278,12 @@ class DiscordApi
178
278
  response
179
279
  end
180
280
 
281
+ # Deletes a message. Returns no content on success.
282
+ # See https://discord.com/developers/docs/resources/message#delete-message
283
+ # @param channel_id [String] The ID of the channel the message is located in
284
+ # @param message_id [String] The ID of the message to delete
285
+ # @param audit_reason [String, nil] The reason for deleting the message. Shows up on the audit log.
286
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
181
287
  def delete_message(channel_id, message_id, audit_reason = nil)
182
288
  url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}"
183
289
  headers = { 'Authorization': @authorization_header }
@@ -190,6 +296,12 @@ class DiscordApi
190
296
  response
191
297
  end
192
298
 
299
+ # Bulk deletes messages. Returns no content on success.
300
+ # See https://discord.com/developers/docs/resources/message#bulk-delete-messages
301
+ # @param channel_id [String] The ID of the channel the messages are located in
302
+ # @param messages [Array] An array of message IDs (as strings) to delete. (2-100 IDs)
303
+ # @param audit_reason [String, nil] The reason for deleting the messages. Shows up on the audit log.
304
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
193
305
  def bulk_delete_messages(channel_id, messages, audit_reason = nil)
194
306
  output = { messages: messages }
195
307
  url = "#{@base_url}/channels/#{channel_id}/messages/bulk-delete"
@@ -203,6 +315,12 @@ class DiscordApi
203
315
  response
204
316
  end
205
317
 
318
+ # Gets pinned messages in a channel. See https://discord.com/developers/docs/resources/message#get-channel-pins for
319
+ # more info and response structure.
320
+ # @param channel_id [String] The ID of the channel to get pinned messages from
321
+ # @param before [String, nil] Get messages pinned before this ISO8601 timestamp
322
+ # @param limit [Integer, nil] The maximum number of messages to return. (1-50, default 50)
323
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
206
324
  def get_channel_pins(channel_id, before: nil, limit: nil)
207
325
  query_string_hash = {}
208
326
  query_string_hash[:before] = before unless before.nil?
@@ -217,6 +335,12 @@ class DiscordApi
217
335
  response
218
336
  end
219
337
 
338
+ # Pins a message in a channel. Returns no content on success.
339
+ # See https://discord.com/developers/docs/resources/message#pin-message
340
+ # @param channel_id [String] The ID of the channel the message is located in
341
+ # @param message_id [String] The ID of the message to pin
342
+ # @param audit_reason [String, nil] The reason for pinning the message. Shows up in the audit log.
343
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
220
344
  def pin_message(channel_id, message_id, audit_reason = nil)
221
345
  url = "#{@base_url}/channels/#{channel_id}/messages/pins/#{message_id}"
222
346
  headers = { 'Authorization': @authorization_header }
@@ -229,6 +353,12 @@ class DiscordApi
229
353
  response
230
354
  end
231
355
 
356
+ # Unpins a message in a channel. Returns no content on success.
357
+ # See https://discord.com/developers/docs/resources/message#unpin-message
358
+ # @param channel_id [String] The ID of the channel the message is located in
359
+ # @param message_id [String] The ID of the message to unpin
360
+ # @param audit_reason [String, nil] The reason for unpinning the message. Shows up in the audit log.
361
+ # @return [Faraday::Response] The response from the Discord API as a Faraday::Response object.
232
362
  def unpin_message(channel_id, message_id, audit_reason = nil)
233
363
  url = "#{@base_url}/channels/#{channel_id}/messages/pins/#{message_id}"
234
364
  headers = { 'Authorization': @authorization_header }
data/lib/disrb/user.rb CHANGED
@@ -2,9 +2,10 @@
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 }
@@ -15,6 +16,9 @@ class DiscordApi
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 }
@@ -25,11 +29,15 @@ class DiscordApi
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?
@@ -48,6 +56,14 @@ class DiscordApi
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,7 +74,7 @@ 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
@@ -69,6 +85,26 @@ class DiscordApi
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 }
@@ -79,6 +115,11 @@ class DiscordApi
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 })
@@ -90,6 +131,12 @@ class DiscordApi
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
@@ -104,6 +151,9 @@ class DiscordApi
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 }
@@ -114,6 +164,11 @@ class DiscordApi
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 }
@@ -125,6 +180,18 @@ class DiscordApi
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 = {}