discorb 0.11.1 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b979da1f1325d5d9cae229e0262cce1d6cf16b6aca91feba731ec6a0146fe42
4
- data.tar.gz: e6e1fcbe4265cfb54828fef5c7e2d6304dd306544e941cb787233ad563d9abaf
3
+ metadata.gz: f0bee36d996a7e3108f2d02247a0d7516ade1811aeb5a354d1d41f509373381d
4
+ data.tar.gz: ce6c4f496b8ed5309cd3798b8cf25e271e77e75617631010d77f5ab3347a26aa
5
5
  SHA512:
6
- metadata.gz: bb35c0736b2e549964030103dfb1202ef7711fd3c36323710d3f94631b958a9976f7c111f340d03cf2ed67032a8f74f5165f66b5ff92c59d0628af25b4a95e79
7
- data.tar.gz: 01d59859fb5e84beaafdc48e333ba841398a8fe5867c52bb9621b2d676493dbc776297c94e303297ffeef591c7f2e527ea30aaae381323e4ed8e8b1ce632b1ef
6
+ metadata.gz: 514f4b1ce307abe3424fa666a9fab56282e7b3817371411661675507658a42a92c23cda56e6358765fcad25bae69b850e754882576392ddc45da8be08ff3d252
7
+ data.tar.gz: e11b02e78f5029a0e544a0c7a55d79ee42ee22276160ba0f00dad0bd85c2d5d15109fa28c50dbfccbaf61066e293175e6b38357db396aeaba4b3033e38dbf2c4
@@ -0,0 +1,34 @@
1
+ name: Publish Gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+ workflow_dispatch:
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@master
15
+
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: "3.0"
20
+
21
+ - name: Setup Release Credentials
22
+ env:
23
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
24
+ run: |
25
+ mkdir -p $HOME/.gem
26
+ touch $HOME/.gem/credentials
27
+ chmod 600 $HOME/.gem/credentials
28
+ echo "---" >$HOME/.gem/credentials
29
+ echo ":github: Bearer ${GITHUB_TOKEN}" >> $HOME/.gem/credentials
30
+ - name: Publish Gem to GitHub Packages
31
+ run: |
32
+ export OWNER=$( echo ${{ github.repository }} | cut -d "/" -f 1 )
33
+ gem build *.gemspec
34
+ gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
data/Changelog.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  ## v0.11
6
6
 
7
+ ### v0.11.2
8
+
9
+ - Add: Add `setup` event
10
+ - Fix: Fix gateway resuming
11
+ - Add: Add GitHub Packages
12
+
7
13
  ### v0.11.1
8
14
 
9
15
  - Improve: Improve rate limit handling
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  ----
10
10
 
11
- discorb is a Discord API wrapper for Ruby.
11
+ discorb is a Discord API wrapper written in Ruby.
12
12
 
13
13
  ## Installation
14
14
 
@@ -98,7 +98,7 @@ end
98
98
  client.run(ENV["DISCORD_BOT_TOKEN"])
99
99
  ```
100
100
 
101
- Note: You must run `discorb setup` before using slash commands.
101
+ Note you must run `discorb setup` before using slash commands.
102
102
 
103
103
  ## Contributing
104
104
 
data/discorb.gemspec CHANGED
@@ -13,8 +13,6 @@ Gem::Specification.new do |spec|
13
13
  spec.license = "MIT"
14
14
  spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
15
15
 
16
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
-
18
16
  spec.metadata["homepage_uri"] = spec.homepage
19
17
  spec.metadata["source_code_uri"] = "https://github.com/discorb-lib/discorb"
20
18
  spec.metadata["changelog_uri"] = "https://discorb-lib.github.io/file.Changelog.html"
File without changes
data/docs/cli/setup.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # discorb setup
4
4
 
5
- This command will setup application commands.
5
+ This command will setup application commands, and call `setup` event.
6
6
 
7
7
  ## Usage
8
8
 
data/docs/cli.md CHANGED
@@ -16,7 +16,7 @@ Currently, discorb has the following commands:
16
16
 
17
17
  | Command | Description |
18
18
  |---------|-------------|
19
- | {file:docs/cli/init.md `init`} | Create a new project. |
19
+ | {file:docs/cli/new.md `new`} | Create a new project. |
20
20
  | {file:docs/cli/irb.md `irb`} | Start an interactive Ruby shell with connected client. |
21
21
  | {file:docs/cli/run.md `run`} | Run a client. |
22
22
  | {file:docs/cli/setup.md `setup`} | Setup application commands. |
data/docs/events.md CHANGED
@@ -88,6 +88,11 @@ Fires when the client is resumed connection.
88
88
  Fires when an error occurs during an event.
89
89
  Defaults to printing the error to stderr, override to handle it yourself.
90
90
 
91
+ #### `setup()`
92
+
93
+ Fires when `discorb setup` is run.
94
+ This is useful for setting up some dependencies, such as the database.
95
+
91
96
  ### Guild events
92
97
 
93
98
  #### `guild_join(guild)`
@@ -446,16 +446,7 @@ module Discorb
446
446
  ::File.open(options[:log_file], "a")
447
447
  end
448
448
  @log.level = options[:log_level].to_sym
449
- @log.colorize_log = case options[:log_color]
450
- when nil
451
- if @log.out == $stdout || @log.out == $stderr
452
- true
453
- else
454
- false
455
- end
456
- when true, false
457
- options[:log_color]
458
- end
449
+ @log.colorize_log = options[:log_color] == nil ? @log.out.isatty : options[:log_color]
459
450
  end
460
451
  end
461
452
  start_client(token)
@@ -468,6 +459,10 @@ module Discorb
468
459
  guild_ids = false
469
460
  end
470
461
  setup_commands(token, guild_ids: guild_ids).wait
462
+ @events[:setup]&.each do |event|
463
+ event.call
464
+ end
465
+ self.on_setup if respond_to? :on_setup
471
466
  end
472
467
  end
473
468
 
@@ -493,7 +488,7 @@ module Discorb
493
488
  @close_condition = Async::Condition.new
494
489
  @main_task = Async do
495
490
  @status = :running
496
- connect_gateway(true).wait
491
+ connect_gateway(false).wait
497
492
  rescue
498
493
  @status = :stopped
499
494
  @close_condition.signal
data/lib/discorb/color.rb CHANGED
@@ -130,6 +130,7 @@ module Discorb
130
130
  #
131
131
  # Create a color from a Discord's color.
132
132
  # Currently these colors are supported:
133
+ #
133
134
  # | Color Name | Hexadecimal |
134
135
  # |------------|------------|
135
136
  # | `:teal` | `#1abc9c` |
@@ -4,7 +4,7 @@ module Discorb
4
4
  # @return [String] The API base URL.
5
5
  API_BASE_URL = "https://discord.com/api/v9"
6
6
  # @return [String] The version of discorb.
7
- VERSION = "0.11.1"
7
+ VERSION = "0.11.2"
8
8
  # @return [String] The user agent for the bot.
9
9
  USER_AGENT = "DiscordBot (https://discorb-lib.github.io #{VERSION}) Ruby/#{RUBY_VERSION}"
10
10
 
@@ -481,11 +481,10 @@ module Discorb
481
481
  module Handler
482
482
  private
483
483
 
484
- def connect_gateway(first)
484
+ def connect_gateway(reconnect)
485
485
  @log.info "Connecting to gateway."
486
486
  Async do
487
- @http = HTTP.new(self) if first
488
- @first = first
487
+ @http = HTTP.new(self)
489
488
  _, gateway_response = @http.get("/gateway").wait
490
489
  gateway_url = gateway_response[:url]
491
490
  endpoint = Async::HTTP::Endpoint.parse("#{gateway_url}?v=9&encoding=json&compress=zlib-stream",
@@ -495,38 +494,53 @@ module Discorb
495
494
  @connection = connection
496
495
  @zlib_stream = Zlib::Inflate.new(Zlib::MAX_WBITS)
497
496
  @buffer = +""
498
-
499
- while (message = @connection.read)
500
- @buffer << message
501
- if message.end_with?((+"\x00\x00\xff\xff").force_encoding("ASCII-8BIT"))
502
- begin
503
- data = @zlib_stream.inflate(@buffer)
504
- @buffer = +""
505
- message = JSON.parse(data, symbolize_names: true)
506
- rescue JSON::ParserError
507
- @buffer = +""
508
- @log.error "Received invalid JSON from gateway."
509
- @log.debug "#{data}"
510
- else
511
- handle_gateway(message)
497
+ begin
498
+ while (message = @connection.read)
499
+ @buffer << message
500
+ if message.end_with?((+"\x00\x00\xff\xff").force_encoding("ASCII-8BIT"))
501
+ begin
502
+ data = @zlib_stream.inflate(@buffer)
503
+ @buffer = +""
504
+ message = JSON.parse(data, symbolize_names: true)
505
+ rescue JSON::ParserError
506
+ @buffer = +""
507
+ @log.error "Received invalid JSON from gateway."
508
+ @log.debug "#{data}"
509
+ else
510
+ handle_gateway(message, reconnect)
511
+ end
512
512
  end
513
513
  end
514
+ rescue EOFError, Async::Wrapper::Cancelled, Async::Wrapper::WaitError
515
+ # Ignore
514
516
  end
515
517
  end
516
518
  rescue Protocol::WebSocket::ClosedError => e
517
- case e.message
518
- when "Authentication failed."
519
- @tasks.map(&:stop)
520
- raise ClientError.new("Authentication failed."), cause: nil
521
- when "Discord WebSocket requesting client reconnect."
522
- @log.info "Discord WebSocket requesting client reconnect"
523
- connect_gateway(false)
519
+ @tasks.map(&:stop)
520
+ case e.code
521
+ when 4004
522
+ raise ClientError.new("Authentication failed"), cause: nil
523
+ when 4009
524
+ @log.info "Session timed out, reconnecting."
525
+ connect_gateway(true)
526
+ when 4014
527
+ raise ClientError.new("Disallowed intents were specified"), cause: nil
528
+ when 4002, 4003, 4005, 4007
529
+ raise ClientError.new(<<~EOS), cause: e
530
+ Disconnected from gateway, probably due to library issues.
531
+ #{e.message}
532
+
533
+ Please report this to the library issue tracker.
534
+ https://github.com/discorb-lib/discorb/issues
535
+ EOS
536
+ when 1001
537
+ @log.info "Gateway closed with code 1001, reconnecting."
538
+ connect_gateway(true)
524
539
  else
525
- @log.error "Discord WebSocket closed: #{e.message}"
540
+ @log.error "Discord WebSocket closed with code #{e.code}."
541
+ @log.debug "#{e.message}"
526
542
  connect_gateway(false)
527
543
  end
528
- rescue EOFError, Async::Wrapper::Cancelled, Async::Wrapper::WaitError
529
- connect_gateway(false)
530
544
  rescue => e
531
545
  @log.error "Discord WebSocket error: #{e.message}"
532
546
  connect_gateway(false)
@@ -540,15 +554,23 @@ module Discorb
540
554
  @log.debug "Sent message #{{ op: opcode, d: value }.to_json.gsub(@token, "[Token]")}"
541
555
  end
542
556
 
543
- def handle_gateway(payload)
557
+ def handle_gateway(payload, reconnect)
544
558
  Async do |task|
545
559
  data = payload[:d]
546
560
  @last_s = payload[:s] if payload[:s]
547
- @log.debug "Received message with opcode #{payload[:op]} from gateway: #{data}"
561
+ @log.debug "Received message with opcode #{payload[:op]} from gateway:"
562
+ @log.debug "#{payload.to_json.gsub(@token, "[Token]")}"
548
563
  case payload[:op]
549
564
  when 10
550
565
  @heartbeat_interval = data[:heartbeat_interval]
551
- if @first
566
+ if reconnect
567
+ payload = {
568
+ token: @token,
569
+ session_id: @session_id,
570
+ seq: @last_s,
571
+ }
572
+ send_gateway(6, **payload)
573
+ else
552
574
  payload = {
553
575
  token: @token,
554
576
  intents: @intents.value,
@@ -557,38 +579,22 @@ module Discorb
557
579
  }
558
580
  payload[:presence] = @identify_presence if @identify_presence
559
581
  send_gateway(2, **payload)
560
- Async do
561
- sleep 2
562
- next unless @uncached_guilds.nil?
563
-
564
- raise ClientError, "Failed to connect to gateway.\nHint: This usually means that your intents are invalid."
565
- exit 1
566
- end
567
- else
568
- payload = {
569
- token: @token,
570
- session_id: @session_id,
571
- seq: @last_s,
572
- }
573
- send_gateway(6, **payload)
574
582
  end
575
583
  when 7
576
584
  @log.info "Received opcode 7, reconnecting"
577
585
  @tasks.map(&:stop)
578
- @connection.close
579
- connect_gateway(false)
580
586
  when 9
581
587
  @log.warn "Received opcode 9, closed connection"
582
588
  @tasks.map(&:stop)
583
589
  if data
584
590
  @log.info "Connection is resumable, reconnecting"
585
591
  @connection.close
586
- connect_gateway(false)
592
+ connect_gateway(true)
587
593
  else
588
594
  @log.info "Connection is not resumable, reconnecting with opcode 2"
589
- sleep(2)
590
595
  @connection.close
591
- connect_gateway(true)
596
+ sleep(2)
597
+ connect_gateway(false)
592
598
  end
593
599
  when 11
594
600
  @log.debug "Received opcode 11"
@@ -458,6 +458,7 @@ module Discorb
458
458
  @interaction_type = 4
459
459
  @interaction_name = :auto_complete
460
460
 
461
+ # @private
461
462
  def _set_data(data)
462
463
  super
463
464
  Sync do
@@ -511,6 +512,7 @@ module Discorb
511
512
  end
512
513
  end
513
514
 
515
+ # @private
514
516
  def send_complete_result(val)
515
517
  @client.http.post("/interactions/#{@id}/#{@token}/callback", {
516
518
  type: 8,
data/lib/discorb/log.rb CHANGED
@@ -65,10 +65,11 @@ module Discorb
65
65
  return
66
66
  end
67
67
 
68
+ time = Time.now.iso8601
68
69
  if @colorize_log
69
- @out.puts("\e[2;90m[#{Time.now.iso8601}] #{color}#{name}\e[m -- #{message}")
70
+ @out.puts("\e[90m#{time}\e[0m #{color}#{name.ljust(5)}\e[0m #{message}")
70
71
  else
71
- @out.puts("[#{Time.now.iso8601}] #{name} -- #{message}")
72
+ @out.puts("#{time} #{name.ljust(5)} #{message}")
72
73
  end
73
74
  end
74
75
  end
@@ -24,7 +24,7 @@ module Discorb
24
24
  return if path.start_with?("https://")
25
25
 
26
26
  if @global
27
- time = b[:reset_at] - Time.now.to_i
27
+ time = b[:reset_at] - Time.now.to_f
28
28
  @client.log.info("global rate limit reached, waiting #{time} seconds")
29
29
  sleep(time)
30
30
  @global = false
@@ -54,7 +54,7 @@ module Discorb
54
54
  #
55
55
  def save(method, path, resp)
56
56
  if resp["X-Ratelimit-Global"] == "true"
57
- @global = Time.now.to_i + JSON.parse(resp.body, symbolize_names: true)[:retry_after]
57
+ @global = Time.now.to_f + JSON.parse(resp.body, symbolize_names: true)[:retry_after]
58
58
  end
59
59
  return unless resp["X-RateLimit-Remaining"]
60
60
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discorb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - sevenc-nanashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-14 00:00:00.000000000 Z
11
+ date: 2021-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
@@ -79,6 +79,7 @@ files:
79
79
  - ".github/ISSUE_TEMPLATE/feature_request.md"
80
80
  - ".github/workflows/build_main.yml"
81
81
  - ".github/workflows/build_version.yml"
82
+ - ".github/workflows/package_register.yml"
82
83
  - ".gitignore"
83
84
  - ".yardopts"
84
85
  - Changelog.md
@@ -103,8 +104,8 @@ files:
103
104
  - docs/assets/08_hello_once.png
104
105
  - docs/assets/unused_ping_pong.png
105
106
  - docs/cli.md
106
- - docs/cli/init.md
107
107
  - docs/cli/irb.md
108
+ - docs/cli/new.md
108
109
  - docs/cli/run.md
109
110
  - docs/cli/setup.md
110
111
  - docs/events.md
@@ -191,7 +192,6 @@ homepage: https://github.com/discorb-lib/discorb
191
192
  licenses:
192
193
  - MIT
193
194
  metadata:
194
- allowed_push_host: https://rubygems.org
195
195
  homepage_uri: https://github.com/discorb-lib/discorb
196
196
  source_code_uri: https://github.com/discorb-lib/discorb
197
197
  changelog_uri: https://discorb-lib.github.io/file.Changelog.html