discorb 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) 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 +1 -1
  8. data/.github/workflows/spec.yml +30 -0
  9. data/.lefthook/commit-msg/validator.rb +5 -0
  10. data/.rspec +2 -0
  11. data/.rspec_parallel +2 -0
  12. data/.rubocop.yml +43 -6
  13. data/Changelog.md +14 -1
  14. data/Gemfile +14 -8
  15. data/Rakefile +41 -26
  16. data/bin/console +3 -3
  17. data/docs/Examples.md +1 -1
  18. data/docs/application_command.md +138 -46
  19. data/docs/cli/irb.md +2 -2
  20. data/docs/cli/new.md +14 -9
  21. data/docs/cli/run.md +7 -11
  22. data/docs/cli.md +17 -10
  23. data/docs/events.md +209 -211
  24. data/docs/extension.md +1 -2
  25. data/docs/faq.md +0 -1
  26. data/docs/tutorial.md +12 -12
  27. data/docs/voice_events.md +106 -106
  28. data/examples/commands/message.rb +63 -0
  29. data/examples/commands/permission.rb +18 -0
  30. data/examples/commands/slash.rb +44 -0
  31. data/examples/commands/user.rb +51 -0
  32. data/examples/components/authorization_button.rb +2 -2
  33. data/examples/components/select_menu.rb +2 -2
  34. data/examples/extension/main.rb +1 -1
  35. data/examples/extension/message_expander.rb +5 -2
  36. data/examples/simple/eval.rb +2 -2
  37. data/examples/simple/ping_pong.rb +1 -1
  38. data/examples/simple/rolepanel.rb +1 -1
  39. data/examples/simple/shard.rb +1 -1
  40. data/examples/simple/wait_for_message.rb +1 -1
  41. data/exe/discorb +31 -16
  42. data/lefthook.yml +45 -0
  43. data/lib/discorb/allowed_mentions.rb +1 -0
  44. data/lib/discorb/app_command/command.rb +127 -65
  45. data/lib/discorb/app_command/common.rb +25 -0
  46. data/lib/discorb/app_command/handler.rb +115 -33
  47. data/lib/discorb/app_command.rb +2 -1
  48. data/lib/discorb/application.rb +1 -0
  49. data/lib/discorb/asset.rb +1 -2
  50. data/lib/discorb/attachment.rb +1 -1
  51. data/lib/discorb/audit_logs.rb +11 -8
  52. data/lib/discorb/channel/base.rb +108 -0
  53. data/lib/discorb/channel/category.rb +32 -0
  54. data/lib/discorb/channel/container.rb +44 -0
  55. data/lib/discorb/channel/dm.rb +28 -0
  56. data/lib/discorb/channel/guild.rb +245 -0
  57. data/lib/discorb/channel/stage.rb +140 -0
  58. data/lib/discorb/channel/text.rb +345 -0
  59. data/lib/discorb/channel/thread.rb +321 -0
  60. data/lib/discorb/channel/voice.rb +79 -0
  61. data/lib/discorb/channel.rb +2 -1165
  62. data/lib/discorb/client.rb +38 -26
  63. data/lib/discorb/common.rb +2 -1
  64. data/lib/discorb/components/button.rb +2 -1
  65. data/lib/discorb/components/select_menu.rb +4 -2
  66. data/lib/discorb/components/text_input.rb +12 -2
  67. data/lib/discorb/components.rb +1 -1
  68. data/lib/discorb/embed.rb +22 -7
  69. data/lib/discorb/emoji.rb +30 -3
  70. data/lib/discorb/emoji_table.rb +4969 -3
  71. data/lib/discorb/event.rb +29 -4
  72. data/lib/discorb/exe/about.rb +1 -0
  73. data/lib/discorb/exe/irb.rb +2 -4
  74. data/lib/discorb/exe/new.rb +90 -23
  75. data/lib/discorb/exe/run.rb +8 -22
  76. data/lib/discorb/exe/setup.rb +25 -12
  77. data/lib/discorb/exe/show.rb +4 -3
  78. data/lib/discorb/extend.rb +1 -0
  79. data/lib/discorb/extension.rb +6 -3
  80. data/lib/discorb/flag.rb +11 -0
  81. data/lib/discorb/gateway.rb +67 -19
  82. data/lib/discorb/guild.rb +188 -56
  83. data/lib/discorb/guild_template.rb +10 -4
  84. data/lib/discorb/http.rb +16 -9
  85. data/lib/discorb/integration.rb +4 -1
  86. data/lib/discorb/intents.rb +1 -1
  87. data/lib/discorb/interaction/autocomplete.rb +28 -16
  88. data/lib/discorb/interaction/command.rb +36 -12
  89. data/lib/discorb/interaction/components.rb +5 -2
  90. data/lib/discorb/interaction/modal.rb +0 -1
  91. data/lib/discorb/interaction/response.rb +61 -22
  92. data/lib/discorb/interaction/root.rb +13 -13
  93. data/lib/discorb/interaction.rb +1 -0
  94. data/lib/discorb/invite.rb +5 -2
  95. data/lib/discorb/member.rb +25 -5
  96. data/lib/discorb/message.rb +47 -14
  97. data/lib/discorb/message_meta.rb +1 -0
  98. data/lib/discorb/modules.rb +56 -14
  99. data/lib/discorb/presence.rb +2 -2
  100. data/lib/discorb/rate_limit.rb +7 -2
  101. data/lib/discorb/reaction.rb +4 -4
  102. data/lib/discorb/role.rb +19 -4
  103. data/lib/discorb/shard.rb +1 -1
  104. data/lib/discorb/sticker.rb +8 -7
  105. data/lib/discorb/user.rb +2 -1
  106. data/lib/discorb/utils/colored_puts.rb +1 -0
  107. data/lib/discorb/voice_state.rb +10 -6
  108. data/lib/discorb/webhook.rb +36 -24
  109. data/lib/discorb.rb +5 -3
  110. data/po/yard.pot +9 -9
  111. data/sig/discorb.rbs +7232 -7235
  112. metadata +21 -5
  113. data/examples/commands/bookmarker.rb +0 -42
  114. data/examples/commands/hello.rb +0 -10
  115. data/examples/commands/inspect.rb +0 -25
@@ -96,7 +96,8 @@ module Discorb
96
96
 
97
97
  unless @guild.nil?
98
98
  @member = if data.key?(:member)
99
- @guild.members[data[:member][:user][:id]] || Member.new(@client, @guild_id, data[:member][:user], data[:member])
99
+ @guild.members[data[:member][:user][:id]] || Member.new(@client, @guild_id, data[:member][:user],
100
+ data[:member])
100
101
  else
101
102
  @guild.members[data[:user_id]]
102
103
  end
@@ -130,6 +131,7 @@ module Discorb
130
131
  class IntegrationDeleteEvent < GatewayEvent
131
132
  # @return [Discorb::Snowflake] The ID of the integration.
132
133
  attr_reader :id
134
+
133
135
  # @!attribute [r] guild
134
136
  # @macro client_cache
135
137
  # @return [Discorb::Guild] The guild of the integration.
@@ -281,6 +283,7 @@ module Discorb
281
283
  attr_reader :guild
282
284
  # @return [Discorb::ScheduledEvent] The scheduled event.
283
285
  attr_reader :scheduled_event
286
+
284
287
  #
285
288
  # Initialize a new instance of the ScheduledEventUserEvent class.
286
289
  # @private
@@ -482,7 +485,14 @@ module Discorb
482
485
  @guild_id = Snowflake.new(data[:guild_id]) if data.key?(:guild_id)
483
486
  @user_id = Snowflake.new(data[:user_id])
484
487
  @timestamp = Time.at(data[:timestamp])
485
- @member = guild.members[@user_id] || Member.new(@client, @guild_id, @client.users[@user_id].instance_variable_get(:@data), data[:member]) if guild
488
+ if guild
489
+ @member = guild.members[@user_id] || Member.new(
490
+ @client,
491
+ @guild_id,
492
+ @client.users[@user_id].instance_variable_get(:@data),
493
+ data[:member]
494
+ )
495
+ end
486
496
  end
487
497
 
488
498
  def user
@@ -594,7 +604,8 @@ module Discorb
594
604
  _, gateway_response = @http.request(Route.new("/gateway", "//gateway", :get)).wait
595
605
  gateway_url = gateway_response[:url]
596
606
  gateway_version = if @intents.to_h[:message_content].nil?
597
- warn "message_content intent not set, using gateway version 9. You should specify `message_content` intent for preventing unexpected changes in the future."
607
+ warn "message_content intent not set, using gateway version 9. " \
608
+ "You should specify `message_content` intent for preventing unexpected changes in the future."
598
609
  9
599
610
  else
600
611
  10
@@ -604,7 +615,11 @@ module Discorb
604
615
  alpn_protocols: Async::HTTP::Protocol::HTTP11.names,
605
616
  )
606
617
  begin
607
- self.connection = Async::WebSocket::Client.connect(endpoint, headers: [["User-Agent", Discorb::USER_AGENT]], handler: RawConnection)
618
+ self.connection = Async::WebSocket::Client.connect(
619
+ endpoint,
620
+ headers: [["User-Agent", Discorb::USER_AGENT]],
621
+ handler: RawConnection,
622
+ )
608
623
  zlib_stream = Zlib::Inflate.new(Zlib::MAX_WBITS)
609
624
  buffer = +""
610
625
  begin
@@ -632,6 +647,7 @@ module Discorb
632
647
  Errno::ECONNRESET,
633
648
  IOError => e
634
649
  next if @status == :closed
650
+
635
651
  logger.error "Gateway connection closed accidentally: #{e.class}: #{e.message}"
636
652
  connection.force_close
637
653
  connect_gateway(true)
@@ -688,7 +704,8 @@ module Discorb
688
704
  con.write({ op: opcode, d: value }.to_json)
689
705
  con.flush
690
706
  end
691
- logger.debug "Sent message to fd #{connection.io.fileno}: #{{ op: opcode, d: value }.to_json.gsub(@token, "[Token]")}"
707
+ logger.debug "Sent message to fd #{connection.io.fileno}: #{{ op: opcode, d: value }.to_json.gsub(@token,
708
+ "[Token]")}"
692
709
  end
693
710
 
694
711
  def handle_gateway(payload, reconnect)
@@ -762,7 +779,11 @@ module Discorb
762
779
  end
763
780
 
764
781
  def handle_event(event_name, data)
765
- return logger.debug "Client isn't ready; event #{event_name} wasn't handled" if @wait_until_ready && !@ready && !%w[READY GUILD_CREATE].include?(event_name)
782
+ return logger.debug "Client isn't ready; event #{event_name} wasn't handled" if @wait_until_ready &&
783
+ !@ready &&
784
+ !%w[
785
+ READY GUILD_CREATE
786
+ ].include?(event_name)
766
787
 
767
788
  dispatch(:event_receive, event_name, data)
768
789
  logger.debug "Handling event #{event_name}"
@@ -829,7 +850,9 @@ module Discorb
829
850
  dispatch(:role_update, before, current)
830
851
  when "GUILD_ROLE_DELETE"
831
852
  return logger.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
832
- return logger.warn "Unknown role id #{data[:role_id]}, ignoring" unless (role = guild.roles.delete(data[:role_id]))
853
+ unless (role = guild.roles.delete(data[:role_id]))
854
+ return logger.warn "Unknown role id #{data[:role_id]}, ignoring"
855
+ end
833
856
 
834
857
  dispatch(:role_delete, role)
835
858
  when "CHANNEL_CREATE"
@@ -874,7 +897,9 @@ module Discorb
874
897
  dispatch(:thread_delete, thread)
875
898
  when "THREAD_LIST_SYNC"
876
899
  data[:threads].each do |raw_thread|
877
- thread = Channel.make_channel(self, raw_thread.merge({ member: raw_thread[:members].find { |m| m[:id] == raw_thread[:id] } }))
900
+ thread = Channel.make_channel(self, raw_thread.merge({ member: raw_thread[:members].find do |m|
901
+ m[:id] == raw_thread[:id]
902
+ end }))
878
903
  @channels[thread.id] = thread
879
904
  end
880
905
  when "THREAD_MEMBER_UPDATE"
@@ -908,15 +933,23 @@ module Discorb
908
933
  instance = StageInstance.new(self, data)
909
934
  dispatch(:stage_instance_create, instance)
910
935
  when "STAGE_INSTANCE_UPDATE"
911
- return logger.warn "Unknown channel id #{data[:channel_id]} , ignoring" unless (channel = @channels[data[:channel_id]])
912
- return logger.warn "Unknown stage instance id #{data[:id]}, ignoring" unless (instance = channel.stage_instances[data[:id]])
936
+ unless (channel = @channels[data[:channel_id]])
937
+ return logger.warn "Unknown channel id #{data[:channel_id]} , ignoring"
938
+ end
939
+ unless (instance = channel.stage_instances[data[:id]])
940
+ return logger.warn "Unknown stage instance id #{data[:id]}, ignoring"
941
+ end
913
942
 
914
943
  old = StageInstance.new(self, instance.instance_variable_get(:@data), no_cache: true)
915
944
  current.send(:_set_data, data)
916
945
  dispatch(:stage_instance_update, old, current)
917
946
  when "STAGE_INSTANCE_DELETE"
918
- return logger.warn "Unknown channel id #{data[:channel_id]} , ignoring" unless (channel = @channels[data[:channel_id]])
919
- return logger.warn "Unknown stage instance id #{data[:id]}, ignoring" unless (instance = channel.stage_instances.delete(data[:id]))
947
+ unless (channel = @channels[data[:channel_id]])
948
+ return logger.warn "Unknown channel id #{data[:channel_id]} , ignoring"
949
+ end
950
+ unless (instance = channel.stage_instances.delete(data[:id]))
951
+ return logger.warn "Unknown stage instance id #{data[:id]}, ignoring"
952
+ end
920
953
 
921
954
  dispatch(:stage_instance_delete, instance)
922
955
  when "GUILD_MEMBER_ADD"
@@ -927,14 +960,18 @@ module Discorb
927
960
  dispatch(:member_add, nm)
928
961
  when "GUILD_MEMBER_UPDATE"
929
962
  return logger.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
930
- return logger.warn "Unknown member id #{data[:user][:id]}, ignoring" unless (nm = guild.members[data[:user][:id]])
963
+ unless (nm = guild.members[data[:user][:id]])
964
+ return logger.warn "Unknown member id #{data[:user][:id]}, ignoring"
965
+ end
931
966
 
932
967
  old = Member.new(self, data[:guild_id], data[:user], data.update({ no_cache: true }))
933
968
  nm.send(:_set_data, data[:user], data)
934
969
  dispatch(:member_update, old, nm)
935
970
  when "GUILD_MEMBER_REMOVE"
936
971
  return logger.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
937
- return logger.warn "Unknown member id #{data[:user][:id]}, ignoring" unless (member = guild.members.delete(data[:user][:id]))
972
+ unless (member = guild.members.delete(data[:user][:id]))
973
+ return logger.warn "Unknown member id #{data[:user][:id]}, ignoring"
974
+ end
938
975
 
939
976
  dispatch(:member_remove, member)
940
977
  when "GUILD_BAN_ADD"
@@ -1111,7 +1148,8 @@ module Discorb
1111
1148
  when "MESSAGE_DELETE"
1112
1149
  message.instance_variable_set(:@deleted, true) if (message = @messages[data[:id]])
1113
1150
 
1114
- dispatch(:message_delete_id, Snowflake.new(data[:id]), channels[data[:channel_id]], data[:guild_id] && guilds[data[:guild_id]])
1151
+ dispatch(:message_delete_id, Snowflake.new(data[:id]), channels[data[:channel_id]],
1152
+ data[:guild_id] && guilds[data[:guild_id]])
1115
1153
  dispatch(:message_delete, message, channels[data[:channel_id]], data[:guild_id] && guilds[data[:guild_id]])
1116
1154
  when "MESSAGE_DELETE_BULK"
1117
1155
  messages = []
@@ -1158,7 +1196,9 @@ module Discorb
1158
1196
  dispatch(:reaction_remove_all, ReactionRemoveAllEvent.new(self, data))
1159
1197
  when "MESSAGE_REACTION_REMOVE_EMOJI"
1160
1198
  if (target_message = @messages[data[:message_id]]) &&
1161
- (target_reaction = target_message.reactions.find { |r| data[:emoji][:id].nil? ? r.name == data[:emoji][:name] : r.id == data[:emoji][:id] })
1199
+ (target_reaction = target_message.reactions.find do |r|
1200
+ data[:emoji][:id].nil? ? r.name == data[:emoji][:name] : r.id == data[:emoji][:id]
1201
+ end)
1162
1202
  target_message.reactions.delete(target_reaction)
1163
1203
  end
1164
1204
  dispatch(:reaction_remove_emoji, ReactionRemoveEmojiEvent.new(self, data))
@@ -1184,7 +1224,9 @@ module Discorb
1184
1224
  dispatch(:scheduled_event_create, event)
1185
1225
  when "GUILD_SCHEDULED_EVENT_UPDATE"
1186
1226
  logger.warn("Unknown guild id #{data[:guild_id]}, ignoring") unless (guild = @guilds[data[:guild_id]])
1187
- logger.warn("Unknown scheduled event id #{data[:id]}, ignoring") unless (event = guild.scheduled_events[data[:id]])
1227
+ unless (event = guild.scheduled_events[data[:id]])
1228
+ logger.warn("Unknown scheduled event id #{data[:id]}, ignoring")
1229
+ end
1188
1230
  old = event.dup
1189
1231
  event.send(:_set_data, data)
1190
1232
  dispatch(:scheduled_event_update, old, event)
@@ -1200,7 +1242,9 @@ module Discorb
1200
1242
  end
1201
1243
  when "GUILD_SCHEDULED_EVENT_DELETE"
1202
1244
  logger.warn("Unknown guild id #{data[:guild_id]}, ignoring") unless (guild = @guilds[data[:guild_id]])
1203
- logger.warn("Unknown scheduled event id #{data[:id]}, ignoring") unless (event = guild.scheduled_events[data[:id]])
1245
+ unless (event = guild.scheduled_events[data[:id]])
1246
+ logger.warn("Unknown scheduled event id #{data[:id]}, ignoring")
1247
+ end
1204
1248
  guild.scheduled_events.remove(data[:id])
1205
1249
  dispatch(:scheduled_event_delete, event)
1206
1250
  dispatch(:scheduled_event_cancel, event)
@@ -1287,7 +1331,11 @@ module Discorb
1287
1331
  end
1288
1332
 
1289
1333
  def io
1290
- @framer.instance_variable_get(:@stream).instance_variable_get(:@io).instance_variable_get(:@io).instance_variable_get(:@io)
1334
+ @framer
1335
+ .instance_variable_get(:@stream)
1336
+ .instance_variable_get(:@io)
1337
+ .instance_variable_get(:@io)
1338
+ .instance_variable_get(:@io)
1291
1339
  end
1292
1340
 
1293
1341
  def parse(buffer)