discorb 0.13.0 → 0.13.4

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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build_main.yml +1 -0
  3. data/.github/workflows/build_version.yml +4 -3
  4. data/.github/workflows/crowdin.yml +32 -0
  5. data/.gitignore +3 -1
  6. data/.yardopts +2 -0
  7. data/CODE_OF_CONDUCT.md +128 -0
  8. data/Changelog.md +420 -388
  9. data/Gemfile +5 -1
  10. data/README.md +2 -2
  11. data/Rakefile +131 -1
  12. data/crowdin.yml +2 -0
  13. data/discorb.gemspec +12 -1
  14. data/docs/Examples.md +2 -0
  15. data/docs/application_command.md +7 -5
  16. data/docs/cli/irb.md +2 -0
  17. data/docs/cli/new.md +2 -0
  18. data/docs/cli/run.md +3 -1
  19. data/docs/cli/setup.md +4 -2
  20. data/docs/cli.md +2 -0
  21. data/docs/events.md +6 -4
  22. data/docs/extension.md +2 -2
  23. data/docs/faq.md +4 -2
  24. data/docs/license.md +2 -0
  25. data/docs/tutorial.md +4 -3
  26. data/docs/voice_events.md +2 -0
  27. data/lib/discorb/app_command.rb +32 -9
  28. data/lib/discorb/application.rb +1 -1
  29. data/lib/discorb/asset.rb +1 -1
  30. data/lib/discorb/channel.rb +170 -125
  31. data/lib/discorb/client.rb +20 -22
  32. data/lib/discorb/common.rb +32 -1
  33. data/lib/discorb/dictionary.rb +10 -2
  34. data/lib/discorb/emoji.rb +7 -9
  35. data/lib/discorb/emoji_table.rb +3891 -3891
  36. data/lib/discorb/event.rb +12 -11
  37. data/lib/discorb/exe/show.rb +2 -0
  38. data/lib/discorb/extension.rb +1 -1
  39. data/lib/discorb/flag.rb +1 -1
  40. data/lib/discorb/gateway.rb +63 -17
  41. data/lib/discorb/guild.rb +110 -156
  42. data/lib/discorb/guild_template.rb +15 -12
  43. data/lib/discorb/http.rb +41 -136
  44. data/lib/discorb/integration.rb +34 -2
  45. data/lib/discorb/interaction/autocomplete.rb +1 -1
  46. data/lib/discorb/interaction/response.rb +34 -32
  47. data/lib/discorb/interaction/root.rb +8 -0
  48. data/lib/discorb/invite.rb +4 -3
  49. data/lib/discorb/log.rb +3 -2
  50. data/lib/discorb/member.rb +44 -15
  51. data/lib/discorb/message.rb +33 -22
  52. data/lib/discorb/modules.rb +28 -35
  53. data/lib/discorb/presence.rb +2 -2
  54. data/lib/discorb/rate_limit.rb +14 -18
  55. data/lib/discorb/role.rb +18 -14
  56. data/lib/discorb/sticker.rb +10 -14
  57. data/lib/discorb/user.rb +10 -10
  58. data/lib/discorb/voice_state.rb +12 -7
  59. data/lib/discorb/webhook.rb +44 -50
  60. data/lib/discorb.rb +1 -1
  61. data/po/yard.pot +7772 -5154
  62. data/sig/discorb.rbs +3316 -3819
  63. data/template-replace/scripts/locale_ja.rb +62 -0
  64. metadata +18 -5
data/lib/discorb/event.rb CHANGED
@@ -90,8 +90,7 @@ module Discorb
90
90
 
91
91
  #
92
92
  # Create a scheduled event for the guild.
93
- # @macro async
94
- # @macro http
93
+ # @async
95
94
  #
96
95
  # @param [:stage_instance, :voice, :external] type The type of event to create.
97
96
  # @param [String] name The name of the event.
@@ -103,6 +102,8 @@ module Discorb
103
102
  # @param [:guild_only] privacy_level The privacy level of the event. This must be `:guild_only`.
104
103
  # @param [:active, :completed, :canceled] status The status of the event.
105
104
  #
105
+ # @return [Async::Task<Discorb::ScheduledEvent>] The event that was created.
106
+ #
106
107
  # @see Event#start
107
108
  # @see Event#cancel
108
109
  # @see Event#complete
@@ -163,7 +164,7 @@ module Discorb
163
164
  else
164
165
  raise ArgumentError, "Invalid scheduled event type: #{type}"
165
166
  end
166
- @client.http.patch("/guilds/#{@guild_id}/scheduled-events/#{@id}", payload).wait
167
+ @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}", "//guilds/:guild_id/scheduled-events/:scheduled_event_id", :patch), payload).wait
167
168
  end
168
169
  end
169
170
 
@@ -194,12 +195,13 @@ module Discorb
194
195
 
195
196
  #
196
197
  # Deletes the event.
197
- # @macro async
198
- # @macro http
198
+ # @async
199
+ #
200
+ # @return [Async::Task<void>] The task.
199
201
  #
200
202
  def delete!
201
203
  Async do
202
- @client.http.delete("/guilds/#{@guild_id}/scheduled-events/#{@id}").wait
204
+ @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}", "//guilds/:guild_id/scheduled-events/:scheduled_event_id", :delete)).wait
203
205
  end
204
206
  end
205
207
 
@@ -207,8 +209,7 @@ module Discorb
207
209
 
208
210
  #
209
211
  # Fetches the event users.
210
- # @macro async
211
- # @macro http
212
+ # @async
212
213
  #
213
214
  # @note You can fetch all of members by not specifying a parameter.
214
215
  #
@@ -218,7 +219,7 @@ module Discorb
218
219
  # @param [Boolean] with_member Whether to include the member object of the event. Defaults to `false`.
219
220
  # This should be used for manual fetching of members.
220
221
  #
221
- # @return [Array<Discorb::Member>] The event users.
222
+ # @return [Async::Task<Array<Discorb::Member>>] The event users.
222
223
  #
223
224
  def fetch_users(limit = nil, before: nil, after: nil, with_member: true)
224
225
  Async do
@@ -226,7 +227,7 @@ module Discorb
226
227
  after = 0
227
228
  res = []
228
229
  while true
229
- _resp, users = @client.http.get("/guilds/#{@guild_id}/scheduled-events/#{@id}/users?limit=100&after=#{after}&with_member=true").wait
230
+ _resp, users = @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}/users?limit=100&after=#{after}&with_member=true", "//guilds/:guild_id/scheduled-events/:scheduled_event_id/users", :get)).wait
230
231
  if users.empty?
231
232
  break
232
233
  end
@@ -241,7 +242,7 @@ module Discorb
241
242
  after: Discorb::Utils.try(around, :id),
242
243
  with_member: with_member,
243
244
  }.filter { |_k, v| !v.nil? }.to_h
244
- _resp, messages = @client.http.get("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}").wait
245
+ _resp, messages = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}", "//channels/:channel_id/messages", :get)).wait
245
246
  messages.map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) }
246
247
  end
247
248
  end
@@ -2,7 +2,9 @@
2
2
 
3
3
  require "etc"
4
4
  require "discorb"
5
+
5
6
  puts "\e[90mRuby:\e[m #{RUBY_VERSION}"
6
7
  puts "\e[90mdiscorb:\e[m #{Discorb::VERSION}"
7
8
  uname = Etc.uname
8
9
  puts "\e[90mSystem:\e[m #{uname[:sysname]} #{uname[:release]}"
10
+ puts "\e[90mPlatform:\e[m #{RUBY_PLATFORM}"
@@ -4,7 +4,7 @@ module Discorb
4
4
  #
5
5
  # Abstract class to make extension.
6
6
  # Include from this module to make your own extension.
7
- # @see file:docs/extension.md
7
+ # @see file:docs/extension.md Extension
8
8
  # @abstract
9
9
  #
10
10
  module Extension
data/lib/discorb/flag.rb CHANGED
@@ -92,7 +92,7 @@ module Discorb
92
92
  #
93
93
  # @return [Discorb::Flag] The negation of the flag.
94
94
  #
95
- def ~@
95
+ def ~
96
96
  self.class.new(~@value)
97
97
  end
98
98
 
@@ -94,8 +94,7 @@ module Discorb
94
94
 
95
95
  # Fetch the message.
96
96
  # If message is cached, it will be returned.
97
- # @macro async
98
- # @macro http
97
+ # @async
99
98
  #
100
99
  # @param [Boolean] force Whether to force fetching the message.
101
100
  #
@@ -109,6 +108,35 @@ module Discorb
109
108
  end
110
109
  end
111
110
 
111
+ #
112
+ # Represents a `INTEGRATION_DELETE` event.
113
+ #
114
+ class IntegrationDeleteEvent < GatewayEvent
115
+ # @return [Discorb::Snowflake] The ID of the integration.
116
+ attr_reader :id
117
+ # @!attribute [r] guild
118
+ # @macro client_cache
119
+ # @return [Discorb::Guild] The guild of the integration.
120
+ # @!attribute [r] user
121
+ # @macro client_cache
122
+ # @return [Discorb::User] The user associated with the integration.
123
+
124
+ # @private
125
+ def initialize(client, data)
126
+ @id = Snowflake.new(data[:id])
127
+ @guild_id = data[:guild_id]
128
+ @user_id = data[:application_id]
129
+ end
130
+
131
+ def guild
132
+ @client.guilds[@guild_id]
133
+ end
134
+
135
+ def user
136
+ @client.users[@user_id]
137
+ end
138
+ end
139
+
112
140
  #
113
141
  # Represents a `MESSAGE_REACTION_REMOVE_ALL` event.
114
142
  #
@@ -143,8 +171,7 @@ module Discorb
143
171
 
144
172
  # Fetch the message.
145
173
  # If message is cached, it will be returned.
146
- # @macro async
147
- # @macro http
174
+ # @async
148
175
  #
149
176
  # @param [Boolean] force Whether to force fetching the message.
150
177
  #
@@ -195,8 +222,7 @@ module Discorb
195
222
 
196
223
  # Fetch the message.
197
224
  # If message is cached, it will be returned.
198
- # @macro async
199
- # @macro http
225
+ # @async
200
226
  #
201
227
  # @param [Boolean] force Whether to force fetching the message.
202
228
  #
@@ -293,8 +319,7 @@ module Discorb
293
319
  end
294
320
 
295
321
  # Fetch the message.
296
- # @macro async
297
- # @macro http
322
+ # @async
298
323
  #
299
324
  # @return [Async::Task<Discorb::Message>] The message.
300
325
  def fetch_message
@@ -503,16 +528,24 @@ module Discorb
503
528
  module Handler
504
529
  private
505
530
 
506
- def connect_gateway(reconnect)
531
+ def connect_gateway(reconnect, force_close: false)
507
532
  if reconnect
508
533
  @log.info "Reconnecting to gateway..."
509
534
  else
510
535
  @log.info "Connecting to gateway..."
511
536
  end
512
537
  Async do
513
- @connection&.close
538
+ if @connection
539
+ Async do
540
+ if force_close
541
+ @connection.force_close
542
+ else
543
+ @connection.close
544
+ end
545
+ end
546
+ end
514
547
  @http = HTTP.new(self)
515
- _, gateway_response = @http.get("/gateway").wait
548
+ _, gateway_response = @http.request(Route.new("/gateway", "//gateway", :get)).wait
516
549
  gateway_url = gateway_response[:url]
517
550
  endpoint = Async::HTTP::Endpoint.parse("#{gateway_url}?v=9&encoding=json&compress=zlib-stream",
518
551
  alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
@@ -537,9 +570,15 @@ module Discorb
537
570
  end
538
571
  end
539
572
  end
540
- rescue Async::Wrapper::Cancelled, OpenSSL::SSL::SSLError, Async::Wrapper::WaitError, EOFError => e
541
- @log.error "Gateway connection closed: #{e.class}: #{e.message}"
542
- connect_gateway(true)
573
+ rescue Async::Wrapper::Cancelled,
574
+ OpenSSL::SSL::SSLError,
575
+ Async::Wrapper::WaitError,
576
+ EOFError,
577
+ Errno::EPIPE,
578
+ Errno::ECONNRESET,
579
+ IOError => e
580
+ @log.error "Gateway connection closed accidentally: #{e.class}: #{e.message}"
581
+ connect_gateway(true, force_close: true)
543
582
  else # should never happen
544
583
  connect_gateway(true)
545
584
  end
@@ -870,9 +909,8 @@ module Discorb
870
909
  dispatch(:integration_update, integration)
871
910
  when "INTEGRATION_DELETE"
872
911
  return @log.warn "Unknown guild id #{data[:guild_id]}, ignoring" unless (guild = @guilds[data[:guild_id]])
873
- return @log.warn "Unknown integration id #{data[:id]}, ignoring" unless (integration = guild.integrations.delete(data[:id]))
874
912
 
875
- dispatch(:integration_delete, integration)
913
+ dispatch(:integration_delete, IntegrationDeleteEvent.new(self, data))
876
914
  when "WEBHOOKS_UPDATE"
877
915
  dispatch(:webhooks_update, WebhooksUpdateEvent.new(self, data))
878
916
  when "INVITE_CREATE"
@@ -888,6 +926,7 @@ module Discorb
888
926
  current = VoiceState.new(self, data)
889
927
  guild.voice_states[data[:user_id]] = current
890
928
  else
929
+ guild.voice_states.remove(data[:user_id]) if data[:channel_id].nil?
891
930
  old = VoiceState.new(self, current.instance_variable_get(:@data))
892
931
  current.send(:_set_data, data)
893
932
  end
@@ -1011,7 +1050,7 @@ module Discorb
1011
1050
  message.instance_variable_set(:@deleted, true)
1012
1051
  messages.push(message)
1013
1052
  else
1014
- messages.push(UnknownDeleteBulkMessage.new(self, id))
1053
+ messages.push(UnknownDeleteBulkMessage.new(self, id, data))
1015
1054
  end
1016
1055
  end
1017
1056
  dispatch(:message_delete_bulk, messages)
@@ -1143,6 +1182,13 @@ module Discorb
1143
1182
  def close
1144
1183
  super
1145
1184
  @closed = true
1185
+ rescue
1186
+ force_close
1187
+ end
1188
+
1189
+ def force_close
1190
+ @framer.instance_variable_get(:@stream).close
1191
+ @closed = true
1146
1192
  end
1147
1193
 
1148
1194
  def parse(buffer)