discorb 0.0.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 (66) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +56 -0
  3. data/.yardopts +6 -0
  4. data/Changelog.md +5 -0
  5. data/Gemfile +23 -0
  6. data/Gemfile.lock +70 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +53 -0
  9. data/Rakefile +46 -0
  10. data/bin/console +15 -0
  11. data/bin/setup +8 -0
  12. data/discorb.gemspec +37 -0
  13. data/docs/Examples.md +26 -0
  14. data/docs/events.md +480 -0
  15. data/docs/voice_events.md +283 -0
  16. data/examples/components/authorization_button.rb +43 -0
  17. data/examples/components/select_menu.rb +61 -0
  18. data/examples/extension/main.rb +12 -0
  19. data/examples/extension/message_expander.rb +41 -0
  20. data/examples/simple/eval.rb +32 -0
  21. data/examples/simple/ping_pong.rb +16 -0
  22. data/examples/simple/rolepanel.rb +65 -0
  23. data/examples/simple/wait_for_message.rb +30 -0
  24. data/lib/discorb/application.rb +157 -0
  25. data/lib/discorb/asset.rb +57 -0
  26. data/lib/discorb/audit_logs.rb +323 -0
  27. data/lib/discorb/channel.rb +1101 -0
  28. data/lib/discorb/client.rb +363 -0
  29. data/lib/discorb/color.rb +173 -0
  30. data/lib/discorb/common.rb +123 -0
  31. data/lib/discorb/components.rb +290 -0
  32. data/lib/discorb/dictionary.rb +119 -0
  33. data/lib/discorb/embed.rb +345 -0
  34. data/lib/discorb/emoji.rb +218 -0
  35. data/lib/discorb/emoji_table.rb +3799 -0
  36. data/lib/discorb/error.rb +98 -0
  37. data/lib/discorb/event.rb +35 -0
  38. data/lib/discorb/extend.rb +18 -0
  39. data/lib/discorb/extension.rb +54 -0
  40. data/lib/discorb/file.rb +69 -0
  41. data/lib/discorb/flag.rb +109 -0
  42. data/lib/discorb/gateway.rb +967 -0
  43. data/lib/discorb/gateway_requests.rb +47 -0
  44. data/lib/discorb/guild.rb +1244 -0
  45. data/lib/discorb/guild_template.rb +211 -0
  46. data/lib/discorb/image.rb +43 -0
  47. data/lib/discorb/integration.rb +111 -0
  48. data/lib/discorb/intents.rb +137 -0
  49. data/lib/discorb/interaction.rb +333 -0
  50. data/lib/discorb/internet.rb +285 -0
  51. data/lib/discorb/invite.rb +145 -0
  52. data/lib/discorb/log.rb +70 -0
  53. data/lib/discorb/member.rb +232 -0
  54. data/lib/discorb/message.rb +583 -0
  55. data/lib/discorb/modules.rb +138 -0
  56. data/lib/discorb/permission.rb +270 -0
  57. data/lib/discorb/presence.rb +308 -0
  58. data/lib/discorb/reaction.rb +48 -0
  59. data/lib/discorb/role.rb +189 -0
  60. data/lib/discorb/sticker.rb +157 -0
  61. data/lib/discorb/user.rb +163 -0
  62. data/lib/discorb/utils.rb +16 -0
  63. data/lib/discorb/voice_state.rb +251 -0
  64. data/lib/discorb/webhook.rb +420 -0
  65. data/lib/discorb.rb +51 -0
  66. metadata +120 -0
@@ -0,0 +1,345 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents an embed of discord.
6
+ #
7
+ class Embed
8
+ # @return [String, nil] The title of embed.
9
+ attr_accessor :title
10
+ # @return [String, nil] The description of embed.
11
+ attr_accessor :description
12
+ # @return [String, nil] The url of embed.
13
+ attr_accessor :url
14
+ # @return [Time, nil] The timestamp of embed.
15
+ attr_accessor :timestamp
16
+ # @return [Discorb::Color, nil] The color of embed.
17
+ attr_accessor :color
18
+ # @return [Discorb::Embed::Author, nil] The author of embed.
19
+ attr_accessor :author
20
+ # @return [Array<Discorb::Embed::Field>] The fields of embed.
21
+ attr_accessor :fields
22
+ # @return [Discorb::Embed::Footer, nil] The footer of embed.
23
+ attr_accessor :footer
24
+ # @return [Symbol] The type of embed.
25
+ attr_reader :type
26
+ attr_reader :image, :thumbnail
27
+
28
+ # @!attribute [rw] image
29
+ # @return [Discorb::Embed::Image] The image of embed.
30
+ # @!attribute [rw] thumbnail
31
+ # @return [Discorb::Embed::Thumbnail] The thumbnail of embed.
32
+
33
+ #
34
+ # Initialize a new Embed object.
35
+ #
36
+ # @param [String] title The title of embed.
37
+ # @param [String] description The description of embed.
38
+ # @param [Discorb::Color] color The color of embed.
39
+ # @param [String] url The url of embed.
40
+ # @param [Time] timestamp The timestamp of embed.
41
+ # @param [Discorb::Embed::Author] author The author field of embed.
42
+ # @param [Array<Discorb::Embed::Field>] fields The fields of embed.
43
+ # @param [Discorb::Embed::Footer] footer The footer of embed.
44
+ # @param [Discorb::Embed::Image] image The image of embed.
45
+ # @param [Discorb::Embed::Thumbnail] thumbnail The thumbnail of embed.
46
+ #
47
+ def initialize(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil,
48
+ fields: nil, footer: nil, image: nil, thumbnail: nil, data: nil)
49
+ if data.nil?
50
+ @title = title
51
+ @description = description
52
+ @url = url
53
+ @timestamp = timestamp
54
+ @color = color
55
+ @author = author
56
+ @fields = fields || []
57
+ @footer = footer
58
+ @image = image
59
+ @thumbnail = thumbnail
60
+ @type = "rich"
61
+ else
62
+ @title = data[:title]
63
+ @description = data[:description]
64
+ @url = data[:url]
65
+ @timestamp = data[:timestamp] && Time.iso8601(data[:timestamp])
66
+ @type = data[:type]
67
+ @color = data[:color] && Color.new(data[:color])
68
+ @footer = data[:footer] && Footer.new(data[:footer][:text], icon: data[:footer][:icon])
69
+ @author = if data[:author]
70
+ Author.new(data[:author][:name], icon: data[:author][:icon],
71
+ url: data[:author][:url])
72
+ end
73
+ @thumbnail = data[:thumbnail] && Thumbnail.new(data[:thumbnail])
74
+ @image = data[:image] && Image.new(data[:image])
75
+ @video = data[:video] && Video.new(data[:video])
76
+ @provider = data[:provider] && Provider.new(data[:provider])
77
+ @fields = data[:fields] ? data[:fields].map { |f| Field.new(f[:name], f[:value], inline: f[:inline]) } : []
78
+ end
79
+ end
80
+
81
+ def image=(value)
82
+ @image = value.is_a? String ? Image.new(value) : value
83
+ end
84
+
85
+ def thumbnail=(value)
86
+ @thumbnail = value.is_a? String ? Thumbnail.new(value) : value
87
+ end
88
+
89
+ #
90
+ # Convert embed to hash.
91
+ #
92
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-structure Offical Discord API Docs
93
+ # @return [Hash] Converted embed.
94
+ #
95
+ def to_hash
96
+ {
97
+ title: @title,
98
+ description: @description,
99
+ url: @url,
100
+ timestamp: @timestamp&.iso8601,
101
+ color: @color&.to_i,
102
+ footer: @footer&.to_hash,
103
+ image: @image&.to_hash,
104
+ thumbnail: @thumbnail&.to_hash,
105
+ author: @author&.to_hash,
106
+ fields: @fields&.map { |f| f.to_hash },
107
+ }
108
+ end
109
+
110
+ #
111
+ # Represents an author of embed.
112
+ #
113
+ class Author
114
+ # @return [String] The name of author.
115
+ attr_accessor :name
116
+ # @return [String, nil] The url of author.
117
+ attr_accessor :url
118
+ # @return [String, nil] The icon url of author.
119
+ attr_accessor :icon
120
+
121
+ #
122
+ # Initialize a new Author object.
123
+ #
124
+ # @param [String] name The name of author.
125
+ # @param [String] url The url of author.
126
+ # @param [String] icon The icon url of author.
127
+ #
128
+ def initialize(name, url: nil, icon: nil)
129
+ @name = name
130
+ @url = url
131
+ @icon = icon
132
+ end
133
+
134
+ #
135
+ # Convert author to hash.
136
+ #
137
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure Offical Discord API Docs
138
+ # @return [Hash] Converted author.
139
+ #
140
+ def to_hash
141
+ {
142
+ name: @name,
143
+ url: @url,
144
+ icon_url: @icon,
145
+ }
146
+ end
147
+ end
148
+
149
+ #
150
+ # Represemts a footer of embed.
151
+ #
152
+ class Footer
153
+ attr_accessor :text, :icon
154
+
155
+ #
156
+ # Initialize a new Footer object.
157
+ #
158
+ # @param [String] text The text of footer.
159
+ # @param [String] icon The icon url of footer.
160
+ #
161
+ def initialize(text, icon: nil)
162
+ @text = text
163
+ @icon = icon
164
+ end
165
+
166
+ #
167
+ # Convert footer to hash.
168
+ #
169
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure Offical Discord API Docs
170
+ # @return [Hash] Converted footer.
171
+ #
172
+ def to_hash
173
+ {
174
+ text: @text,
175
+ icon_url: @icon,
176
+ }
177
+ end
178
+ end
179
+
180
+ #
181
+ # Represents a field of embed.
182
+ #
183
+ class Field
184
+ # @return [String] The name of field.
185
+ attr_accessor :name
186
+ # @return [String] The value of field.
187
+ attr_accessor :value
188
+ # @return [Boolean] Whether the field is inline.
189
+ attr_accessor :inline
190
+
191
+ #
192
+ # Initialize a new Field object.
193
+ #
194
+ # @param [String] name The name of field.
195
+ # @param [String] value The value of field.
196
+ # @param [Boolean] inline Whether the field is inline.
197
+ #
198
+ def initialize(name, value, inline: true)
199
+ @name = name
200
+ @value = value
201
+ @inline = inline
202
+ end
203
+
204
+ #
205
+ # Convert field to hash.
206
+ #
207
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure Offical Discord API Docs
208
+ # @return [Hash] Converted field.
209
+ #
210
+ def to_hash
211
+ {
212
+ name: @name,
213
+ value: @value,
214
+ inline: @inline,
215
+ }
216
+ end
217
+ end
218
+
219
+ #
220
+ # Represents an image of embed.
221
+ #
222
+ class Image
223
+ # @return [String] The url of image.
224
+ attr_accessor :url
225
+ # @return [String] The proxy url of image.
226
+ # @return [nil] The Image object wasn't created from gateway.
227
+ attr_reader :proxy_url
228
+ # @return [Integer] The height of image.
229
+ # @return [nil] The Image object wasn't created from gateway.
230
+ attr_reader :height
231
+ # @return [Integer] The width of image.
232
+ # @return [nil] The Image object wasn't created from gateway.
233
+ attr_reader :width
234
+
235
+ #
236
+ # Initialize a new Image object.
237
+ #
238
+ # @param [String] url URL of image.
239
+ #
240
+ def initialize(url)
241
+ data = url
242
+ if data.is_a? String
243
+ @url = data
244
+ else
245
+ @url = data[:url]
246
+ @proxy_url = data[:proxy_url]
247
+ @height = data[:height]
248
+ @width = data[:width]
249
+ end
250
+ end
251
+
252
+ #
253
+ # Convert image to hash for sending.
254
+ #
255
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure Offical Discord API Docs
256
+ # @return [Hash] Converted image.
257
+ #
258
+ def to_hash
259
+ { url: @url }
260
+ end
261
+ end
262
+
263
+ #
264
+ # Represents a thumbnail of embed.
265
+ #
266
+ class Thumbnail
267
+ # @return [String] The url of thumbnail.
268
+ attr_accessor :url
269
+ # @return [String] The proxy url of thumbnail.
270
+ # @return [nil] The Thumbnail object wasn't created from gateway.
271
+ attr_reader :proxy_url
272
+ # @return [Integer] The height of thumbnail.
273
+ # @return [nil] The Thumbnail object wasn't created from gateway.
274
+ attr_reader :height
275
+ # @return [Integer] The width of thumbnail.
276
+ # @return [nil] The Thumbnail object wasn't created from gateway.
277
+ attr_reader :width
278
+
279
+ #
280
+ # Initialize a new Thumbnail object.
281
+ #
282
+ # @param [String] url URL of thumbnail.
283
+ #
284
+ def initialize(url)
285
+ data = url
286
+ if data.is_a? String
287
+ @url = data
288
+ else
289
+ @url = data[:url]
290
+ @proxy_url = data[:proxy_url]
291
+ @height = data[:height]
292
+ @width = data[:width]
293
+ end
294
+ end
295
+
296
+ #
297
+ # Convert thumbnail to hash for sending.
298
+ #
299
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure Offical Discord API Docs
300
+ # @return [Hash] Converted thumbnail.
301
+ #
302
+ def to_hash
303
+ { url: @url }
304
+ end
305
+ end
306
+
307
+ #
308
+ # Represents a video of embed.
309
+ #
310
+ class Video
311
+ # @return [String] The url of video.
312
+ attr_reader :url
313
+ # @return [String] The proxy url of video.
314
+ attr_reader :proxy_url
315
+ # @return [Integer] The height of video.
316
+ attr_reader :height
317
+ # @return [Integer] The width of video.
318
+ attr_reader :width
319
+
320
+ # @!visibility private
321
+ def initialize(data)
322
+ @url = data[:url]
323
+ @proxy_url = data[:proxy_url]
324
+ @height = data[:height]
325
+ @width = data[:width]
326
+ end
327
+ end
328
+
329
+ #
330
+ # Represents a provider of embed.
331
+ #
332
+ class Provider
333
+ # @return [String] The name of provider.
334
+ attr_reader :name
335
+ # @return [String] The url of provider.
336
+ attr_reader :url
337
+
338
+ # @!visibility private
339
+ def initialize(name, url)
340
+ @name = name
341
+ @url = url
342
+ end
343
+ end
344
+ end
345
+ end
@@ -0,0 +1,218 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module Discorb
6
+ # Represents a Discord emoji.
7
+ # @abstract
8
+ class Emoji
9
+ end
10
+
11
+ # Represents a custom emoji in discord.
12
+ class CustomEmoji < Emoji
13
+ # @return [Discorb::Snowflake] The ID of the emoji.
14
+ attr_reader :id
15
+ # @return [String] The name of the emoji.
16
+ attr_reader :name
17
+ # @return [Array<Discorb::Role>] The roles that can use this emoji.
18
+ attr_reader :roles
19
+ # @return [Discorb::User] The user that created this emoji.
20
+ attr_reader :user
21
+ # @return [Boolean] Whether the emoji requires colons.
22
+ attr_reader :guild
23
+ # @return [Boolean] whether the emoji is managed by integration (ex: Twitch).
24
+ attr_reader :managed
25
+ alias managed? managed
26
+ # @return [Boolean] whether the emoji requires colons.
27
+ attr_reader :require_colons
28
+ alias require_colons? require_colons
29
+ # @return [Boolean] whether the emoji is available.
30
+ attr_reader :available
31
+ alias available? available
32
+
33
+ # @!attribute [r] roles?
34
+ # @return [Boolean] whether or not this emoji is restricted to certain roles.
35
+
36
+ # @!visibility private
37
+ def initialize(client, guild, data)
38
+ @client = client
39
+ @guild = guild
40
+ @data = {}
41
+ _set_data(data)
42
+ end
43
+
44
+ #
45
+ # Format the emoji for sending.
46
+ #
47
+ # @return [String] the formatted emoji.
48
+ #
49
+ def to_s
50
+ "<#{@animated ? "a" : ""}:#{@name}:#{id}>"
51
+ end
52
+
53
+ #
54
+ # Format the emoji for URI.
55
+ #
56
+ # @return [String] the formatted emoji.
57
+ #
58
+ def to_uri
59
+ "#{@name}:#{@id}"
60
+ end
61
+
62
+ def roles?
63
+ @roles != []
64
+ end
65
+
66
+ alias role? roles?
67
+
68
+ def inspect
69
+ "#<#{self.class} id=#{@id} :#{@name}:>"
70
+ end
71
+
72
+ #
73
+ # Edit the emoji.
74
+ # @macro async
75
+ # @macro http
76
+ # @macro edit
77
+ #
78
+ # @param [String] name The new name of the emoji.
79
+ # @param [Array<Discorb::Role>] roles The new roles that can use this emoji.
80
+ # @param [String] reason The reason for editing the emoji.
81
+ #
82
+ # @return [self] The edited emoji.
83
+ #
84
+ def edit(name: :unset, roles: :unset, reason: nil)
85
+ Async do
86
+ payload = {}
87
+ payload[:name] = name if name != :unset
88
+ payload[:roles] = roles.map { |r| Discorb::Utils.try(r, :id) } if roles != :unset
89
+ @client.internet.patch("/guilds/#{@guild.id}/emojis/#{@id}", payload, audit_log_reason: reason)
90
+ self
91
+ end
92
+ end
93
+
94
+ alias modify edit
95
+
96
+ #
97
+ # Delete the emoji.
98
+ # @macro async
99
+ # @macro http
100
+ #
101
+ # @param [String] reason The reason for deleting the emoji.
102
+ #
103
+ # @return [self] The deleted emoji.
104
+ #
105
+ def delete!(reason: nil)
106
+ Async do
107
+ @client.internet.delete("/guilds/#{@guild.id}/emojis/#{@id}", audit_log_reason: reason).wait
108
+ @available = false
109
+ self
110
+ end
111
+ end
112
+
113
+ alias destroy! delete!
114
+
115
+ private
116
+
117
+ def _set_data(data)
118
+ @id = Snowflake.new(data[:id])
119
+ @name = data[:name]
120
+ @roles = data[:role] ? data[:role].map { |r| Role.new(@client, r) } : []
121
+ @user = User.new(@client, data[:user]) if data[:user]
122
+ @require_colons = data[:require_colons]
123
+ @managed = data[:managed]
124
+ @animated = data[:animated]
125
+ @available = data[:available]
126
+ @guild.emojis[@id] = self unless data[:no_cache]
127
+ @data.update(data)
128
+ end
129
+ end
130
+
131
+ #
132
+ # Represents a partial custom emoji in discord.
133
+ #
134
+ class PartialEmoji < DiscordModel
135
+ # @return [Discorb::Snowflake] The ID of the emoji.
136
+ attr_reader :id
137
+ # @return [String] The name of the emoji.
138
+ attr_reader :name
139
+ # @return [Boolean] Whether the emoji is deleted.
140
+ attr_reader :deleted
141
+ alias deleted? deleted
142
+
143
+ # @!visibility private
144
+ def initialize(data)
145
+ @id = Snowflake.new(data[:id])
146
+ @name = data[:name]
147
+ @animated = data[:animated]
148
+ @deleted = @name.nil?
149
+ end
150
+
151
+ #
152
+ # Format the emoji for URI.
153
+ #
154
+ # @return [String] the formatted emoji.
155
+ #
156
+ def to_uri
157
+ "#{@name}:#{@id}"
158
+ end
159
+
160
+ def inspect
161
+ "#<#{self.class} id=#{@id} :#{@name}:>"
162
+ end
163
+
164
+ #
165
+ # Format the emoji for sending.
166
+ #
167
+ # @return [String] the formatted emoji.
168
+ #
169
+ def to_s
170
+ "<#{@animated ? "a" : ""}:#{@name}:#{@id}>"
171
+ end
172
+ end
173
+
174
+ #
175
+ # Represents a unicode emoji (default emoji) in discord.
176
+ #
177
+ class UnicodeEmoji < Emoji
178
+ # @return [String] The name of the emoji. (e.g. :grinning:)
179
+ attr_reader :name
180
+ # @return [String] The unicode value of the emoji. (e.g. U+1F600)
181
+ attr_reader :value
182
+
183
+ # @!visibility private
184
+ def initialize(name)
185
+ if EmojiTable::DISCORD_TO_UNICODE.key?(name)
186
+ @name = name
187
+ @value = EmojiTable::DISCORD_TO_UNICODE[name]
188
+ elsif EmojiTable::UNICODE_TO_DISCORD.key?(name)
189
+ @name = EmojiTable::UNICODE_TO_DISCORD[name][0]
190
+ @value = name
191
+ else
192
+ raise ArgumentError, "No such emoji: #{name}"
193
+ end
194
+ end
195
+
196
+ # @return [String] The unicode string of the emoji.
197
+ def to_s
198
+ @value
199
+ end
200
+
201
+ #
202
+ # Format the emoji for URI.
203
+ #
204
+ # @return [String] the formatted emoji.
205
+ #
206
+ def to_uri
207
+ URI.encode_www_form_component(@value)
208
+ end
209
+
210
+ def inspect
211
+ "#<#{self.class} :#{@name}:>"
212
+ end
213
+
214
+ class << self
215
+ alias [] new
216
+ end
217
+ end
218
+ end