discorb 0.16.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/build_main.yml +2 -2
  4. data/.github/workflows/build_version.yml +1 -1
  5. data/.github/workflows/codeql-analysis.yml +1 -1
  6. data/.github/workflows/lint-push.yml +3 -5
  7. data/.github/workflows/lint.yml +2 -4
  8. data/.github/workflows/spec.yml +33 -0
  9. data/.github/workflows/validate.yml +21 -0
  10. data/.gitignore +2 -0
  11. data/.lefthook/commit-msg/validator.rb +5 -0
  12. data/.rspec +2 -0
  13. data/.rspec_parallel +2 -0
  14. data/.rubocop.yml +43 -6
  15. data/Changelog.md +30 -1
  16. data/Gemfile +20 -8
  17. data/Rakefile +226 -98
  18. data/Steepfile +28 -0
  19. data/bin/console +3 -3
  20. data/docs/Examples.md +1 -1
  21. data/docs/application_command.md +156 -47
  22. data/docs/cli/irb.md +2 -2
  23. data/docs/cli/new.md +14 -9
  24. data/docs/cli/run.md +7 -11
  25. data/docs/cli.md +17 -10
  26. data/docs/events.md +247 -213
  27. data/docs/extension.md +1 -2
  28. data/docs/faq.md +0 -1
  29. data/docs/tutorial.md +18 -18
  30. data/docs/voice_events.md +106 -106
  31. data/examples/commands/message.rb +68 -0
  32. data/examples/commands/permission.rb +19 -0
  33. data/examples/commands/slash.rb +48 -0
  34. data/examples/commands/user.rb +54 -0
  35. data/examples/components/authorization_button.rb +4 -3
  36. data/examples/components/select_menu.rb +6 -3
  37. data/examples/extension/main.rb +2 -1
  38. data/examples/extension/message_expander.rb +6 -2
  39. data/examples/sig/commands/message.rbs +5 -0
  40. data/examples/simple/eval.rb +3 -2
  41. data/examples/simple/ping_pong.rb +2 -1
  42. data/examples/simple/rolepanel.rb +17 -6
  43. data/examples/simple/shard.rb +3 -2
  44. data/examples/simple/wait_for_message.rb +4 -1
  45. data/exe/discorb +33 -18
  46. data/lefthook.yml +45 -0
  47. data/lib/discorb/allowed_mentions.rb +2 -1
  48. data/lib/discorb/app_command/command.rb +130 -72
  49. data/lib/discorb/app_command/common.rb +25 -0
  50. data/lib/discorb/app_command/handler.rb +130 -33
  51. data/lib/discorb/app_command.rb +2 -1
  52. data/lib/discorb/application.rb +1 -0
  53. data/lib/discorb/asset.rb +1 -2
  54. data/lib/discorb/attachment.rb +1 -1
  55. data/lib/discorb/audit_logs.rb +19 -10
  56. data/lib/discorb/automod.rb +269 -0
  57. data/lib/discorb/channel/base.rb +108 -0
  58. data/lib/discorb/channel/category.rb +32 -0
  59. data/lib/discorb/channel/container.rb +44 -0
  60. data/lib/discorb/channel/dm.rb +28 -0
  61. data/lib/discorb/channel/guild.rb +246 -0
  62. data/lib/discorb/channel/stage.rb +140 -0
  63. data/lib/discorb/channel/text.rb +336 -0
  64. data/lib/discorb/channel/thread.rb +325 -0
  65. data/lib/discorb/channel/voice.rb +79 -0
  66. data/lib/discorb/channel.rb +2 -1165
  67. data/lib/discorb/client.rb +50 -36
  68. data/lib/discorb/color.rb +37 -60
  69. data/lib/discorb/common.rb +2 -1
  70. data/lib/discorb/components/button.rb +2 -1
  71. data/lib/discorb/components/select_menu.rb +4 -2
  72. data/lib/discorb/components/text_input.rb +12 -2
  73. data/lib/discorb/components.rb +1 -1
  74. data/lib/discorb/dictionary.rb +1 -1
  75. data/lib/discorb/embed.rb +26 -10
  76. data/lib/discorb/emoji.rb +31 -4
  77. data/lib/discorb/emoji_table.rb +4969 -3
  78. data/lib/discorb/event.rb +29 -4
  79. data/lib/discorb/exe/about.rb +2 -1
  80. data/lib/discorb/exe/irb.rb +2 -4
  81. data/lib/discorb/exe/new.rb +89 -26
  82. data/lib/discorb/exe/run.rb +8 -22
  83. data/lib/discorb/exe/setup.rb +25 -12
  84. data/lib/discorb/exe/show.rb +4 -3
  85. data/lib/discorb/extend.rb +1 -0
  86. data/lib/discorb/extension.rb +6 -7
  87. data/lib/discorb/flag.rb +13 -2
  88. data/lib/discorb/gateway.rb +79 -589
  89. data/lib/discorb/gateway_events.rb +638 -0
  90. data/lib/discorb/guild.rb +318 -67
  91. data/lib/discorb/guild_template.rb +11 -5
  92. data/lib/discorb/http.rb +53 -24
  93. data/lib/discorb/integration.rb +4 -1
  94. data/lib/discorb/intents.rb +28 -19
  95. data/lib/discorb/interaction/autocomplete.rb +28 -16
  96. data/lib/discorb/interaction/command.rb +42 -14
  97. data/lib/discorb/interaction/components.rb +5 -2
  98. data/lib/discorb/interaction/modal.rb +0 -1
  99. data/lib/discorb/interaction/response.rb +125 -26
  100. data/lib/discorb/interaction/root.rb +13 -13
  101. data/lib/discorb/interaction.rb +1 -0
  102. data/lib/discorb/invite.rb +5 -2
  103. data/lib/discorb/member.rb +28 -8
  104. data/lib/discorb/message.rb +60 -25
  105. data/lib/discorb/message_meta.rb +3 -3
  106. data/lib/discorb/modules.rb +59 -16
  107. data/lib/discorb/presence.rb +2 -0
  108. data/lib/discorb/rate_limit.rb +7 -2
  109. data/lib/discorb/reaction.rb +2 -2
  110. data/lib/discorb/role.rb +20 -5
  111. data/lib/discorb/shard.rb +1 -1
  112. data/lib/discorb/sticker.rb +9 -8
  113. data/lib/discorb/user.rb +4 -3
  114. data/lib/discorb/utils/colored_puts.rb +1 -0
  115. data/lib/discorb/voice_state.rb +6 -2
  116. data/lib/discorb/webhook.rb +64 -31
  117. data/lib/discorb.rb +7 -5
  118. data/po/yard.pot +20 -20
  119. data/rbs_collection.lock.yaml +88 -0
  120. data/rbs_collection.yaml +21 -0
  121. data/sig/async.rbs +11 -0
  122. data/sig/discorb/activity.rbs +23 -0
  123. data/sig/discorb/allowed_mentions.rbs +44 -0
  124. data/sig/discorb/app_command/base.rbs +282 -0
  125. data/sig/discorb/app_command/handler.rbs +171 -0
  126. data/sig/discorb/application.rbs +142 -0
  127. data/sig/discorb/asset.rbs +32 -0
  128. data/sig/discorb/attachment.rbs +91 -0
  129. data/sig/discorb/audit_log.rbs +231 -0
  130. data/sig/discorb/automod.rbs +128 -0
  131. data/sig/discorb/avatar.rbs +26 -0
  132. data/sig/discorb/channel/base.rbs +179 -0
  133. data/sig/discorb/channel/category.rbs +56 -0
  134. data/sig/discorb/channel/container.rbs +29 -0
  135. data/sig/discorb/channel/dm.rbs +14 -0
  136. data/sig/discorb/channel/news.rbs +20 -0
  137. data/sig/discorb/channel/stage.rbs +77 -0
  138. data/sig/discorb/channel/text.rbs +158 -0
  139. data/sig/discorb/channel/thread.rbs +185 -0
  140. data/sig/discorb/channel/voice.rbs +41 -0
  141. data/sig/discorb/client.rbs +2495 -0
  142. data/sig/discorb/color.rbs +142 -0
  143. data/sig/discorb/component/base.rbs +28 -0
  144. data/sig/discorb/component/button.rbs +65 -0
  145. data/sig/discorb/component/select_menu.rbs +107 -0
  146. data/sig/discorb/component/text_input.rbs +69 -0
  147. data/sig/discorb/connectable.rbs +8 -0
  148. data/sig/discorb/custom_emoji.rbs +90 -0
  149. data/sig/discorb/dictionary.rbs +85 -0
  150. data/sig/discorb/discord_model.rbs +15 -0
  151. data/sig/discorb/embed.rbs +279 -0
  152. data/sig/discorb/emoji.rbs +13 -0
  153. data/sig/discorb/error.rbs +73 -0
  154. data/sig/discorb/event_handler.rbs +27 -0
  155. data/sig/discorb/extension.rbs +1734 -0
  156. data/sig/discorb/flag.rbs +72 -0
  157. data/sig/discorb/gateway.rbs +481 -0
  158. data/sig/discorb/guild.rbs +870 -0
  159. data/sig/discorb/guild_template.rbs +174 -0
  160. data/sig/discorb/http.rbs +147 -0
  161. data/sig/discorb/image.rbs +20 -0
  162. data/sig/discorb/integration.rbs +118 -0
  163. data/sig/discorb/intents.rbs +97 -0
  164. data/sig/discorb/interaction/autocomplete.rbs +9 -0
  165. data/sig/discorb/interaction/base.rbs +66 -0
  166. data/sig/discorb/interaction/command.rbs +66 -0
  167. data/sig/discorb/interaction/message_component.rbs +140 -0
  168. data/sig/discorb/interaction/modal.rbs +50 -0
  169. data/sig/discorb/interaction/responder.rbs +157 -0
  170. data/sig/discorb/invite.rbs +86 -0
  171. data/sig/discorb/member.rbs +187 -0
  172. data/sig/discorb/message.rbs +469 -0
  173. data/sig/discorb/messageable.rbs +153 -0
  174. data/sig/discorb/partial_emoji.rbs +35 -0
  175. data/sig/discorb/permissions.rbs +149 -0
  176. data/sig/discorb/presence.rbs +237 -0
  177. data/sig/discorb/reaction.rbs +33 -0
  178. data/sig/discorb/role.rbs +145 -0
  179. data/sig/discorb/scheduled_event.rbs +148 -0
  180. data/sig/discorb/shard.rbs +62 -0
  181. data/sig/discorb/snowflake.rbs +56 -0
  182. data/sig/discorb/stage_instance.rbs +63 -0
  183. data/sig/discorb/sticker.rbs +116 -0
  184. data/sig/discorb/system_channel_flag.rbs +17 -0
  185. data/sig/discorb/unicode_emoji.rbs +49 -0
  186. data/sig/discorb/user.rbs +93 -0
  187. data/sig/discorb/utils.rbs +8 -0
  188. data/sig/discorb/voice_region.rbs +30 -0
  189. data/sig/discorb/voice_state.rbs +71 -0
  190. data/sig/discorb/webhook.rbs +327 -0
  191. data/sig/discorb/welcome_screen.rbs +78 -0
  192. data/sig/discorb.rbs +6 -7230
  193. data/sig/manifest.yaml +3 -0
  194. data/sig/override.rbs +19 -0
  195. data/template-replace/files/css/common.css +4 -0
  196. metadata +102 -6
  197. data/examples/commands/bookmarker.rb +0 -42
  198. data/examples/commands/hello.rb +0 -10
  199. data/examples/commands/inspect.rb +0 -25
@@ -54,7 +54,7 @@ module Discorb
54
54
  attr_reader :shards
55
55
  # @private
56
56
  # @return [Hash{Discorb::Snowflake => Discorb::ApplicationCommand::Command}] The commands on the top level.
57
- attr_reader :bottom_commands
57
+ attr_reader :callable_commands
58
58
  # @private
59
59
  # @return [{String => Thread::Mutex}] A hash of mutexes.
60
60
  attr_reader :mutex
@@ -66,7 +66,7 @@ module Discorb
66
66
  # @return [Discorb::Shard] The current shard. This is implemented with Thread variables.
67
67
  # @return [nil] If client has no shard.
68
68
  # @!attribute [r] shard_id
69
- # @return [Discorb::Shard] The current shard ID. This is implemented with Thread variables.
69
+ # @return [Integer] The current shard ID. This is implemented with Thread variables.
70
70
  # @return [nil] If client has no shard.
71
71
  # @!attribute [r] logger
72
72
  # @return [Logger] The logger.
@@ -81,7 +81,8 @@ module Discorb
81
81
  # @param [:debug, :info, :warn, :error, :critical] log_level The log level.
82
82
  # @param [Boolean] wait_until_ready Whether to delay event dispatch until ready.
83
83
  # @param [Boolean] fetch_member Whether to fetch member on ready. This may slow down the client. Default to `false`.
84
- # @param [String] title The title of the process. `false` to default, `nil` to `discorb: User#0000`. Default to `nil`.
84
+ # @param [String] title
85
+ # The title of the process. `false` to default of ruby, `nil` to `discorb: User#0000`. Default to `nil`.
85
86
  #
86
87
  def initialize(
87
88
  allowed_mentions: nil, intents: nil, message_caches: 1000,
@@ -93,7 +94,11 @@ module Discorb
93
94
  @intents = (intents or Intents.default)
94
95
  @events = {}
95
96
  @api_version = nil
96
- @logger = logger || Logger.new($stdout, progname: "discorb")
97
+ @logger = logger || Logger.new(
98
+ $stdout,
99
+ progname: "discorb",
100
+ level: Logger::ERROR,
101
+ )
97
102
  @user = nil
98
103
  @users = Discorb::Dictionary.new
99
104
  @channels = Discorb::Dictionary.new
@@ -108,7 +113,7 @@ module Discorb
108
113
  @tasks = []
109
114
  @conditions = {}
110
115
  @commands = []
111
- @bottom_commands = []
116
+ @callable_commands = []
112
117
  @status = :initialized
113
118
  @fetch_member = fetch_member
114
119
  @title = title
@@ -206,7 +211,11 @@ module Discorb
206
211
  block.call(*args)
207
212
  logger.debug "Dispatched proc with ID #{block.id.inspect}"
208
213
  rescue StandardError, ScriptError => e
209
- dispatch(:error, event_name, args, e)
214
+ if event_name == :error
215
+ raise e
216
+ else
217
+ dispatch(:error, event_name, args, e)
218
+ end
210
219
  end
211
220
  end
212
221
  end
@@ -276,7 +285,13 @@ module Discorb
276
285
  #
277
286
  def fetch_invite(code, with_count: true, with_expiration: true)
278
287
  Async do
279
- _resp, data = @http.request(Route.new("/invites/#{code}?with_count=#{with_count}&with_expiration=#{with_expiration}", "//invites/:code", :get)).wait
288
+ _resp, data = @http.request(
289
+ Route.new(
290
+ "/invites/#{code}?with_count=#{with_count}&with_expiration=#{with_expiration}",
291
+ "//invites/:code",
292
+ :get
293
+ )
294
+ ).wait
280
295
  Invite.new(self, data, false)
281
296
  end
282
297
  end
@@ -387,6 +402,7 @@ module Discorb
387
402
  case ext
388
403
  when Class
389
404
  raise ArgumentError, "#{ext} is not a extension" unless ext < Discorb::Extension
405
+
390
406
  ins = ext.new(self, ...)
391
407
  when Discorb::Extension
392
408
  ins = ext
@@ -415,13 +431,13 @@ module Discorb
415
431
 
416
432
  cls = ins.class
417
433
  cls.loaded(self, ...) if cls.respond_to? :loaded
418
- ins.class.bottom_commands.each do |cmd|
434
+ ins.class.callable_commands.each do |cmd|
419
435
  unless cmd.respond_to? :self_replaced
420
436
  cmd.define_singleton_method(:extension) { ins.class.name }
421
437
  cmd.replace_block(ins)
422
438
  cmd.block.define_singleton_method(:self_replaced) { true }
423
439
  end
424
- @bottom_commands << cmd
440
+ @callable_commands << cmd
425
441
  end
426
442
  @extensions[ins.class.name] = ins
427
443
  ins
@@ -440,9 +456,10 @@ module Discorb
440
456
  # @note If the token is nil, you should use `discorb run` with the `-e` or `--env` option.
441
457
  #
442
458
  def run(token = nil, shards: nil, shard_count: nil)
443
- token ||= ENV["DISCORB_CLI_TOKEN"]
459
+ token ||= ENV.fetch("DISCORB_CLI_TOKEN", nil)
444
460
  raise ArgumentError, "Token is not specified, and -e/--env is not specified" if token.nil?
445
- case ENV["DISCORB_CLI_FLAG"]
461
+
462
+ case ENV.fetch("DISCORB_CLI_FLAG", nil)
446
463
  when nil
447
464
  start_client(token, shards: shards, shard_count: shard_count)
448
465
  when "run"
@@ -458,7 +475,7 @@ module Discorb
458
475
  #
459
476
  def close!
460
477
  if @shards.any?
461
- @shards.each(&:close!)
478
+ @shards.each_value(&:close!)
462
479
  else
463
480
  @connection.send_close
464
481
  end
@@ -490,18 +507,20 @@ module Discorb
490
507
 
491
508
  def before_run(token)
492
509
  require "json"
493
- options = JSON.parse(ENV["DISCORB_CLI_OPTIONS"], symbolize_names: true)
510
+ options = JSON.parse(ENV.fetch("DISCORB_CLI_OPTIONS", nil), symbolize_names: true)
494
511
  setup_commands(token) if options[:setup]
495
512
  end
496
513
 
497
514
  def run_setup(token)
498
- guild_ids = "global"
499
- if guilds = ENV["DISCORB_SETUP_GUILDS"]
515
+ # @type var guild_ids: Array[String] | false
516
+ guild_ids = false
517
+ if guilds = ENV.fetch("DISCORB_SETUP_GUILDS", nil)
500
518
  guild_ids = guilds.split(",")
501
519
  end
502
520
  guild_ids = false if guild_ids == ["global"]
503
521
  setup_commands(token, guild_ids: guild_ids).wait
504
- if ENV["DISCORB_SETUP_SCRIPT"] == "true"
522
+ clear_commands(token, ENV.fetch("DISCORB_SETUP_CLEAR_GUILDS", "").split(","))
523
+ if ENV.fetch("DISCORB_SETUP_SCRIPT", nil) == "true"
505
524
  @events[:setup]&.each do |event|
506
525
  event.call
507
526
  end
@@ -534,8 +553,9 @@ module Discorb
534
553
  end
535
554
 
536
555
  def session_id=(value)
537
- if shard_id
538
- @shards[shard_id].session_id = value
556
+ sid = shard_id
557
+ if sid
558
+ @shards[sid].session_id = value
539
559
  else
540
560
  @session_id = value
541
561
  end
@@ -552,28 +572,22 @@ module Discorb
552
572
  if shards.nil?
553
573
  main_loop(nil)
554
574
  else
555
- @shards = shards.map.with_index do |shard, i|
556
- Shard.new(self, shard, shard_count, i)
575
+ @shards = shards.to_h.with_index do |shard, i|
576
+ [shard, Shard.new(self, shard, shard_count, i)]
557
577
  end
558
- @shards[..-1].each_with_index do |shard, i|
559
- shard.next_shard = @shards[i + 1]
578
+ @shards.values[..-1].each_with_index do |shard, i|
579
+ shard.next_shard = @shards.values[i + 1]
560
580
  end
561
- @shards.each { |s| s.thread.join }
581
+ @shards.each_value { |s| s.thread.join }
562
582
  end
563
583
  end
564
584
 
565
585
  def main_loop(shard)
566
- close_condition = Async::Condition.new
567
- self.main_task = Async do
568
- set_status(:running, shard)
569
- connect_gateway(false).wait
570
- rescue StandardError
571
- set_status(:running, shard)
572
- close_condition.signal
573
- raise
574
- end
575
- close_condition.wait
576
- main_task.stop
586
+ set_status(:running, shard)
587
+ connect_gateway(false).wait
588
+ rescue StandardError
589
+ set_status(:closed, shard)
590
+ raise
577
591
  end
578
592
 
579
593
  def main_task
@@ -595,13 +609,13 @@ module Discorb
595
609
  def set_default_events
596
610
  on :error, override: true do |event_name, _args, e|
597
611
  message = "An error occurred while dispatching #{event_name}:\n#{e.full_message}"
598
- logger.error message, fallback: $stderr
612
+ logger.error message
599
613
  end
600
614
 
601
615
  once :standby do
602
616
  next if @title == false
603
617
 
604
- title = @title || ENV["DISCORB_CLI_TITLE"] || "discorb: #{@user}"
618
+ title = @title || ENV.fetch("DISCORB_CLI_TITLE", nil) || "discorb: #{@user}"
605
619
  Process.setproctitle title
606
620
  end
607
621
  end
data/lib/discorb/color.rb CHANGED
@@ -85,7 +85,11 @@ module Discorb
85
85
  # @return [Hash{:r, :g, :b => Integer}] A RGB hash.
86
86
  #
87
87
  def to_rgb_hash
88
- [@value / (256 * 256), @value / 256 % 256, @value % 256]
88
+ {
89
+ r: @value / (256 * 256),
90
+ g: @value / 256 % 256,
91
+ b: @value % 256,
92
+ }
89
93
  end
90
94
 
91
95
  alias deconstruct_keys to_rgb_hash
@@ -131,65 +135,38 @@ module Discorb
131
135
  # Create a color from a Discord's color.
132
136
  # Currently these colors are supported:
133
137
  #
134
- # | Color Name | Hexadecimal |
135
- # |------------|------------|
136
- # | `:teal` | `#1abc9c` |
137
- # | `:dark_teal` | `#11806a` |
138
- # | `:green` | `#2ecc71` |
139
- # | `:dark_green` | `#1f8b4c` |
140
- # | `:blue` | `#3498db` |
141
- # | `:dark_blue` | `#206694` |
142
- # | `:purple` | `#9b59b6` |
143
- # | `:dark_purple` | `#71368a` |
144
- # | `:magenta` | `#e91e63` |
145
- # | `:dark_magenta` | `#ad1457` |
146
- # | `:gold` | `#f1c40f` |
147
- # | `:dark_gold` | `#c27c0e` |
148
- # | `:orange` | `#e67e22` |
149
- # | `:dark_orange` | `#a84300` |
150
- # | `:red` | `#e74c3c` |
151
- # | `:dark_red` | `#992d22` |
152
- # | `:lighter_grey` | `#95a5a6` |
153
- # | `:lighter_gray` | `#95a5a6` |
154
- # | `:dark_grey` | `#607d8b` |
155
- # | `:dark_gray` | `#607d8b` |
156
- # | `:light_grey` | `#979c9f` |
157
- # | `:light_gray` | `#979c9f` |
158
- # | `:darker_grey` | `#546e7a` |
159
- # | `:darker_gray` | `#546e7a` |
160
- # | `:og_blurple` | `#7289da` |
161
- # | `:blurple` | `#5865f2` |
162
- # | `:greyple` | `#99aab5` |
163
- # | `:dark_theme` | `#36393f` |
164
- # | `:fuchsia` | `#eb459e` |
165
- # | `:dark_teal` | `#11806a` |
166
- # | `:green` | `#2ecc71` |
167
- # | `:dark_green` | `#1f8b4c` |
168
- # | `:blue` | `#3498db` |
169
- # | `:dark_blue` | `#206694` |
170
- # | `:purple` | `#9b59b6` |
171
- # | `:dark_purple` | `#71368a` |
172
- # | `:magenta` | `#e91e63` |
173
- # | `:dark_magenta` | `#ad1457` |
174
- # | `:gold` | `#f1c40f` |
175
- # | `:dark_gold` | `#c27c0e` |
176
- # | `:orange` | `#e67e22` |
177
- # | `:dark_orange` | `#a84300` |
178
- # | `:red` | `#e74c3c` |
179
- # | `:dark_red` | `#992d22` |
180
- # | `:lighter_grey` | `#95a5a6` |
181
- # | `:lighter_gray` | `#95a5a6` |
182
- # | `:dark_grey` | `#607d8b` |
183
- # | `:dark_gray` | `#607d8b` |
184
- # | `:light_grey` | `#979c9f` |
185
- # | `:light_gray` | `#979c9f` |
186
- # | `:darker_grey` | `#546e7a` |
187
- # | `:darker_gray` | `#546e7a` |
188
- # | `:og_blurple` | `#7289da` |
189
- # | `:blurple` | `#5865f2` |
190
- # | `:greyple` | `#99aab5` |
191
- # | `:dark_theme` | `#36393f` |
192
- # | `:fuchsia` | `#eb459e` |
138
+ # | Color name | Color code |
139
+ # |-----------------|------------|
140
+ # | `:teal` | `#1abc9c` |
141
+ # | `:dark_teal` | `#11806a` |
142
+ # | `:green` | `#2ecc71` |
143
+ # | `:dark_green` | `#1f8b4c` |
144
+ # | `:blue` | `#3498db` |
145
+ # | `:dark_blue` | `#206694` |
146
+ # | `:purple` | `#9b59b6` |
147
+ # | `:dark_purple` | `#71368a` |
148
+ # | `:magenta` | `#e91e63` |
149
+ # | `:dark_magenta` | `#ad1457` |
150
+ # | `:gold` | `#f1c40f` |
151
+ # | `:dark_gold` | `#c27c0e` |
152
+ # | `:orange` | `#e67e22` |
153
+ # | `:dark_orange` | `#a84300` |
154
+ # | `:red` | `#e74c3c` |
155
+ # | `:dark_red` | `#992d22` |
156
+ # | `:lighter_grey` | `#95a5a6` |
157
+ # | `:lighter_gray` | `#95a5a6` |
158
+ # | `:dark_grey` | `#607d8b` |
159
+ # | `:dark_gray` | `#607d8b` |
160
+ # | `:light_grey` | `#979c9f` |
161
+ # | `:light_gray` | `#979c9f` |
162
+ # | `:darker_grey` | `#546e7a` |
163
+ # | `:darker_gray` | `#546e7a` |
164
+ # | `:og_blurple` | `#7289da` |
165
+ # | `:blurple` | `#5865f2` |
166
+ # | `:greyple` | `#99aab5` |
167
+ # | `:dark_theme` | `#36393f` |
168
+ # | `:fuchsia` | `#eb459e` |
169
+ # | `:yellow` | `#fee75c` |
193
170
  #
194
171
  # @param [Symbol] color A Discord color name.
195
172
  #
@@ -4,7 +4,7 @@ module Discorb
4
4
  # @return [String] The API base URL.
5
5
  API_BASE_URL = "https://discord.com/api/v10"
6
6
  # @return [String] The version of discorb.
7
- VERSION = "0.16.0"
7
+ VERSION = "0.18.0"
8
8
  # @return [Array<Integer>] The version array of discorb.
9
9
  VERSION_ARRAY = VERSION.split(".").map(&:to_i).freeze
10
10
  # @return [String] The user agent for the bot.
@@ -152,6 +152,7 @@ module Discorb
152
152
  def major_param
153
153
  param_type = @key.split("/").find { |k| k.start_with?(":") }
154
154
  return "" unless param_type
155
+
155
156
  param = url.gsub(API_BASE_URL, "").split("/")[@key.split("/").index(param_type) - 1]
156
157
  %w[:channel_id :guild_id :webhook_id].include?(param_type) ? param : ""
157
158
  end
@@ -53,7 +53,8 @@ module Discorb
53
53
  #
54
54
  # Converts the button to a hash.
55
55
  #
56
- # @see https://discord.com/developers/docs/interactions/message-components#button-object-button-structure Official Discord API docs
56
+ # @see https://discord.com/developers/docs/interactions/message-components#button-object-button-structure
57
+ # Official Discord API docs
57
58
  # @return [Hash] A hash representation of the button.
58
59
  #
59
60
  def to_hash
@@ -37,7 +37,8 @@ module Discorb
37
37
  #
38
38
  # Converts the select menu to a hash.
39
39
  #
40
- # @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure Official Discord API docs
40
+ # @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure
41
+ # Official Discord API docs
41
42
  # @return [Hash] A hash representation of the select menu.
42
43
  #
43
44
  def to_hash
@@ -109,7 +110,8 @@ module Discorb
109
110
  #
110
111
  # Converts the option to a hash.
111
112
  #
112
- # @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure Official Discord API docs
113
+ # @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure
114
+ # Official Discord API docs
113
115
  # @return [Hash] Hash representation of the option.
114
116
  #
115
117
  def to_hash
@@ -40,7 +40,16 @@ module Discorb
40
40
  # @param [String, nil] value The prefilled value of the text input.
41
41
  # @param [String, nil] placeholder The placeholder of the text input.
42
42
  #
43
- def initialize(label, custom_id, style, min_length: nil, max_length: nil, required: false, value: nil, placeholder: nil)
43
+ def initialize(
44
+ label,
45
+ custom_id,
46
+ style,
47
+ min_length: nil,
48
+ max_length: nil,
49
+ required: false,
50
+ value: nil,
51
+ placeholder: nil
52
+ )
44
53
  @label = label
45
54
  @custom_id = custom_id
46
55
  @style = style
@@ -54,7 +63,8 @@ module Discorb
54
63
  #
55
64
  # Converts the select menu to a hash.
56
65
  #
57
- # @see https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure Official Discord API docs
66
+ # @see https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure
67
+ # Official Discord API docs
58
68
  # @return [Hash] A hash representation of the text input.
59
69
  #
60
70
  def to_hash
@@ -63,4 +63,4 @@ end
63
63
 
64
64
  %w[button text_input select_menu].each do |name|
65
65
  require_relative "components/#{name}"
66
- end
66
+ end
@@ -34,7 +34,7 @@ module Discorb
34
34
  @cache[id.to_s] = body
35
35
  @cache = @cache.sort_by(&@sort).to_h if @sort
36
36
  @cache.delete(@cache.keys[0]) if !@limit.nil? && @cache.size > @limit
37
- body
37
+ self
38
38
  end
39
39
 
40
40
  #
data/lib/discorb/embed.rb CHANGED
@@ -35,7 +35,7 @@ module Discorb
35
35
  #
36
36
  # @param [String] title The title of embed.
37
37
  # @param [String] description The description of embed.
38
- # @param [Discorb::Color] color The color of embed.
38
+ # @param [Discorb::Color, Integer] color The color of embed.
39
39
  # @param [String] url The url of embed.
40
40
  # @param [Time] timestamp The timestamp of embed.
41
41
  # @param [Discorb::Embed::Author] author The author field of embed.
@@ -44,19 +44,29 @@ module Discorb
44
44
  # @param [Discorb::Embed::Image, String] image The image of embed.
45
45
  # @param [Discorb::Embed::Thumbnail, String] thumbnail The thumbnail of embed.
46
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)
47
+ def initialize(
48
+ title = nil,
49
+ description = nil,
50
+ color: nil,
51
+ url: nil,
52
+ timestamp: nil,
53
+ author: nil,
54
+ fields: nil,
55
+ footer: nil,
56
+ image: nil,
57
+ thumbnail: nil
58
+ )
49
59
  @title = title
50
60
  @description = description
51
61
  @url = url
52
62
  @timestamp = timestamp
53
- @color = color
63
+ @color = color && (color.is_a?(Color) ? color : Color.new(color))
54
64
  @author = author
55
65
  @fields = fields || []
56
66
  @footer = footer
57
67
  @image = image && (image.is_a?(String) ? Image.new(image) : image)
58
68
  @thumbnail = thumbnail && (thumbnail.is_a?(String) ? Thumbnail.new(thumbnail) : thumbnail)
59
- @type = "rich"
69
+ @type = :rich
60
70
  end
61
71
 
62
72
  #
@@ -103,6 +113,7 @@ module Discorb
103
113
  # @return [Hash] Converted embed.
104
114
  #
105
115
  def to_hash
116
+ # @type var ret: Hash[untyped, untyped]
106
117
  ret = { type: "rich" }
107
118
  ret[:title] = @title if @title
108
119
  ret[:description] = @description if @description
@@ -161,7 +172,8 @@ module Discorb
161
172
  #
162
173
  # Convert author to hash.
163
174
  #
164
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure Offical Discord API Docs
175
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure
176
+ # Offical Discord API Docs
165
177
  # @return [Hash] Converted author.
166
178
  #
167
179
  def to_hash
@@ -193,7 +205,8 @@ module Discorb
193
205
  #
194
206
  # Convert footer to hash.
195
207
  #
196
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure Offical Discord API Docs
208
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
209
+ # Offical Discord API Docs
197
210
  # @return [Hash] Converted footer.
198
211
  #
199
212
  def to_hash
@@ -231,7 +244,8 @@ module Discorb
231
244
  #
232
245
  # Convert field to hash.
233
246
  #
234
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure Offical Discord API Docs
247
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
248
+ # Offical Discord API Docs
235
249
  # @return [Hash] Converted field.
236
250
  #
237
251
  def to_hash
@@ -279,7 +293,8 @@ module Discorb
279
293
  #
280
294
  # Convert image to hash for sending.
281
295
  #
282
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure Offical Discord API Docs
296
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure
297
+ # Offical Discord API Docs
283
298
  # @return [Hash] Converted image.
284
299
  #
285
300
  def to_hash
@@ -323,7 +338,8 @@ module Discorb
323
338
  #
324
339
  # Convert thumbnail to hash for sending.
325
340
  #
326
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure Offical Discord API Docs
341
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure
342
+ # Offical Discord API Docs
327
343
  # @return [Hash] Converted thumbnail.
328
344
  #
329
345
  def to_hash
data/lib/discorb/emoji.rb CHANGED
@@ -29,7 +29,7 @@ module Discorb
29
29
  attr_reader :roles
30
30
  # @return [Discorb::User] The user that created this emoji.
31
31
  attr_reader :user
32
- # @return [Boolean] Whether the emoji requires colons.
32
+ # @return [Discorb::Guild] The guild that owns this emoji.
33
33
  attr_reader :guild
34
34
  # @return [Boolean] whether the emoji is managed by integration (ex: Twitch).
35
35
  attr_reader :managed
@@ -103,7 +103,10 @@ module Discorb
103
103
  payload = {}
104
104
  payload[:name] = name if name != Discorb::Unset
105
105
  payload[:roles] = roles.map { |r| Discorb::Utils.try(r, :id) } if roles != Discorb::Unset
106
- @client.http.request(Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id", :patch), payload, audit_log_reason: reason)
106
+ @client.http.request(
107
+ Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id",
108
+ :patch), payload, audit_log_reason: reason,
109
+ )
107
110
  self
108
111
  end
109
112
  end
@@ -120,7 +123,10 @@ module Discorb
120
123
  #
121
124
  def delete!(reason: nil)
122
125
  Async do
123
- @client.http.request(Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id", :delete), audit_log_reason: reason).wait
126
+ @client.http.request(
127
+ Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id",
128
+ :delete), {}, audit_log_reason: reason,
129
+ ).wait
124
130
  @available = false
125
131
  self
126
132
  end
@@ -230,10 +236,31 @@ module Discorb
230
236
  elsif EmojiTable::UNICODE_TO_DISCORD.key?(name)
231
237
  @name = EmojiTable::UNICODE_TO_DISCORD[name][0]
232
238
  @value = name
239
+ elsif EmojiTable::SKIN_TONES.any? { |t| name.include?(t) }
240
+ name2 = name.dup
241
+ EmojiTable::SKIN_TONES.each.with_index do |t, i|
242
+ next unless name2.include?(t)
243
+
244
+ @skin_tone = i
245
+ name2.sub!(t, "")
246
+ break
247
+ end
248
+ raise ArgumentError, "Invalid skin tone: #{tone}" unless @skin_tone
249
+
250
+ @name = EmojiTable::UNICODE_TO_DISCORD[name2].first
251
+ @value = name
233
252
  else
234
253
  raise ArgumentError, "No such emoji: #{name}"
235
254
  end
236
- @value += EmojiTable::SKIN_TONES[tone] if tone.positive?
255
+ if tone.positive?
256
+ unless @value = EmojiTable::DISCORD_TO_UNICODE["#{name}_tone#{tone}"]
257
+ raise ArgumentError,
258
+ "Invalid skin tone for emoji: #{name}"
259
+ end
260
+
261
+ @name = "#{name}_tone#{tone}"
262
+ @skin_tone = tone
263
+ end
237
264
  end
238
265
 
239
266
  # @return [String] The unicode string of the emoji.