discorb 0.13.4 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,9 +1,13 @@
1
+ # frozen_string_literal: true
1
2
  module Discorb
3
+ #
4
+ # Represents an interaction of Discord.
5
+ #
2
6
  class Interaction
3
7
  #
4
8
  # A module for response with source.
5
9
  #
6
- module SourceResponse
10
+ module SourceResponder
7
11
  #
8
12
  # Response with `DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE`(`5`).
9
13
  #
@@ -38,14 +42,14 @@ module Discorb
38
42
  # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
39
43
  # @param [Boolean] ephemeral Whether to make the response ephemeral.
40
44
  #
41
- # @return [Discorb::Interaction::SourceResponse::CallbackMessage, Discorb::Webhook::Message] The callback message.
45
+ # @return [Discorb::Interaction::SourceResponder::CallbackMessage, Discorb::Webhook::Message] The callback message.
42
46
  #
43
47
  def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, ephemeral: false)
44
48
  Async do
45
49
  payload = {}
46
50
  payload[:content] = content if content
47
51
  payload[:tts] = tts
48
- payload[:embeds] = (embeds || [embed])&.map { |e| e&.to_hash }.filter { _1 }
52
+ payload[:embeds] = (embeds || [embed]).map { |e| e&.to_hash }.filter { _1 }
49
53
  payload[:allowed_mentions] = allowed_mentions&.to_hash(@client.allowed_mentions) || @client.allowed_mentions.to_hash
50
54
  payload[:components] = Component.to_payload(components) if components
51
55
  payload[:flags] = (ephemeral ? 1 << 6 : 0)
@@ -66,8 +70,19 @@ module Discorb
66
70
  end
67
71
  end
68
72
 
73
+ #
74
+ # Represents of a callback message of interaction.
75
+ #
69
76
  class CallbackMessage
77
+ #
78
+ # Initializes a new instance of CallbackMessage.
70
79
  # @private
80
+ #
81
+ # @param [Client] client The client.
82
+ # @param [Hash] data The payload.
83
+ # @param [String] application_id The application ID.
84
+ # @param [String] token The token.
85
+ #
71
86
  def initialize(client, data, application_id, token)
72
87
  @client = client
73
88
  @data = data
@@ -121,13 +136,17 @@ module Discorb
121
136
  @client.http.request(Route.new("/webhooks/#{@application_id}/#{@token}/messages/@original", "//webhooks/:webhook_id/:token/messages/@original", :delete)).wait
122
137
  end
123
138
  end
139
+
140
+ def inspect
141
+ "#<#{self.class.name} application_id=#{@application_id}"
142
+ end
124
143
  end
125
144
  end
126
145
 
127
146
  #
128
147
  # A module for response with update.
129
148
  #
130
- module UpdateResponse
149
+ module UpdateResponder
131
150
  #
132
151
  # Response with `DEFERRED_UPDATE_MESSAGE`(`6`).
133
152
  # @async
@@ -181,6 +200,28 @@ module Discorb
181
200
  end
182
201
  end
183
202
 
203
+ #
204
+ # A module for response with modal.
205
+ #
206
+ module ModalResponder
207
+ #
208
+ # Response with `MODAL`(`9`).
209
+ #
210
+ # @param [String] title The title of the modal.
211
+ # @param [String] custom_id The custom id of the modal.
212
+ # @param [Array<Discorb::TextInput>] components The text inputs to send.
213
+ #
214
+ # @return [Async::Task<void>] The task.
215
+ #
216
+ def show_modal(title, custom_id, components)
217
+ payload = { title: title, custom_id: custom_id, components: Component.to_payload(components) }
218
+ @client.http.request(
219
+ Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback", :post),
220
+ { type: 9, data: payload }
221
+ ).wait
222
+ end
223
+ end
224
+
184
225
  private
185
226
 
186
227
  def _set_data(*)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Discorb
2
3
  #
3
4
  # Represents a user interaction with the bot.
@@ -37,7 +38,13 @@ module Discorb
37
38
  @interaction_type = nil
38
39
  @interaction_name = nil
39
40
 
41
+ #
42
+ # Initialize a new interaction.
40
43
  # @private
44
+ #
45
+ # @param [Discorb::Client] client The client this interaction belongs to.
46
+ # @param [Hash] data The data of the interaction.
47
+ #
41
48
  def initialize(client, data)
42
49
  @client = client
43
50
  @id = Snowflake.new(data[:id])
@@ -80,7 +87,13 @@ module Discorb
80
87
  # @private
81
88
  attr_reader :interaction_type, :interaction_name, :event_name
82
89
 
90
+ #
91
+ # Create a new Interaction instance from the data.
83
92
  # @private
93
+ #
94
+ # @param [Discorb::Client] client The client this interaction belongs to.
95
+ # @param [Hash] data The data of the interaction.
96
+ #
84
97
  def make_interaction(client, data)
85
98
  interaction = nil
86
99
  descendants.each do |klass|
@@ -93,7 +106,10 @@ module Discorb
93
106
  interaction
94
107
  end
95
108
 
109
+ #
110
+ # Returns the descendants of the class.
96
111
  # @private
112
+ #
97
113
  def descendants
98
114
  ObjectSpace.each_object(Class).select { |klass| klass < self }
99
115
  end
@@ -1,3 +1,4 @@
1
- %w[root response command components autocomplete].each do |file|
1
+ # frozen_string_literal: true
2
+ %w[root response command components autocomplete modal].each do |file|
2
3
  require_relative "interaction/#{file}.rb"
3
4
  end
@@ -66,13 +66,22 @@ module Discorb
66
66
  # Whether the invite is temporary.
67
67
  # @return [Boolean]
68
68
 
69
- @target_types = {
69
+ # @private
70
+ # @return [{Integer => Symbol}] The mapping of target types.
71
+ TARGET_TYPES = {
70
72
  nil => :voice,
71
73
  1 => :stream,
72
74
  2 => :guild,
73
75
  }.freeze
74
76
 
77
+ #
78
+ # Initialize a new invite.
75
79
  # @private
80
+ #
81
+ # @param [Discorb::Client] client The client.
82
+ # @param [Hash] data The data of invite.
83
+ # @param [Boolean] gateway Whether the data is from gateway.
84
+ #
76
85
  def initialize(client, data, gateway)
77
86
  @client = client
78
87
  @data = data[:data]
@@ -124,7 +133,7 @@ module Discorb
124
133
  @expires_at = data[:expires_at] && Time.iso8601(data[:expires_at])
125
134
  end
126
135
  @inviter_data = data[:inviter]
127
- @target_type = self.class.target_types[data[:target_type]]
136
+ @target_type = TARGET_TYPES[data[:target_type]]
128
137
  @target_user = @client.users[data[:target_user][:id]] || User.new(@client, data[:target_user]) if data[:target_user]
129
138
  # @target_application = nil
130
139
 
@@ -137,10 +146,5 @@ module Discorb
137
146
  @temporary = data[:temporary]
138
147
  @created_at = Time.iso8601(data[:created_at])
139
148
  end
140
-
141
- class << self
142
- # @private
143
- attr_reader :target_types
144
- end
145
149
  end
146
150
  end
data/lib/discorb/log.rb CHANGED
@@ -5,11 +5,11 @@ module Discorb
5
5
  class Logger
6
6
  attr_accessor :out, :colorize_log
7
7
 
8
- @levels = %i[debug info warn error fatal].freeze
8
+ LEVELS = %i[debug info warn error fatal].freeze
9
9
 
10
10
  def initialize(out, colorize_log, level)
11
11
  @out = out
12
- @level = self.class.levels.index(level)
12
+ @level = LEVELS.index(level)
13
13
  @colorize_log = colorize_log
14
14
  end
15
15
 
@@ -18,11 +18,11 @@ module Discorb
18
18
  end
19
19
 
20
20
  def level
21
- self.class.levels[@level]
21
+ LEVELS[@level]
22
22
  end
23
23
 
24
24
  def level=(level)
25
- @level = self.class.levels.index(level)
25
+ @level = LEVELS.index(level)
26
26
  raise ArgumentError, "Invalid log level: #{level}" unless @level
27
27
  end
28
28
 
@@ -64,7 +64,7 @@ module Discorb
64
64
 
65
65
  def write_output(name, color, message, fallback)
66
66
  unless @out
67
- fallback.puts(message) if fallback
67
+ fallback&.puts(message)
68
68
 
69
69
  return
70
70
  end
@@ -62,7 +62,15 @@ module Discorb
62
62
  # @!attribute [r] owner?
63
63
  # @return [Boolean] Whether the member is the owner of the guild.
64
64
 
65
+ #
66
+ # Initialize a new instance of the member.
65
67
  # @private
68
+ #
69
+ # @param [Discorb::Client] client The client.
70
+ # @param [Discorb::Snowflake] guild_id The ID of the guild.
71
+ # @param [Hash] user_data The data of the user.
72
+ # @param [Hash] member_data The data of the member.
73
+ #
66
74
  def initialize(client, guild_id, user_data, member_data)
67
75
  @guild_id = guild_id
68
76
  @client = client
@@ -105,9 +113,7 @@ module Discorb
105
113
  end
106
114
 
107
115
  def permissions
108
- if owner?
109
- return Permission.new((1 << 38) - 1)
110
- end
116
+ return Permission.new((1 << 38) - 1) if owner?
111
117
  roles.map(&:permissions).sum(Permission.new(0))
112
118
  end
113
119
 
@@ -250,6 +256,19 @@ module Discorb
250
256
  end
251
257
  end
252
258
 
259
+ #
260
+ # Checks if the member can manage the given role.
261
+ #
262
+ # @param [Discorb::Role] role The role.
263
+ #
264
+ # @return [Boolean] `true` if the member can manage the role.
265
+ #
266
+ def can_manage?(role)
267
+ return true if owner?
268
+ top_role = roles.max_by(&:position)
269
+ top_role.position > role.position
270
+ end
271
+
253
272
  private
254
273
 
255
274
  def _set_data(user_data, member_data)
@@ -1,68 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Discorb
4
- #
5
- # Represents a allowed mentions in a message.
6
- #
7
- class AllowedMentions
8
- # @return [Boolean] Whether to allow @everyone or @here.
9
- attr_accessor :everyone
10
- # @return [Boolean, Array<Discorb::Role>] The roles to allow, or false to disable.
11
- attr_accessor :roles
12
- # @return [Boolean, Array<Discorb::User>] The users to allow, or false to disable.
13
- attr_accessor :users
14
- # @return [Boolean] Whether to ping the user that sent the message to reply.
15
- attr_accessor :replied_user
16
-
17
- #
18
- # Initializes a new instance of the AllowedMentions class.
19
- #
20
- # @param [Boolean] everyone Whether to allow @everyone or @here.
21
- # @param [Boolean, Array<Discorb::Role>] roles The roles to allow, or false to disable.
22
- # @param [Boolean, Array<Discorb::User>] users The users to allow, or false to disable.
23
- # @param [Boolean] replied_user Whether to ping the user that sent the message to reply.
24
- #
25
- def initialize(everyone: nil, roles: nil, users: nil, replied_user: nil)
26
- @everyone = everyone
27
- @roles = roles
28
- @users = users
29
- @replied_user = replied_user
30
- end
31
-
32
- def inspect
33
- "#<#{self.class} @everyone=#{@everyone} @roles=#{@roles} @users=#{@users} @replied_user=#{@replied_user}>"
34
- end
35
-
36
- # @private
37
- def to_hash(other = nil)
38
- payload = {
39
- parse: %w[everyone roles users],
40
- }
41
- replied_user = nil_merge(@replied_user, other&.replied_user)
42
- everyone = nil_merge(@everyone, other&.everyone)
43
- roles = nil_merge(@roles, other&.roles)
44
- users = nil_merge(@users, other&.users)
45
- payload[:replied_user] = replied_user
46
- payload[:parse].delete("everyone") if everyone == false
47
- if (roles == false) || roles.is_a?(Array)
48
- payload[:roles] = roles.map { |u| u.id.to_s } if roles.is_a? Array
49
- payload[:parse].delete("roles")
50
- end
51
- if (users == false) || users.is_a?(Array)
52
- payload[:users] = users.map { |u| u.id.to_s } if users.is_a? Array
53
- payload[:parse].delete("users")
54
- end
55
- payload
56
- end
57
-
58
- def nil_merge(*args)
59
- args.each do |a|
60
- return a unless a.nil?
61
- end
62
- nil
63
- end
64
- end
65
-
66
4
  #
67
5
  # Represents a message.
68
6
  #
@@ -214,7 +152,14 @@ module Discorb
214
152
  !@guild_id.nil?
215
153
  end
216
154
 
155
+ #
156
+ # Initialize a new message.
217
157
  # @private
158
+ #
159
+ # @param [Discorb::Client] client The client.
160
+ # @param [Hash] data The data of the welcome screen.
161
+ # @param [Boolean] no_cache Whether to disable caching.
162
+ #
218
163
  def initialize(client, data, no_cache: false)
219
164
  @client = client
220
165
  @data = {}
@@ -257,38 +202,44 @@ module Discorb
257
202
  #
258
203
  def clean_content(user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false)
259
204
  ret = @content.dup
260
- ret.gsub!(/<@!?(\d+)>/) do |match|
261
- member = guild&.members&.[]($1)
262
- member ||= @client.users[$1]
263
- member ? "@#{member.name}" : "@Unknown User"
264
- end if user
265
- ret.gsub!(/<#(\d+)>/) do |match|
205
+ if user
206
+ ret.gsub!(/<@!?(\d+)>/) do |_match|
207
+ member = guild&.members&.[]($1)
208
+ member ||= @client.users[$1]
209
+ member ? "@#{member.name}" : "@Unknown User"
210
+ end
211
+ end
212
+ ret.gsub!(/<#(\d+)>/) do |_match|
266
213
  channel = @client.channels[$1]
267
214
  channel ? "<##{channel.id}>" : "#Unknown Channel"
268
215
  end
269
- ret.gsub!(/<@&(\d+)>/) do |match|
270
- role = guild&.roles&.[]($1)
271
- role ? "@#{role.name}" : "@Unknown Role"
272
- end if role
273
- ret.gsub!(/<a?:([a-zA-Z0-9_]+):\d+>/) do |match|
274
- $1
275
- end if emoji
216
+ if role
217
+ ret.gsub!(/<@&(\d+)>/) do |_match|
218
+ role = guild&.roles&.[]($1)
219
+ role ? "@#{role.name}" : "@Unknown Role"
220
+ end
221
+ end
222
+ if emoji
223
+ ret.gsub!(/<a?:([a-zA-Z0-9_]+):\d+>/) do |_match|
224
+ $1
225
+ end
226
+ end
276
227
  ret.gsub!(/@(everyone|here)/, "@\u200b\\1") if everyone
277
- unless codeblock
228
+ if codeblock
229
+ ret
230
+ else
278
231
  codeblocks = ret.split("```", -1)
279
232
  original_codeblocks = @content.scan(/```(.+?)```/m)
280
233
  res = []
281
234
  max = codeblocks.length
282
- codeblocks.each_with_index do |codeblock, i|
283
- if max % 2 == 0 && i == max - 1 or i.even?
284
- res << codeblock
235
+ codeblocks.each_with_index do |single_codeblock, i|
236
+ res << if max.even? && i == max - 1 || i.even?
237
+ single_codeblock
285
238
  else
286
- res << original_codeblocks[i / 2]
239
+ original_codeblocks[i / 2]
287
240
  end
288
241
  end
289
242
  res.join("```")
290
- else
291
- ret
292
243
  end
293
244
  end
294
245
 
@@ -300,16 +251,17 @@ module Discorb
300
251
  # @param [Discorb::Embed] embed The embed to send.
301
252
  # @param [Array<Discorb::Embed>] embeds The embeds to send.
302
253
  # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions.
254
+ # @param [Array<Discorb::Attachment>] attachments The new attachments.
303
255
  # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
304
256
  # @param [Boolean] supress Whether to supress embeds.
305
257
  #
306
258
  # @return [Async::Task<void>] The task.
307
259
  #
308
- def edit(content = nil, embed: nil, embeds: nil, allowed_mentions: nil,
309
- components: nil, supress: nil)
260
+ def edit(content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset,
261
+ attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset)
310
262
  Async do
311
263
  channel.edit_message(@id, content, embed: embed, embeds: embeds, allowed_mentions: allowed_mentions,
312
- components: components, supress: supress).wait
264
+ attachments: attachments, components: components, supress: supress).wait
313
265
  end
314
266
  end
315
267
 
@@ -501,102 +453,6 @@ module Discorb
501
453
  "#<#{self.class} #{@content.inspect} id=#{@id}>"
502
454
  end
503
455
 
504
- #
505
- # Represents message flag.
506
- # ## Flag fields
507
- # |Field|Value|
508
- # |-|-|
509
- # |`1 << 0`|`:crossposted`|
510
- # |`1 << 1`|`:crosspost`|
511
- # |`1 << 2`|`:supress_embeds`|
512
- # |`1 << 3`|`:source_message_deleted`|
513
- # |`1 << 4`|`:urgent`|
514
- # |`1 << 5`|`:has_thread`|
515
- # |`1 << 6`|`:ephemeral`|
516
- # |`1 << 7`|`:loading`|
517
- #
518
- class Flag < Discorb::Flag
519
- @bits = {
520
- crossposted: 0,
521
- crosspost: 1,
522
- supress_embeds: 2,
523
- source_message_deleted: 3,
524
- urgent: 4,
525
- has_thread: 5,
526
- ephemeral: 6,
527
- loading: 7,
528
- }.freeze
529
- end
530
-
531
- #
532
- # Represents reference of message.
533
- #
534
- class Reference
535
- # @return [Discorb::Snowflake] The guild ID.
536
- attr_accessor :guild_id
537
- # @return [Discorb::Snowflake] The channel ID.
538
- attr_accessor :channel_id
539
- # @return [Discorb::Snowflake] The message ID.
540
- attr_accessor :message_id
541
- # @return [Boolean] Whether fail the request if the message is not found.
542
- attr_accessor :fail_if_not_exists
543
-
544
- alias fail_if_not_exists? fail_if_not_exists
545
-
546
- #
547
- # Initialize a new reference.
548
- #
549
- # @param [Discorb::Snowflake] guild_id The guild ID.
550
- # @param [Discorb::Snowflake] channel_id The channel ID.
551
- # @param [Discorb::Snowflake] message_id The message ID.
552
- # @param [Boolean] fail_if_not_exists Whether fail the request if the message is not found.
553
- #
554
- def initialize(guild_id, channel_id, message_id, fail_if_not_exists: true)
555
- @guild_id = guild_id
556
- @channel_id = channel_id
557
- @message_id = message_id
558
- @fail_if_not_exists = fail_if_not_exists
559
- end
560
-
561
- #
562
- # Convert the reference to a hash.
563
- #
564
- # @return [Hash] The hash.
565
- #
566
- def to_hash
567
- {
568
- message_id: @message_id,
569
- channel_id: @channel_id,
570
- guild_id: @guild_id,
571
- fail_if_not_exists: @fail_if_not_exists,
572
- }
573
- end
574
-
575
- alias to_reference to_hash
576
-
577
- #
578
- # Initialize a new reference from a hash.
579
- #
580
- # @param [Hash] data The hash.
581
- #
582
- # @return [Discorb::Message::Reference] The reference.
583
- # @see https://discord.com/developers/docs/resources/channel#message-reference-object
584
- #
585
- def self.from_hash(data)
586
- new(data[:guild_id], data[:channel_id], data[:message_id], fail_if_not_exists: data[:fail_if_not_exists])
587
- end
588
- end
589
-
590
- class Sticker
591
- attr_reader :id, :name, :format
592
-
593
- def initialize(data)
594
- @id = Snowflake.new(data[:id])
595
- @name = data[:name]
596
- @format = Discorb::Sticker.sticker_format[data[:format]]
597
- end
598
- end
599
-
600
456
  private
601
457
 
602
458
  def _set_data(data)
@@ -627,8 +483,8 @@ module Discorb
627
483
  @tts = data[:tts]
628
484
  @mention_everyone = data[:mention_everyone]
629
485
  @mention_roles = data[:mention_roles].map { |r| guild.roles[r] }
630
- @attachments = data[:attachments].map { |a| Attachment.new(a) }
631
- @embeds = data[:embeds] ? data[:embeds].map { |e| Embed.new(data: e) } : []
486
+ @attachments = data[:attachments].map { |a| Attachment.from_hash(a) }
487
+ @embeds = data[:embeds] ? data[:embeds].map { |e| Embed.from_hash(e) } : []
632
488
  @reactions = data[:reactions] ? data[:reactions].map { |r| Reaction.new(self, r) } : []
633
489
  @pinned = data[:pinned]
634
490
  @type = self.class.message_type[data[:type]]
@@ -645,57 +501,6 @@ module Discorb
645
501
  @deleted = false
646
502
  end
647
503
 
648
- #
649
- # Represents a interaction of message.
650
- #
651
- class Interaction < DiscordModel
652
- # @return [Discorb::Snowflake] The user ID.
653
- attr_reader :id
654
- # @return [String] The name of command.
655
- # @return [nil] If the message is not a command.
656
- attr_reader :name
657
- # @return [Class] The type of interaction.
658
- attr_reader :type
659
- # @return [Discorb::User] The user.
660
- attr_reader :user
661
-
662
- # @private
663
- def initialize(client, data)
664
- @id = Snowflake.new(data[:id])
665
- @name = data[:name]
666
- @type = Discorb::Interaction.descendants.find { |c| c.interaction_type == data[:type] }
667
- @user = client.users[data[:user][:id]] || User.new(client, data[:user])
668
- end
669
- end
670
-
671
- #
672
- # Represents a activity of message.
673
- #
674
- class Activity < DiscordModel
675
- # @return [String] The name of activity.
676
- attr_reader :name
677
- # @return [Symbol] The type of activity.
678
- attr_reader :type
679
-
680
- @type = {
681
- 1 => :join,
682
- 2 => :spectate,
683
- 3 => :listen,
684
- 5 => :join_request,
685
- }
686
-
687
- # @private
688
- def initialize(data)
689
- @name = data[:name]
690
- @type = self.class.type(data[:type])
691
- end
692
-
693
- class << self
694
- # @private
695
- attr_reader :type
696
- end
697
- end
698
-
699
504
  class << self
700
505
  # @private
701
506
  attr_reader :message_type