discorb 0.13.4 → 0.15.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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +2 -0
  3. data/.github/workflows/build_version.yml +1 -1
  4. data/.github/workflows/codeql-analysis.yml +70 -0
  5. data/.github/workflows/lint-push.yml +20 -0
  6. data/.github/workflows/lint.yml +16 -0
  7. data/.rubocop.yml +74 -0
  8. data/Changelog.md +30 -0
  9. data/Gemfile +7 -3
  10. data/Rakefile +28 -22
  11. data/discorb.gemspec +1 -0
  12. data/docs/events.md +50 -0
  13. data/docs/faq.md +8 -8
  14. data/examples/commands/bookmarker.rb +2 -1
  15. data/examples/commands/hello.rb +1 -0
  16. data/examples/commands/inspect.rb +3 -2
  17. data/examples/components/authorization_button.rb +2 -1
  18. data/examples/components/select_menu.rb +2 -1
  19. data/examples/extension/main.rb +1 -0
  20. data/examples/extension/message_expander.rb +1 -0
  21. data/examples/simple/eval.rb +3 -2
  22. data/examples/simple/ping_pong.rb +1 -0
  23. data/examples/simple/rolepanel.rb +1 -0
  24. data/examples/simple/wait_for_message.rb +4 -3
  25. data/exe/discorb +8 -7
  26. data/lib/discorb/allowed_mentions.rb +71 -0
  27. data/lib/discorb/app_command/command.rb +336 -0
  28. data/lib/discorb/app_command/handler.rb +168 -0
  29. data/lib/discorb/app_command.rb +2 -426
  30. data/lib/discorb/application.rb +16 -7
  31. data/lib/discorb/asset.rb +11 -0
  32. data/lib/discorb/{file.rb → attachment.rb} +55 -33
  33. data/lib/discorb/audit_logs.rb +45 -7
  34. data/lib/discorb/channel.rb +65 -15
  35. data/lib/discorb/client.rb +34 -27
  36. data/lib/discorb/common.rb +19 -27
  37. data/lib/discorb/components/button.rb +105 -0
  38. data/lib/discorb/components/select_menu.rb +143 -0
  39. data/lib/discorb/components/text_input.rb +96 -0
  40. data/lib/discorb/components.rb +11 -276
  41. data/lib/discorb/dictionary.rb +5 -0
  42. data/lib/discorb/embed.rb +73 -40
  43. data/lib/discorb/emoji.rb +48 -5
  44. data/lib/discorb/error.rb +9 -5
  45. data/lib/discorb/event.rb +36 -24
  46. data/lib/discorb/exe/about.rb +1 -0
  47. data/lib/discorb/exe/irb.rb +4 -3
  48. data/lib/discorb/exe/new.rb +6 -7
  49. data/lib/discorb/exe/run.rb +2 -1
  50. data/lib/discorb/exe/setup.rb +8 -5
  51. data/lib/discorb/exe/show.rb +1 -0
  52. data/lib/discorb/extend.rb +19 -14
  53. data/lib/discorb/extension.rb +5 -1
  54. data/lib/discorb/gateway.rb +112 -51
  55. data/lib/discorb/gateway_requests.rb +4 -7
  56. data/lib/discorb/guild.rb +73 -41
  57. data/lib/discorb/guild_template.rb +26 -5
  58. data/lib/discorb/http.rb +38 -18
  59. data/lib/discorb/integration.rb +24 -9
  60. data/lib/discorb/intents.rb +16 -11
  61. data/lib/discorb/interaction/autocomplete.rb +6 -5
  62. data/lib/discorb/interaction/command.rb +66 -12
  63. data/lib/discorb/interaction/components.rb +19 -3
  64. data/lib/discorb/interaction/modal.rb +33 -0
  65. data/lib/discorb/interaction/response.rb +45 -4
  66. data/lib/discorb/interaction/root.rb +16 -0
  67. data/lib/discorb/interaction.rb +2 -1
  68. data/lib/discorb/invite.rb +11 -7
  69. data/lib/discorb/log.rb +5 -5
  70. data/lib/discorb/member.rb +22 -3
  71. data/lib/discorb/message.rb +39 -234
  72. data/lib/discorb/message_meta.rb +186 -0
  73. data/lib/discorb/modules.rb +39 -15
  74. data/lib/discorb/permission.rb +16 -7
  75. data/lib/discorb/presence.rb +45 -9
  76. data/lib/discorb/rate_limit.rb +7 -4
  77. data/lib/discorb/reaction.rb +6 -0
  78. data/lib/discorb/role.rb +12 -0
  79. data/lib/discorb/sticker.rb +22 -14
  80. data/lib/discorb/user.rb +12 -1
  81. data/lib/discorb/utils/colored_puts.rb +1 -0
  82. data/lib/discorb/voice_state.rb +23 -2
  83. data/lib/discorb/webhook.rb +54 -3
  84. data/lib/discorb.rb +5 -2
  85. data/sig/discorb.rbs +838 -702
  86. data/template-replace/scripts/arrow.rb +1 -0
  87. data/template-replace/scripts/favicon.rb +1 -0
  88. data/template-replace/scripts/index.rb +2 -1
  89. data/template-replace/scripts/locale_ja.rb +5 -4
  90. data/template-replace/scripts/sidebar.rb +1 -0
  91. data/template-replace/scripts/version.rb +7 -10
  92. data/template-replace/scripts/yard_replace.rb +5 -4
  93. metadata +17 -3
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: true
2
+ module Discorb
3
+ #
4
+ # Represents a message in Discord.
5
+ #
6
+ class Message < DiscordModel
7
+ #
8
+ # Represents message flag.
9
+ # ## Flag fields
10
+ # |Field|Value|
11
+ # |-|-|
12
+ # |`1 << 0`|`:crossposted`|
13
+ # |`1 << 1`|`:crosspost`|
14
+ # |`1 << 2`|`:supress_embeds`|
15
+ # |`1 << 3`|`:source_message_deleted`|
16
+ # |`1 << 4`|`:urgent`|
17
+ # |`1 << 5`|`:has_thread`|
18
+ # |`1 << 6`|`:ephemeral`|
19
+ # |`1 << 7`|`:loading`|
20
+ #
21
+ class Flag < Discorb::Flag
22
+ @bits = {
23
+ crossposted: 0,
24
+ crosspost: 1,
25
+ supress_embeds: 2,
26
+ source_message_deleted: 3,
27
+ urgent: 4,
28
+ has_thread: 5,
29
+ ephemeral: 6,
30
+ loading: 7,
31
+ }.freeze
32
+ end
33
+
34
+ #
35
+ # Represents reference of message.
36
+ #
37
+ class Reference
38
+ # @return [Discorb::Snowflake] The guild ID.
39
+ attr_accessor :guild_id
40
+ # @return [Discorb::Snowflake] The channel ID.
41
+ attr_accessor :channel_id
42
+ # @return [Discorb::Snowflake] The message ID.
43
+ attr_accessor :message_id
44
+ # @return [Boolean] Whether fail the request if the message is not found.
45
+ attr_accessor :fail_if_not_exists
46
+
47
+ alias fail_if_not_exists? fail_if_not_exists
48
+
49
+ #
50
+ # Initialize a new reference.
51
+ #
52
+ # @param [Discorb::Snowflake] guild_id The guild ID.
53
+ # @param [Discorb::Snowflake] channel_id The channel ID.
54
+ # @param [Discorb::Snowflake] message_id The message ID.
55
+ # @param [Boolean] fail_if_not_exists Whether fail the request if the message is not found.
56
+ #
57
+ def initialize(guild_id, channel_id, message_id, fail_if_not_exists: true)
58
+ @guild_id = guild_id
59
+ @channel_id = channel_id
60
+ @message_id = message_id
61
+ @fail_if_not_exists = fail_if_not_exists
62
+ end
63
+
64
+ #
65
+ # Convert the reference to a hash.
66
+ #
67
+ # @return [Hash] The hash.
68
+ #
69
+ def to_hash
70
+ {
71
+ message_id: @message_id,
72
+ channel_id: @channel_id,
73
+ guild_id: @guild_id,
74
+ fail_if_not_exists: @fail_if_not_exists,
75
+ }
76
+ end
77
+
78
+ alias to_reference to_hash
79
+
80
+ #
81
+ # Initialize a new reference from a hash.
82
+ #
83
+ # @param [Hash] data The hash.
84
+ #
85
+ # @return [Discorb::Message::Reference] The reference.
86
+ # @see https://discord.com/developers/docs/resources/channel#message-reference-object
87
+ #
88
+ def self.from_hash(data)
89
+ new(data[:guild_id], data[:channel_id], data[:message_id], fail_if_not_exists: data[:fail_if_not_exists])
90
+ end
91
+
92
+ def inspect
93
+ "#<#{self.class.name} #{@channel_id}/#{@message_id}>"
94
+ end
95
+ end
96
+
97
+ #
98
+ # Represents a sticker.
99
+ #
100
+ class Sticker
101
+ # @return [Discorb::Snowflake] The sticker ID.
102
+ attr_reader :id
103
+ # @return [String] The sticker name.
104
+ attr_reader :name
105
+ # @return [Symbol] The sticker format.
106
+ attr_reader :format
107
+
108
+ def initialize(data)
109
+ @id = Snowflake.new(data[:id])
110
+ @name = data[:name]
111
+ @format = Discorb::Sticker::STICKER_FORMAT[data[:format]]
112
+ end
113
+
114
+ def inspect
115
+ "#<#{self.class.name} #{@id}: #{@name} format=#{@format}>"
116
+ end
117
+ end
118
+
119
+ #
120
+ # Represents a interaction of message.
121
+ #
122
+ class Interaction < DiscordModel
123
+ # @return [Discorb::Snowflake] The interaction ID.
124
+ attr_reader :id
125
+ # @return [String] The name of command.
126
+ # @return [nil] If the message is not a command.
127
+ attr_reader :name
128
+ # @return [Class] The type of interaction.
129
+ attr_reader :type
130
+ # @return [Discorb::User] The user.
131
+ attr_reader :user
132
+
133
+ #
134
+ # Initialize a new interaction.
135
+ # @private
136
+ #
137
+ # @param [Discorb::Client] client The client.
138
+ # @param [Hash] data The interaction data.
139
+ #
140
+ def initialize(client, data)
141
+ @id = Snowflake.new(data[:id])
142
+ @name = data[:name]
143
+ @type = Discorb::Interaction.descendants.find { |c| c.interaction_type == data[:type] }
144
+ @user = client.users[data[:user][:id]] || User.new(client, data[:user])
145
+ end
146
+
147
+ def inspect
148
+ "<#{self.class.name} #{@id}: #{@name} type=#{@type} #{@user}>"
149
+ end
150
+ end
151
+
152
+ #
153
+ # Represents a activity of message.
154
+ #
155
+ class Activity < DiscordModel
156
+ # @return [String] The name of activity.
157
+ attr_reader :name
158
+ # @return [Symbol] The type of activity.
159
+ attr_reader :type
160
+
161
+ # @private
162
+ # @return [{Integer => Symbol}] The mapping of activity type.
163
+ TYPES = {
164
+ 1 => :join,
165
+ 2 => :spectate,
166
+ 3 => :listen,
167
+ 5 => :join_request,
168
+ }.freeze
169
+
170
+ #
171
+ # Initialize a new activity.
172
+ # @private
173
+ #
174
+ # @param [Hash] data The activity data.
175
+ #
176
+ def initialize(data)
177
+ @name = data[:name]
178
+ @type = TYPES[data[:type]]
179
+ end
180
+
181
+ def inspect
182
+ "<#{self.class.name} #{@name} type=#{@type}>"
183
+ end
184
+ end
185
+ end
186
+ end
@@ -16,13 +16,13 @@ module Discorb
16
16
  # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions.
17
17
  # @param [Discorb::Message, Discorb::Message::Reference] reference The message to reply to.
18
18
  # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
19
- # @param [Discorb::File] file The file to send.
20
- # @param [Array<Discorb::File>] files The files to send.
19
+ # @param [Discorb::Attachment] attachment The attachment to send.
20
+ # @param [Array<Discorb::Attachment>] attachments The attachments to send.
21
21
  #
22
22
  # @return [Async::Task<Discorb::Message>] The message sent.
23
23
  #
24
24
  def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil,
25
- reference: nil, components: nil, file: nil, files: nil)
25
+ reference: nil, components: nil, attachment: nil, attachments: nil)
26
26
  Async do
27
27
  payload = {}
28
28
  payload[:content] = content if content
@@ -37,8 +37,17 @@ module Discorb
37
37
  allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash
38
38
  payload[:message_reference] = reference.to_reference if reference
39
39
  payload[:components] = Component.to_payload(components) if components
40
- files = [file]
41
- _resp, data = @client.http.multipart_request(Route.new("/channels/#{channel_id.wait}/messages", "//channels/:channel_id/messages", :post), payload, files).wait
40
+ attachments ||= attachment ? [attachment] : []
41
+
42
+ payload[:attachments] = attachments.map.with_index do |a, i|
43
+ {
44
+ id: i,
45
+ filename: a.filename,
46
+ description: a.description,
47
+ }
48
+ end
49
+
50
+ _resp, data = @client.http.multipart_request(Route.new("/channels/#{channel_id.wait}/messages", "//channels/:channel_id/messages", :post), payload, attachments).wait
42
51
  Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
43
52
  end
44
53
  end
@@ -48,33 +57,48 @@ module Discorb
48
57
  #
49
58
  # Edit a message.
50
59
  # @async
60
+ # @!macro edit
51
61
  #
52
62
  # @param [#to_s] message_id The message id.
53
63
  # @param [String] content The message content.
54
64
  # @param [Discorb::Embed] embed The embed to send.
55
65
  # @param [Array<Discorb::Embed>] embeds The embeds to send.
56
66
  # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions.
67
+ # @param [Array<Discorb::Attachment>] attachments The new attachments.
57
68
  # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
58
69
  # @param [Boolean] supress Whether to supress embeds.
59
70
  #
60
71
  # @return [Async::Task<void>] The task.
61
72
  #
62
- def edit_message(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil,
63
- components: nil, supress: nil)
73
+ def edit_message(message_id, content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset,
74
+ attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset)
64
75
  Async do
65
76
  payload = {}
66
- payload[:content] = content if content
67
- tmp_embed = if embed
77
+ payload[:content] = content if content != Discorb::Unset
78
+ tmp_embed = if embed != Discorb::Unset
68
79
  [embed]
69
- elsif embeds
80
+ elsif embeds != Discorb::Unset
70
81
  embeds
71
82
  end
72
83
  payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
73
84
  payload[:allowed_mentions] =
74
- allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash
75
- payload[:components] = Component.to_payload(components) if components
76
- payload[:flags] = (supress ? 1 << 2 : 0) unless supress.nil?
77
- @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{message_id}", "//channels/:channel_id/messages/:message_id", :patch), payload).wait
85
+ allowed_mentions == Discorb::Unset ? @client.allowed_mentions.to_hash : allowed_mentions.to_hash(@client.allowed_mentions)
86
+ payload[:components] = Component.to_payload(components) if components != Discorb::Unset
87
+ payload[:flags] = (supress ? 1 << 2 : 0) if supress != Discorb::Unset
88
+ if attachments != Discorb::Unset
89
+ payload[:attachments] = attachments.map.with_index do |a, i|
90
+ {
91
+ id: i,
92
+ filename: a.filename,
93
+ description: a.description,
94
+ }
95
+ end
96
+ end
97
+ @client.http.multipart_request(
98
+ Route.new("/channels/#{channel_id.wait}/messages/#{message_id}", "//channels/:channel_id/messages/:message_id", :patch),
99
+ payload,
100
+ attachments == Discorb::Unset ? [] : attachments
101
+ ).wait
78
102
  end
79
103
  end
80
104
 
@@ -204,7 +228,7 @@ module Discorb
204
228
  post_task.stop
205
229
  end
206
230
  else
207
- Async do |task|
231
+ Async do |_task|
208
232
  @client.http.request(Route.new("/channels/#{@id}/typing", "//channels/:channel_id/typing", :post), {})
209
233
  end
210
234
  end
@@ -138,7 +138,13 @@ module Discorb
138
138
  }.freeze
139
139
  @bits = @raw_bits.transform_values { |v| 1 << v }.freeze
140
140
 
141
+ #
142
+ # Initializes a new PermissionOverwrite.
141
143
  # @private
144
+ #
145
+ # @param allow [Integer] The allowed permissions.
146
+ # @param deny [Integer] The denied permissions.
147
+ #
142
148
  def initialize(allow, deny)
143
149
  @allow = allow
144
150
  @deny = deny
@@ -174,13 +180,16 @@ module Discorb
174
180
  # @return [Hash] The permission overwrite as a hash.
175
181
  #
176
182
  def to_hash
177
- self.class.bits.keys.map do |field|
178
- [field, if @allow & self.class.bits[field] != 0
179
- true
180
- elsif @deny & self.class.bits[method] != 0
181
- false
182
- end]
183
- end.to_h
183
+ self.class.bits.keys.to_h do |field|
184
+ [
185
+ field,
186
+ if @allow & self.class.bits[field] != 0
187
+ true
188
+ elsif @deny & self.class.bits[field] != 0
189
+ false
190
+ end,
191
+ ]
192
+ end
184
193
  end
185
194
 
186
195
  #
@@ -19,7 +19,13 @@ module Discorb
19
19
  # @!attribute [r] activity
20
20
  # @return [Discorb::Presence::Activity] The activity of the presence.
21
21
 
22
+ #
23
+ # Initialize a presence.
22
24
  # @private
25
+ #
26
+ # @param [Discorb::Client] client The client.
27
+ # @param [Hash] data The data of the presence.
28
+ #
23
29
  def initialize(client, data)
24
30
  @client = client
25
31
  @data = data
@@ -80,19 +86,26 @@ module Discorb
80
86
  # @return [Discorb::Presence::Activity::Flag] The flags of the activity.
81
87
  attr_reader :flags
82
88
 
83
- @activity_types = {
89
+ # @private
90
+ # @return [{Integer => Symbol}] The mapping of activity types.
91
+ ACTIVITY_TYPES = {
84
92
  0 => :game,
85
93
  1 => :streaming,
86
94
  2 => :listening,
87
95
  3 => :watching,
88
96
  4 => :custom,
89
97
  5 => :competing,
90
- }
98
+ }.freeze
91
99
 
100
+ #
101
+ # Initialize the activity.
92
102
  # @private
103
+ #
104
+ # @param [Hash] data The activity data.
105
+ #
93
106
  def initialize(data)
94
107
  @name = data[:name]
95
- @type = self.class.activity_types[data[:type]]
108
+ @type = ACTIVITY_TYPES[data[:type]]
96
109
  @url = data[:url]
97
110
  @created_at = Time.at(data[:created_at])
98
111
  @timestamps = data[:timestamps] && Timestamps.new(data[:timestamps])
@@ -105,7 +118,7 @@ module Discorb
105
118
  @party = data[:party] && Party.new(data[:party])
106
119
  @assets = data[:assets] && Asset.new(data[:assets])
107
120
  @instance = data[:instance]
108
- @buttons = data[:buttons] && data[:buttons].map { |b| Button.new(b) }
121
+ @buttons = data[:buttons]&.map { |b| Button.new(b) }
109
122
  @flags = data[:flags] && Flag.new(data[:flags])
110
123
  end
111
124
 
@@ -140,13 +153,21 @@ module Discorb
140
153
  # @return [Time] The end time of the activity.
141
154
  attr_reader :end
142
155
 
156
+ #
157
+ # Initialize the timestamps.
143
158
  # @private
159
+ #
160
+ # @param [Hash] data The timestamps data.
161
+ #
144
162
  def initialize(data)
145
163
  @start = data[:start] && Time.at(data[:start])
146
164
  @end = data[:end] && Time.at(data[:end])
147
165
  end
148
166
  end
149
167
 
168
+ #
169
+ # Represents the party of an activity.
170
+ #
150
171
  class Party < DiscordModel
151
172
  # @return [String] The id of the party.
152
173
  attr_reader :id
@@ -156,7 +177,12 @@ module Discorb
156
177
  # @!attribute [r] max_size
157
178
  # @return [Integer] The max size of the party.
158
179
 
180
+ #
181
+ # Initialize the party.
159
182
  # @private
183
+ #
184
+ # @param [Hash] data The party data.
185
+ #
160
186
  def initialize(data)
161
187
  @id = data[:id]
162
188
  @size = data[:size]
@@ -226,7 +252,12 @@ module Discorb
226
252
  # @return [String] The match secret of the activity.
227
253
  attr_reader :match
228
254
 
255
+ #
256
+ # Initialize the secrets.
229
257
  # @private
258
+ #
259
+ # @param [Hash] data The secrets data.
260
+ #
230
261
  def initialize(data)
231
262
  @join = data[:join]
232
263
  @spectate = data[:spectate]
@@ -244,17 +275,17 @@ module Discorb
244
275
  attr_reader :url
245
276
  alias text label
246
277
 
278
+ #
279
+ # Initialize the button.
247
280
  # @private
281
+ #
282
+ # @param [Hash] data The button data.
283
+ #
248
284
  def initialize(data)
249
285
  @label = data[0]
250
286
  @url = data[1]
251
287
  end
252
288
  end
253
-
254
- class << self
255
- # @private
256
- attr_reader :activity_types
257
- end
258
289
  end
259
290
 
260
291
  #
@@ -275,7 +306,12 @@ module Discorb
275
306
  # @!attribute [r] web?
276
307
  # @return [Boolean] Whether the user is not offline on web.
277
308
 
309
+ #
310
+ # Initialize the client status.
278
311
  # @private
312
+ #
313
+ # @param [Hash] data The client status data.
314
+ #
279
315
  def initialize(data)
280
316
  @desktop = data[:desktop]&.to_sym || :offline
281
317
  @mobile = data[:mobile]&.to_sym || :offline
@@ -6,7 +6,12 @@ module Discorb
6
6
  # @private
7
7
  #
8
8
  class RatelimitHandler
9
+ #
10
+ # Initialize a rate limit handler.
9
11
  # @private
12
+ #
13
+ # @param [Discorb::Client] client The client.
14
+ #
10
15
  def initialize(client)
11
16
  @client = client
12
17
  @path_ratelimit_bucket = {}
@@ -40,7 +45,7 @@ module Discorb
40
45
  @path_ratelimit_bucket.delete(path.identifier + path.major_param)
41
46
  return
42
47
  end
43
- return if bucket[:remaining] > 0
48
+ return if (bucket[:remaining]).positive?
44
49
 
45
50
  time = bucket[:reset_at] - Time.now.to_f
46
51
  @client.log.info("rate limit for #{path.identifier} with #{path.major_param} reached, waiting #{time.round(4)} seconds")
@@ -54,9 +59,7 @@ module Discorb
54
59
  # @param [Net::HTTPResponse] resp The response.
55
60
  #
56
61
  def save(path, resp)
57
- if resp["X-Ratelimit-Global"] == "true"
58
- @global = Time.now.to_f + JSON.parse(resp.body, symbolize_names: true)[:retry_after]
59
- end
62
+ @global = Time.now.to_f + JSON.parse(resp.body, symbolize_names: true)[:retry_after] if resp["X-Ratelimit-Global"] == "true"
60
63
  return unless resp["X-RateLimit-Remaining"]
61
64
  @path_ratelimit_hash[path.identifier] = resp["X-Ratelimit-Bucket"]
62
65
  @path_ratelimit_bucket[resp["X-Ratelimit-Bucket"] + path.major_param] = {
@@ -16,7 +16,13 @@ module Discorb
16
16
  alias me? me
17
17
  alias reacted? me
18
18
 
19
+ #
20
+ # Initialize a new reaction.
19
21
  # @private
22
+ #
23
+ # @param [Discorb::Message] message The message that this reaction is on.
24
+ # @param [Hash] data The data of the reaction.
25
+ #
20
26
  def initialize(message, data)
21
27
  @message = message
22
28
  _set_data(data)
data/lib/discorb/role.rb CHANGED
@@ -42,7 +42,14 @@ module Discorb
42
42
 
43
43
  include Comparable
44
44
 
45
+ #
46
+ # Initializes a new role.
45
47
  # @private
48
+ #
49
+ # @param [Discorb::Client] client The client.
50
+ # @param [Discorb::Guild] guild The guild the role belongs to.
51
+ # @param [Hash] data The data of the role.
52
+ #
46
53
  def initialize(client, guild, data)
47
54
  @client = client
48
55
  @guild = guild
@@ -176,7 +183,12 @@ module Discorb
176
183
  # @!attribute [r] integration?
177
184
  # @return [Boolean] Whether the role is an integration role.
178
185
 
186
+ #
187
+ # Initializes a new tag.
179
188
  # @private
189
+ #
190
+ # @param [Hash] data The data of the tag.
191
+ #
180
192
  def initialize(data)
181
193
  @bot_id = Snowflake.new(data[:bot_id])
182
194
  @integration_id = Snowflake.new(data[:integration_id])
@@ -29,16 +29,27 @@ module Discorb
29
29
  attr_reader :available
30
30
  alias available? available
31
31
 
32
- @sticker_type = {
32
+ # @private
33
+ # @return [{Integer => Symbol}] The mapping of sticker types.
34
+ STICKER_TYPE = {
33
35
  1 => :official,
34
36
  2 => :guild,
35
37
  }.freeze
36
- @sticker_format = {
38
+
39
+ # @private
40
+ # @return [{Integer => Symbol}] The mapping of sticker format.
41
+ STICKER_FORMAT = {
37
42
  1 => :png,
38
43
  2 => :apng,
39
44
  3 => :lottie,
40
- }
45
+ }.freeze
46
+ #
47
+ # Initialize a new sticker.
41
48
  # @private
49
+ #
50
+ # @param [Discorb::Client] client The client.
51
+ # @param [Hash] data The sticker data.
52
+ #
42
53
  def initialize(client, data)
43
54
  @client = client
44
55
  _set_data(data)
@@ -51,15 +62,6 @@ module Discorb
51
62
  # @!attribute [r] guild
52
63
  # @macro client_cache
53
64
  # @return [Discorb::Guild] The guild the sticker is in.
54
- @sticker_type = {
55
- 1 => :official,
56
- 2 => :guild,
57
- }.freeze
58
- @sticker_format = {
59
- 1 => :png,
60
- 2 => :apng,
61
- 3 => :lottie,
62
- }
63
65
 
64
66
  def guild
65
67
  @client.guilds[@guild_id]
@@ -121,7 +123,13 @@ module Discorb
121
123
  # @return [Discorb::Asset] The banner of the pack.
122
124
  attr_reader :banner
123
125
 
126
+ #
127
+ # Initialize a new sticker pack.
124
128
  # @private
129
+ #
130
+ # @param [Discorb::Client] client The client.
131
+ # @param [Hash] data The sticker pack data.
132
+ #
125
133
  def initialize(client, data)
126
134
  @client = client
127
135
  @id = Snowflake.new(data[:id])
@@ -141,8 +149,8 @@ module Discorb
141
149
  @id = Snowflake.new(data[:id])
142
150
  @name = data[:name]
143
151
  @tags = data[:tags].split(",")
144
- @type = self.class.sticker_type[data[:type]]
145
- @format = self.class.sticker_format[data[:format]]
152
+ @type = STICKER_TYPE[data[:type]]
153
+ @format = STICKER_FORMAT[data[:format]]
146
154
  @description = data[:description]
147
155
  @available = data[:available]
148
156
  if @type == :official
data/lib/discorb/user.rb CHANGED
@@ -29,7 +29,13 @@ module Discorb
29
29
  # @!attribute [r] mention
30
30
  # @return [String] The user's mention.
31
31
 
32
+ #
33
+ # Initializes a new user.
32
34
  # @private
35
+ #
36
+ # @param [Discorb::Client] client The client.
37
+ # @param [Hash] data The user data.
38
+ #
33
39
  def initialize(client, data)
34
40
  @client = client
35
41
  @data = {}
@@ -79,7 +85,12 @@ module Discorb
79
85
 
80
86
  alias app_owner? bot_owner?
81
87
 
88
+ #
89
+ # Returns the dm channel id of the user.
82
90
  # @private
91
+ #
92
+ # @return [Async::Task<Discorb::Snowflake>] A task that resolves to the channel id.
93
+ #
83
94
  def channel_id
84
95
  Async do
85
96
  next @dm_channel_id if @dm_channel_id
@@ -137,7 +148,7 @@ module Discorb
137
148
  @avatar = data[:avatar] ? Asset.new(self, data[:avatar]) : DefaultAvatar.new(data[:discriminator])
138
149
  @bot = data[:bot]
139
150
  @raw_data = data
140
- @client.users[@id] = self if !data[:no_cache]
151
+ @client.users[@id] = self unless data[:no_cache]
141
152
  @created_at = @id.timestamp
142
153
  @data.update(data)
143
154
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # @private
2
3
  def sputs(text)
3
4
  puts "\e[92m#{text}\e[m"