discorb 0.13.4 → 0.14.0

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 (83) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +2 -0
  3. data/.github/workflows/codeql-analysis.yml +70 -0
  4. data/.github/workflows/lint-push.yml +18 -0
  5. data/.github/workflows/lint.yml +16 -0
  6. data/.rubocop.yml +70 -0
  7. data/Changelog.md +12 -0
  8. data/Gemfile +7 -3
  9. data/Rakefile +22 -22
  10. data/discorb.gemspec +1 -0
  11. data/examples/commands/bookmarker.rb +2 -1
  12. data/examples/commands/hello.rb +1 -0
  13. data/examples/commands/inspect.rb +3 -2
  14. data/examples/components/authorization_button.rb +2 -1
  15. data/examples/components/select_menu.rb +2 -1
  16. data/examples/extension/main.rb +1 -0
  17. data/examples/extension/message_expander.rb +1 -0
  18. data/examples/simple/eval.rb +3 -2
  19. data/examples/simple/ping_pong.rb +1 -0
  20. data/examples/simple/rolepanel.rb +1 -0
  21. data/examples/simple/wait_for_message.rb +4 -3
  22. data/exe/discorb +8 -7
  23. data/lib/discorb/allowed_mentions.rb +64 -0
  24. data/lib/discorb/app_command/command.rb +274 -0
  25. data/lib/discorb/app_command/handler.rb +168 -0
  26. data/lib/discorb/app_command.rb +2 -426
  27. data/lib/discorb/asset.rb +2 -0
  28. data/lib/discorb/audit_logs.rb +3 -3
  29. data/lib/discorb/channel.rb +19 -4
  30. data/lib/discorb/client.rb +30 -27
  31. data/lib/discorb/common.rb +4 -26
  32. data/lib/discorb/components/button.rb +106 -0
  33. data/lib/discorb/components/select_menu.rb +157 -0
  34. data/lib/discorb/components/text_input.rb +96 -0
  35. data/lib/discorb/components.rb +11 -276
  36. data/lib/discorb/dictionary.rb +3 -0
  37. data/lib/discorb/embed.rb +2 -2
  38. data/lib/discorb/emoji.rb +19 -3
  39. data/lib/discorb/emoji_table.rb +1 -1
  40. data/lib/discorb/error.rb +4 -6
  41. data/lib/discorb/event.rb +9 -7
  42. data/lib/discorb/exe/about.rb +1 -0
  43. data/lib/discorb/exe/irb.rb +4 -3
  44. data/lib/discorb/exe/new.rb +6 -7
  45. data/lib/discorb/exe/run.rb +2 -1
  46. data/lib/discorb/exe/setup.rb +8 -5
  47. data/lib/discorb/exe/show.rb +1 -0
  48. data/lib/discorb/extend.rb +19 -14
  49. data/lib/discorb/extension.rb +5 -1
  50. data/lib/discorb/gateway.rb +28 -30
  51. data/lib/discorb/guild.rb +11 -13
  52. data/lib/discorb/guild_template.rb +2 -2
  53. data/lib/discorb/http.rb +15 -17
  54. data/lib/discorb/integration.rb +1 -1
  55. data/lib/discorb/intents.rb +1 -1
  56. data/lib/discorb/interaction/autocomplete.rb +4 -3
  57. data/lib/discorb/interaction/command.rb +34 -9
  58. data/lib/discorb/interaction/components.rb +5 -2
  59. data/lib/discorb/interaction/modal.rb +33 -0
  60. data/lib/discorb/interaction/response.rb +33 -4
  61. data/lib/discorb/interaction/root.rb +1 -0
  62. data/lib/discorb/interaction.rb +2 -1
  63. data/lib/discorb/log.rb +1 -1
  64. data/lib/discorb/member.rb +1 -3
  65. data/lib/discorb/message.rb +26 -277
  66. data/lib/discorb/message_meta.rb +205 -0
  67. data/lib/discorb/modules.rb +1 -1
  68. data/lib/discorb/permission.rb +2 -2
  69. data/lib/discorb/presence.rb +4 -1
  70. data/lib/discorb/rate_limit.rb +2 -4
  71. data/lib/discorb/user.rb +1 -1
  72. data/lib/discorb/utils/colored_puts.rb +1 -0
  73. data/lib/discorb/voice_state.rb +3 -0
  74. data/lib/discorb/webhook.rb +1 -1
  75. data/lib/discorb.rb +1 -0
  76. data/template-replace/scripts/arrow.rb +1 -0
  77. data/template-replace/scripts/favicon.rb +1 -0
  78. data/template-replace/scripts/index.rb +2 -1
  79. data/template-replace/scripts/locale_ja.rb +5 -4
  80. data/template-replace/scripts/sidebar.rb +1 -0
  81. data/template-replace/scripts/version.rb +7 -10
  82. data/template-replace/scripts/yard_replace.rb +5 -4
  83. metadata +16 -2
@@ -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
  #
@@ -257,38 +195,44 @@ module Discorb
257
195
  #
258
196
  def clean_content(user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false)
259
197
  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|
198
+ if user
199
+ ret.gsub!(/<@!?(\d+)>/) do |_match|
200
+ member = guild&.members&.[]($1)
201
+ member ||= @client.users[$1]
202
+ member ? "@#{member.name}" : "@Unknown User"
203
+ end
204
+ end
205
+ ret.gsub!(/<#(\d+)>/) do |_match|
266
206
  channel = @client.channels[$1]
267
207
  channel ? "<##{channel.id}>" : "#Unknown Channel"
268
208
  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
209
+ if role
210
+ ret.gsub!(/<@&(\d+)>/) do |_match|
211
+ role = guild&.roles&.[]($1)
212
+ role ? "@#{role.name}" : "@Unknown Role"
213
+ end
214
+ end
215
+ if emoji
216
+ ret.gsub!(/<a?:([a-zA-Z0-9_]+):\d+>/) do |_match|
217
+ $1
218
+ end
219
+ end
276
220
  ret.gsub!(/@(everyone|here)/, "@\u200b\\1") if everyone
277
- unless codeblock
221
+ if codeblock
222
+ ret
223
+ else
278
224
  codeblocks = ret.split("```", -1)
279
225
  original_codeblocks = @content.scan(/```(.+?)```/m)
280
226
  res = []
281
227
  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
228
+ codeblocks.each_with_index do |single_codeblock, i|
229
+ res << if max.even? && i == max - 1 || i.even?
230
+ single_codeblock
285
231
  else
286
- res << original_codeblocks[i / 2]
232
+ original_codeblocks[i / 2]
287
233
  end
288
234
  end
289
235
  res.join("```")
290
- else
291
- ret
292
236
  end
293
237
  end
294
238
 
@@ -501,201 +445,6 @@ module Discorb
501
445
  "#<#{self.class} #{@content.inspect} id=#{@id}>"
502
446
  end
503
447
 
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
- private
601
-
602
- def _set_data(data)
603
- @id = Snowflake.new(data[:id])
604
- @channel_id = data[:channel_id]
605
-
606
- if data[:guild_id]
607
- @guild_id = data[:guild_id]
608
- @dm = nil
609
- else
610
- @dm = Discorb::DMChannel.new(@client, data[:channel_id])
611
- @guild_id = nil
612
- end
613
-
614
- if data[:member].nil? && data[:webhook_id]
615
- @webhook_id = Snowflake.new(data[:webhook_id])
616
- @author = Webhook::Message::Author.new(data[:author])
617
- elsif data[:guild_id].nil? || data[:guild_id].empty? || data[:member].nil?
618
- @author = @client.users[data[:author][:id]] || User.new(@client, data[:author])
619
- else
620
- @author = guild&.members&.get(data[:author][:id]) || Member.new(@client,
621
- @guild_id, data[:author], data[:member])
622
- end
623
- @content = data[:content]
624
- @created_at = Time.iso8601(data[:timestamp])
625
- @updated_at = data[:edited_timestamp].nil? ? nil : Time.iso8601(data[:edited_timestamp])
626
-
627
- @tts = data[:tts]
628
- @mention_everyone = data[:mention_everyone]
629
- @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) } : []
632
- @reactions = data[:reactions] ? data[:reactions].map { |r| Reaction.new(self, r) } : []
633
- @pinned = data[:pinned]
634
- @type = self.class.message_type[data[:type]]
635
- @activity = data[:activity] && Activity.new(data[:activity])
636
- @application_id = data[:application_id]
637
- @message_reference = data[:message_reference] && Reference.from_hash(data[:message_reference])
638
- @flag = Flag.new(0b111 - data[:flags])
639
- @sticker_items = data[:sticker_items] ? data[:sticker_items].map { |s| Message::Sticker.new(s) } : []
640
- # @referenced_message = data[:referenced_message] && Message.new(@client, data[:referenced_message])
641
- @interaction = data[:interaction] && Message::Interaction.new(@client, data[:interaction])
642
- @thread = data[:thread] && Channel.make_channel(@client, data[:thread])
643
- @components = data[:components].map { |c| c[:components].map { |co| Component.from_hash(co) } }
644
- @data.update(data)
645
- @deleted = false
646
- end
647
-
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
448
  class << self
700
449
  # @private
701
450
  attr_reader :message_type
@@ -0,0 +1,205 @@
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
+ end
92
+
93
+ #
94
+ # Represents a sticker.
95
+ #
96
+ class Sticker
97
+ attr_reader :id, :name, :format
98
+
99
+ def initialize(data)
100
+ @id = Snowflake.new(data[:id])
101
+ @name = data[:name]
102
+ @format = Discorb::Sticker.sticker_format[data[:format]]
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def _set_data(data)
109
+ @id = Snowflake.new(data[:id])
110
+ @channel_id = data[:channel_id]
111
+
112
+ if data[:guild_id]
113
+ @guild_id = data[:guild_id]
114
+ @dm = nil
115
+ else
116
+ @dm = Discorb::DMChannel.new(@client, data[:channel_id])
117
+ @guild_id = nil
118
+ end
119
+
120
+ if data[:member].nil? && data[:webhook_id]
121
+ @webhook_id = Snowflake.new(data[:webhook_id])
122
+ @author = Webhook::Message::Author.new(data[:author])
123
+ elsif data[:guild_id].nil? || data[:guild_id].empty? || data[:member].nil?
124
+ @author = @client.users[data[:author][:id]] || User.new(@client, data[:author])
125
+ else
126
+ @author = guild&.members&.get(data[:author][:id]) || Member.new(@client,
127
+ @guild_id, data[:author], data[:member])
128
+ end
129
+ @content = data[:content]
130
+ @created_at = Time.iso8601(data[:timestamp])
131
+ @updated_at = data[:edited_timestamp].nil? ? nil : Time.iso8601(data[:edited_timestamp])
132
+
133
+ @tts = data[:tts]
134
+ @mention_everyone = data[:mention_everyone]
135
+ @mention_roles = data[:mention_roles].map { |r| guild.roles[r] }
136
+ @attachments = data[:attachments].map { |a| Attachment.new(a) }
137
+ @embeds = data[:embeds] ? data[:embeds].map { |e| Embed.new(data: e) } : []
138
+ @reactions = data[:reactions] ? data[:reactions].map { |r| Reaction.new(self, r) } : []
139
+ @pinned = data[:pinned]
140
+ @type = self.class.message_type[data[:type]]
141
+ @activity = data[:activity] && Activity.new(data[:activity])
142
+ @application_id = data[:application_id]
143
+ @message_reference = data[:message_reference] && Reference.from_hash(data[:message_reference])
144
+ @flag = Flag.new(0b111 - data[:flags])
145
+ @sticker_items = data[:sticker_items] ? data[:sticker_items].map { |s| Message::Sticker.new(s) } : []
146
+ # @referenced_message = data[:referenced_message] && Message.new(@client, data[:referenced_message])
147
+ @interaction = data[:interaction] && Message::Interaction.new(@client, data[:interaction])
148
+ @thread = data[:thread] && Channel.make_channel(@client, data[:thread])
149
+ @components = data[:components].map { |c| c[:components].map { |co| Component.from_hash(co) } }
150
+ @data.update(data)
151
+ @deleted = false
152
+ end
153
+
154
+ #
155
+ # Represents a interaction of message.
156
+ #
157
+ class Interaction < DiscordModel
158
+ # @return [Discorb::Snowflake] The user ID.
159
+ attr_reader :id
160
+ # @return [String] The name of command.
161
+ # @return [nil] If the message is not a command.
162
+ attr_reader :name
163
+ # @return [Class] The type of interaction.
164
+ attr_reader :type
165
+ # @return [Discorb::User] The user.
166
+ attr_reader :user
167
+
168
+ # @private
169
+ def initialize(client, data)
170
+ @id = Snowflake.new(data[:id])
171
+ @name = data[:name]
172
+ @type = Discorb::Interaction.descendants.find { |c| c.interaction_type == data[:type] }
173
+ @user = client.users[data[:user][:id]] || User.new(client, data[:user])
174
+ end
175
+ end
176
+
177
+ #
178
+ # Represents a activity of message.
179
+ #
180
+ class Activity < DiscordModel
181
+ # @return [String] The name of activity.
182
+ attr_reader :name
183
+ # @return [Symbol] The type of activity.
184
+ attr_reader :type
185
+
186
+ @type = {
187
+ 1 => :join,
188
+ 2 => :spectate,
189
+ 3 => :listen,
190
+ 5 => :join_request,
191
+ }
192
+
193
+ # @private
194
+ def initialize(data)
195
+ @name = data[:name]
196
+ @type = self.class.type(data[:type])
197
+ end
198
+
199
+ class << self
200
+ # @private
201
+ attr_reader :type
202
+ end
203
+ end
204
+ end
205
+ end
@@ -204,7 +204,7 @@ module Discorb
204
204
  post_task.stop
205
205
  end
206
206
  else
207
- Async do |task|
207
+ Async do |_task|
208
208
  @client.http.request(Route.new("/channels/#{@id}/typing", "//channels/:channel_id/typing", :post), {})
209
209
  end
210
210
  end
@@ -174,13 +174,13 @@ module Discorb
174
174
  # @return [Hash] The permission overwrite as a hash.
175
175
  #
176
176
  def to_hash
177
- self.class.bits.keys.map do |field|
177
+ self.class.bits.keys.to_h do |field|
178
178
  [field, if @allow & self.class.bits[field] != 0
179
179
  true
180
180
  elsif @deny & self.class.bits[method] != 0
181
181
  false
182
182
  end]
183
- end.to_h
183
+ end
184
184
  end
185
185
 
186
186
  #
@@ -105,7 +105,7 @@ module Discorb
105
105
  @party = data[:party] && Party.new(data[:party])
106
106
  @assets = data[:assets] && Asset.new(data[:assets])
107
107
  @instance = data[:instance]
108
- @buttons = data[:buttons] && data[:buttons].map { |b| Button.new(b) }
108
+ @buttons = data[:buttons]&.map { |b| Button.new(b) }
109
109
  @flags = data[:flags] && Flag.new(data[:flags])
110
110
  end
111
111
 
@@ -147,6 +147,9 @@ module Discorb
147
147
  end
148
148
  end
149
149
 
150
+ #
151
+ # Represents the party of an activity.
152
+ #
150
153
  class Party < DiscordModel
151
154
  # @return [String] The id of the party.
152
155
  attr_reader :id
@@ -40,7 +40,7 @@ module Discorb
40
40
  @path_ratelimit_bucket.delete(path.identifier + path.major_param)
41
41
  return
42
42
  end
43
- return if bucket[:remaining] > 0
43
+ return if (bucket[:remaining]).positive?
44
44
 
45
45
  time = bucket[:reset_at] - Time.now.to_f
46
46
  @client.log.info("rate limit for #{path.identifier} with #{path.major_param} reached, waiting #{time.round(4)} seconds")
@@ -54,9 +54,7 @@ module Discorb
54
54
  # @param [Net::HTTPResponse] resp The response.
55
55
  #
56
56
  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
57
+ @global = Time.now.to_f + JSON.parse(resp.body, symbolize_names: true)[:retry_after] if resp["X-Ratelimit-Global"] == "true"
60
58
  return unless resp["X-RateLimit-Remaining"]
61
59
  @path_ratelimit_hash[path.identifier] = resp["X-Ratelimit-Bucket"]
62
60
  @path_ratelimit_bucket[resp["X-Ratelimit-Bucket"] + path.major_param] = {
data/lib/discorb/user.rb CHANGED
@@ -137,7 +137,7 @@ module Discorb
137
137
  @avatar = data[:avatar] ? Asset.new(self, data[:avatar]) : DefaultAvatar.new(data[:discriminator])
138
138
  @bot = data[:bot]
139
139
  @raw_data = data
140
- @client.users[@id] = self if !data[:no_cache]
140
+ @client.users[@id] = self unless data[:no_cache]
141
141
  @created_at = @id.timestamp
142
142
  @data.update(data)
143
143
  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"
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Discorb
4
+ #
5
+ # Represents a state of user in voice channel.
6
+ #
4
7
  class VoiceState < DiscordModel
5
8
  # @return [Discorb::Member] The member associated with this voice state.
6
9
  attr_reader :member
@@ -151,7 +151,7 @@ module Discorb
151
151
  payload[:attachments] = attachments.map(&:to_hash) if attachments != Discorb::Unset
152
152
  payload[:allowed_mentions] = allowed_mentions if allowed_mentions != Discorb::Unset
153
153
  files = [file] if file != Discorb::Unset
154
- _resp, data = @http.multipart_request(Route.new("#{url}/messages/#{Utils.try(message, :id)}", "//webhooks/:webhook_id/:token/messages/:message_id", :patch), payload, headers: headers).wait
154
+ _resp, data = @http.multipart_request(Route.new("#{url}/messages/#{Utils.try(message, :id)}", "//webhooks/:webhook_id/:token/messages/:message_id", :patch), payload, files).wait
155
155
  message.send(:_set_data, data)
156
156
  message
157
157
  end
data/lib/discorb.rb CHANGED
@@ -40,6 +40,7 @@ module Discorb
40
40
  end
41
41
 
42
42
  require_order = %w[common flag dictionary error rate_limit http intents emoji_table modules] +
43
+ %w[message_meta allowed_mentions] +
43
44
  %w[user member guild emoji channel embed message] +
44
45
  %w[application audit_logs color components event event_handler] +
45
46
  %w[file guild_template image integration interaction invite log permission] +
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  def replace_arrow(dir)
2
3
  Dir.glob("#{dir}/**/*.html") do |file|
3
4
  content = File.read(file)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "fileutils"
2
3
 
3
4
  def add_favicon(dir)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "fileutils"
2
3
 
3
4
  def replace_index(dir, version)
@@ -5,7 +6,7 @@ def replace_index(dir, version)
5
6
  next if (m = file.match(/[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+)?/)) && m[0] != version
6
7
 
7
8
  content = File.read(file)
8
- content.gsub!(/(?<=["\/])_index.html/, "a_index.html")
9
+ content.gsub!(%r{(?<=["/])_index.html}, "a_index.html")
9
10
  File.write(file, content)
10
11
  end
11
12