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/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
@@ -23,28 +34,57 @@ class DiscordApi
23
34
  url = "#{@base_url}/channels/#{channel_id}/messages#{query_string}"
24
35
  headers = { 'Authorization': @authorization_header }
25
36
  response = DiscordApi.get(url, headers)
26
- return response unless response.status != 200
37
+ return response if response.status == 200
27
38
 
28
39
  @logger.error("Failed to get messages from channel with ID #{channel_id}. Response: #{response.body}")
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 }
35
51
  response = DiscordApi.get(url, headers)
36
- return response unless response.status != 200
52
+ return response if response.status == 200
37
53
 
38
54
  @logger.error("Failed to get message with ID #{message_id} from channel with ID #{channel_id}. " \
39
55
  "Response: #{response.body}")
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?
@@ -65,98 +105,159 @@ class DiscordApi
65
105
  data = JSON.generate(output)
66
106
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
67
107
  response = DiscordApi.post(url, data, headers)
68
- return response unless response.status != 200
108
+ return response if response.status == 200
69
109
 
70
110
  @logger.error("Failed to create message in channel #{channel_id}. Response: #{response.body}")
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 }
77
122
  response = DiscordApi.post(url, nil, headers)
78
- return response unless response.status != 200
123
+ return response if response.status == 200
79
124
 
80
125
  @logger.error("Failed to crosspost message with ID #{message_id} in channel with ID #{channel_id}. " \
81
126
  "Response: #{response.body}")
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
- return response unless response.status != 204
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
- return response unless response.status != 204
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
- return response unless response.status != 204
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
- return response unless response.status != 200
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 }
137
215
  response = DiscordApi.delete(url, headers)
138
- return response unless response.status != 204
216
+ return response if response.status == 204
139
217
 
140
218
  @logger.error("Failed to delete all reactions in channel with ID #{channel_id} for message with ID #{message_id}" \
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
- return response unless response.status != 204
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,32 +265,43 @@ 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)
172
272
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
173
273
  response = DiscordApi.patch(url, data, headers)
174
- return response unless response.status != 200
274
+ return response if response.status == 200
175
275
 
176
276
  @logger.error("Failed to edit message with ID #{message_id} in channel with ID #{channel_id}. " \
177
277
  "Response: #{response.body}")
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 }
184
290
  headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
185
291
  response = DiscordApi.delete(url, headers)
186
- return response unless response.status != 204
292
+ return response if response.status == 204
187
293
 
188
294
  @logger.error("Failed to delete message with ID #{message_id} in channel with ID #{channel_id}. " \
189
295
  "Response: #{response.body}")
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"
@@ -197,12 +309,18 @@ class DiscordApi
197
309
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
198
310
  headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
199
311
  response = DiscordApi.post(url, data, headers)
200
- return response unless response.status != 204
312
+ return response if response.status == 204
201
313
 
202
314
  @logger.error("Failed to bulk delete messages in channel with ID #{channel_id}. Response: #{response.body}")
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?
@@ -211,30 +329,42 @@ class DiscordApi
211
329
  url = "#{@base_url}/channels/#{channel_id}/messages/pins#{query_string}"
212
330
  headers = { 'Authorization': @authorization_header }
213
331
  response = DiscordApi.get(url, headers)
214
- return response unless response.status != 200
332
+ return response if response.status == 200
215
333
 
216
334
  @logger.error("Failed to get pinned messages in channel with ID #{channel_id}. Response: #{response.body}")
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 }
223
347
  headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
224
348
  response = DiscordApi.put(url, nil, headers)
225
- return response unless response.status != 204
349
+ return response if response.status == 204
226
350
 
227
351
  @logger.error("Failed to pin message with ID #{message_id} in channel with ID #{channel_id}. " \
228
352
  "Response: #{response.body}")
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 }
235
365
  headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
236
366
  response = DiscordApi.delete(url, headers)
237
- return response unless response.status != 204
367
+ return response if response.status == 204
238
368
 
239
369
  @logger.error("Failed to unpin message with ID #{message_id} in channel with ID #{channel_id}. " \
240
370
  "Response: #{response.body}")