disrb 0.1.1.3 → 0.1.2.1

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -14
  3. data/lib/disrb/message.rb +207 -0
  4. data/lib/disrb.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1609e867d37e419357cfa31391bfc5a4cf16725766112927318a55463f5207e0
4
- data.tar.gz: 934eaa5331dd52ebed1d93f901fa85beb9449331778f90972567165f76e9c4f4
3
+ metadata.gz: 281bc6ea24c6df412e563a2b8961264240e058cbf895433c216964957beff5ae
4
+ data.tar.gz: 14d5149c99039845ee85e75c57f65cd9acc31242ed5c67600eb799b36e99c1cb
5
5
  SHA512:
6
- metadata.gz: 486aaa625c9030df5a94ed8259934928fbac728545d0274181036cbfde58d53ad86e2dc7453e983eebae10ed1476539534f239581b27c7edeaa5e7849aae80fa
7
- data.tar.gz: 7cffa404e815ff23b8e3e8a4be1cb926215553564f7a344e2497e0b0a23992800612c8c510513ca5e6112a548cce0ede5adcdf8a508539e7eab692009c55be8d
6
+ metadata.gz: d2ffcb0bd4ec5ae0be535b21f005587427106a8840d07cbbb518b1047919ccb5149d74d0b464bfb878d44bba1e648920ef00ca3018bcdf7be4206f68c2160322
7
+ data.tar.gz: 798506bfcd44025ee9be06976d6b3b4fad6d932207a4c0dc1e98c0ee80cb3e4f7fa8a4760170c1c6438cdb8825b87d11085c9985d1b3a70dca99d4b8b72e6a14
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # discord.rb indev
2
2
 
3
- [Changelog](CHANGELOG.md) | [Licensed under the MIT License](LICENSE)
3
+ [![Licensed under the MIT license](https://img.shields.io/github/license/hoovad/discord.rb)](LICENSE)
4
+ [![Gem Version](https://img.shields.io/gem/v/disrb?logo=ruby&color=green)](https://rubygems.org/gems/disrb)
5
+ ![project status: active](https://img.shields.io/badge/project_status-active-active)
6
+ [![main](https://github.com/hoovad/discord.rb/actions/workflows/main.yml/badge.svg)](https://github.com/hoovad/discord.rb/actions/workflows/main.yml)
4
7
 
5
- ![CI Pipeline](https://ci.codeberg.org/api/badges/14824/status.svg) [![Gem Version](https://badge.fury.io/rb/disrb.svg?icon=si%3Arubygems)](https://badge.fury.io/rb/disrb)
8
+ [Changelog](CHANGELOG.md) | [Documentation](https://www.rubydoc.info/gems/disrb/) (currently in development)
6
9
 
7
10
  W.I.P. Discord API wrapper written in Ruby for fun.
8
11
 
@@ -20,17 +23,17 @@ The test.rb file creates two commands "test" and "test2", that return "Hi" and "
20
23
  - [x] Basic gateway support
21
24
  - [x] RubyGem building and publishing
22
25
  - [ ] Alpha release (v0.2.0)
23
- - [ ] Add support for all Discord API endpoints
24
- - [ ] Add support for all Discord Gateway events and properly handle the connection
25
- - [ ] Documentation (v0.1.2)
26
- - [x] Transition to Faraday for HTTP requests (v0.1.1)
27
- - [x] Functions where all options are optional, check if atleast one is provided (v0.1.1.1)
28
- - [x] Prefer to use keyword arguments over positional arguments if there are more than 1 optional arguments (v0.1.1.1)
26
+ - [ ] Add support for all Discord API endpoints
27
+ - [ ] Add support for all Discord Gateway events and properly handle the connection
28
+ - [ ] Documentation (v0.1.3)
29
+ - [x] Transition to Faraday for HTTP requests (v0.1.1)
30
+ - [x] Functions where all options are optional, check if atleast one is provided (v0.1.1.1)
31
+ - [x] Prefer to use keyword arguments over positional arguments if there are more than 1 optional arguments (v0.1.1.1)
29
32
  - [ ] Beta release (v0.3.0)
30
- - [ ] Component support and builder
31
- - [ ] Sharding support
32
- - [ ] Rate limit handling
33
- - [ ] Voice support
34
- - [ ] Add parameter validation
33
+ - [ ] Component support and builder
34
+ - [ ] Sharding support
35
+ - [ ] Rate limit handling
36
+ - [ ] Voice support
37
+ - [ ] Add parameter validation
35
38
  - [ ] Stable release (v1.0.0)
36
- - [ ] Bugfixes, consistency and improvements
39
+ - [ ] Bugfixes, consistency and improvements
data/lib/disrb/message.rb CHANGED
@@ -3,6 +3,43 @@
3
3
  # DiscordApi
4
4
  # The class that contains everything that interacts with the Discord API.
5
5
  class DiscordApi
6
+ def get_channel_messages(channel_id, around: nil, before: nil, after: nil, limit: nil)
7
+ options = { around: around, before: before, after: after }
8
+ specified_keys = options.reject { |_k, v| v.nil? }.keys
9
+
10
+ if specified_keys.size > 1
11
+ @logger.error('You can only specify one of around, before or after. Setting all to nil.')
12
+ around, before, after = nil
13
+ elsif specified_keys.size == 1
14
+ instance_variable_set("@#{specified_keys.first}", options[specified_keys.first])
15
+ end
16
+
17
+ query_string_hash = {}
18
+ query_string_hash[:around] = around unless around.nil?
19
+ query_string_hash[:before] = before unless before.nil?
20
+ query_string_hash[:after] = after unless after.nil?
21
+ query_string_hash[:limit] = limit unless limit.nil?
22
+ query_string = DiscordApi.handle_query_strings(query_string_hash)
23
+ url = "#{@base_url}/channels/#{channel_id}/messages#{query_string}"
24
+ headers = { 'Authorization': @authorization_header }
25
+ response = DiscordApi.get(url, headers)
26
+ return response unless response.status != 200
27
+
28
+ @logger.error("Failed to get messages from channel with ID #{channel_id}. Response: #{response.body}")
29
+ response
30
+ end
31
+
32
+ def get_channel_message(channel_id, message_id)
33
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}"
34
+ headers = { 'Authorization': @authorization_header }
35
+ response = DiscordApi.get(url, headers)
36
+ return response unless response.status != 200
37
+
38
+ @logger.error("Failed to get message with ID #{message_id} from channel with ID #{channel_id}. " \
39
+ "Response: #{response.body}")
40
+ response
41
+ end
42
+
6
43
  def create_message(channel_id, content: nil, nonce: nil, tts: nil, embeds: nil, allowed_mentions: nil,
7
44
  message_reference: nil, components: nil, sticker_ids: nil, files: nil, attachments: nil,
8
45
  flags: nil, enforce_nonce: nil, poll: nil)
@@ -33,4 +70,174 @@ class DiscordApi
33
70
  @logger.error("Failed to create message in channel #{channel_id}. Response: #{response.body}")
34
71
  response
35
72
  end
73
+
74
+ def crosspost_message(channel_id, message_id)
75
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/crosspost"
76
+ headers = { 'Authorization': @authorization_header }
77
+ response = DiscordApi.post(url, nil, headers)
78
+ return response unless response.status != 200
79
+
80
+ @logger.error("Failed to crosspost message with ID #{message_id} in channel with ID #{channel_id}. " \
81
+ "Response: #{response.body}")
82
+ response
83
+ end
84
+
85
+ def create_reaction(channel_id, message_id, emoji_id)
86
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}/@me"
87
+ headers = { 'Authorization': @authorization_header }
88
+ response = DiscordApi.put(url, nil, headers)
89
+ return response unless response.status != 204
90
+
91
+ @logger.error("Failed to create reaction with emoji ID #{emoji_id} in channel with ID #{channel_id} " \
92
+ "for message with ID #{message_id}. Response: #{response.body}")
93
+ response
94
+ end
95
+
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"
98
+ headers = { 'Authorization': @authorization_header }
99
+ response = DiscordApi.delete(url, headers)
100
+ return response unless response.status != 204
101
+
102
+ @logger.error("Failed to delete own reaction with emoji ID #{emoji_id} in channel with ID #{channel_id} " \
103
+ "for message with ID #{message_id}. Response: #{response.body}")
104
+ response
105
+ end
106
+
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}"
109
+ headers = { 'Authorization': @authorization_header }
110
+ response = DiscordApi.delete(url, headers)
111
+ return response unless response.status != 204
112
+
113
+ @logger.error("Failed to delete user reaction with emoji ID #{emoji_id} in channel with ID #{channel_id} " \
114
+ "for message with ID #{message_id} by user with ID #{user_id}. Response: #{response.body}")
115
+ response
116
+ end
117
+
118
+ def get_reactions(channel_id, message_id, emoji_id, type: nil, after: nil, limit: nil)
119
+ query_string_hash = {}
120
+ query_string_hash[:type] = type unless type.nil?
121
+ query_string_hash[:after] = after unless after.nil?
122
+ query_string_hash[:limit] = limit unless limit.nil?
123
+ query_string = DiscordApi.handle_query_strings(query_string_hash)
124
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji_id}#{query_string}"
125
+ headers = { 'Authorization': @authorization_header }
126
+ response = DiscordApi.get(url, headers)
127
+ return response unless response.status != 200
128
+
129
+ @logger.error("Failed to get reactions for emoji with ID #{emoji_id} in channel with ID #{channel_id} " \
130
+ "for message with ID #{message_id}. Response: #{response.body}")
131
+ response
132
+ end
133
+
134
+ def delete_all_reactions(channel_id, message_id)
135
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}/reactions"
136
+ headers = { 'Authorization': @authorization_header }
137
+ response = DiscordApi.delete(url, headers)
138
+ return response unless response.status != 204
139
+
140
+ @logger.error("Failed to delete all reactions in channel with ID #{channel_id} for message with ID #{message_id}" \
141
+ ". Response: #{response.body}")
142
+ end
143
+
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}"
146
+ headers = { 'Authorization': @authorization_header }
147
+ response = DiscordApi.delete(url, headers)
148
+ return response unless response.status != 204
149
+
150
+ @logger.error("Failed to delete all reactions for emoji with ID #{emoji_id} in channel with ID #{channel_id} for " \
151
+ "message with ID #{message_id}. Response: #{response.body}")
152
+ end
153
+
154
+ 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?)
157
+ @logger.warn("No modifications provided for message with ID #{message_id} in channel with ID #{channel_id}. " \
158
+ 'Skipping function.')
159
+ return nil
160
+ end
161
+ output = {}
162
+ output[:content] = content unless content.nil?
163
+ output[:embeds] = embeds unless embeds.nil?
164
+ output[:flags] = flags unless flags.nil?
165
+ output[:allowed_mentions] = allowed_mentions unless allowed_mentions.nil?
166
+ output[:components] = components unless components.nil?
167
+ output[:files] = files unless files.nil?
168
+ output[:payload_json] = payload_json unless payload_json.nil?
169
+ output[:attachments] = attachments unless attachments.nil?
170
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}"
171
+ data = JSON.generate(output)
172
+ headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
173
+ response = DiscordApi.patch(url, data, headers)
174
+ return response unless response.status != 200
175
+
176
+ @logger.error("Failed to edit message with ID #{message_id} in channel with ID #{channel_id}. " \
177
+ "Response: #{response.body}")
178
+ response
179
+ end
180
+
181
+ def delete_message(channel_id, message_id, audit_reason = nil)
182
+ url = "#{@base_url}/channels/#{channel_id}/messages/#{message_id}"
183
+ headers = { 'Authorization': @authorization_header }
184
+ headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
185
+ response = DiscordApi.delete(url, headers)
186
+ return response unless response.status != 204
187
+
188
+ @logger.error("Failed to delete message with ID #{message_id} in channel with ID #{channel_id}. " \
189
+ "Response: #{response.body}")
190
+ response
191
+ end
192
+
193
+ def bulk_delete_messages(channel_id, messages, audit_reason = nil)
194
+ output = { messages: messages }
195
+ url = "#{@base_url}/channels/#{channel_id}/messages/bulk-delete"
196
+ data = JSON.generate(output)
197
+ headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
198
+ headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
199
+ response = DiscordApi.post(url, data, headers)
200
+ return response unless response.status != 204
201
+
202
+ @logger.error("Failed to bulk delete messages in channel with ID #{channel_id}. Response: #{response.body}")
203
+ response
204
+ end
205
+
206
+ def get_channel_pins(channel_id, before: nil, limit: nil)
207
+ query_string_hash = {}
208
+ query_string_hash[:before] = before unless before.nil?
209
+ query_string_hash[:limit] = limit unless limit.nil?
210
+ query_string = DiscordApi.handle_query_strings(query_string_hash)
211
+ url = "#{@base_url}/channels/#{channel_id}/messages/pins#{query_string}"
212
+ headers = { 'Authorization': @authorization_header }
213
+ response = DiscordApi.get(url, headers)
214
+ return response unless response.status != 200
215
+
216
+ @logger.error("Failed to get pinned messages in channel with ID #{channel_id}. Response: #{response.body}")
217
+ response
218
+ end
219
+
220
+ def pin_message(channel_id, message_id, audit_reason = nil)
221
+ url = "#{@base_url}/channels/#{channel_id}/messages/pins/#{message_id}"
222
+ headers = { 'Authorization': @authorization_header }
223
+ headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
224
+ response = DiscordApi.put(url, nil, headers)
225
+ return response unless response.status != 204
226
+
227
+ @logger.error("Failed to pin message with ID #{message_id} in channel with ID #{channel_id}. " \
228
+ "Response: #{response.body}")
229
+ response
230
+ end
231
+
232
+ def unpin_message(channel_id, message_id, audit_reason = nil)
233
+ url = "#{@base_url}/channels/#{channel_id}/messages/pins/#{message_id}"
234
+ headers = { 'Authorization': @authorization_header }
235
+ headers[:'X-Audit-Log-Reason'] = audit_reason unless audit_reason.nil?
236
+ response = DiscordApi.delete(url, headers)
237
+ return response unless response.status != 204
238
+
239
+ @logger.error("Failed to unpin message with ID #{message_id} in channel with ID #{channel_id}. " \
240
+ "Response: #{response.body}")
241
+ response
242
+ end
36
243
  end
data/lib/disrb.rb CHANGED
@@ -429,7 +429,7 @@ class DiscordApi
429
429
  url = if rescue_connection.nil?
430
430
  response = DiscordApi.get("#{@base_url}/gateway")
431
431
  if response.status == 200
432
- "#{JSON.parse(response)['url']}/?v=#{@api_version}&encoding=json"
432
+ "#{JSON.parse(response.body)['url']}/?v=#{@api_version}&encoding=json"
433
433
  else
434
434
  @logger.fatal_error("Failed to get gateway URL. Response: #{response.body}")
435
435
  exit
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: disrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.3
4
+ version: 0.1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hoovad
@@ -94,7 +94,7 @@ files:
94
94
  - lib/disrb/logger.rb
95
95
  - lib/disrb/message.rb
96
96
  - lib/disrb/user.rb
97
- homepage: https://codeberg.org/hoovad/discord.rb
97
+ homepage: https://github.com/hoovad/discord.rb
98
98
  licenses:
99
99
  - MIT
100
100
  metadata: {}