discorb 0.7.6 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2d341757256d728ba6bb750b068a06f859e5d796d9e44bb1e4d616f4b439444
4
- data.tar.gz: 3d4f6aaf740f8366a7f714011f60688871a9b4698bce9a37b6fd4c35cd31ab10
3
+ metadata.gz: eb2e5dd94a2bda9644a1a5679fdab3006bbdb1b10c8bf3780764b7fc115aa002
4
+ data.tar.gz: 4bfd645c6e39a4f3bd48ed2b2afaaccc8c2cf5702eb6dc675cfb01b17667b09f
5
5
  SHA512:
6
- metadata.gz: dd2620d3de3cf9e1b024701a9312d63a0990ac5360c9b5e65d096cc3792dd81effb79b26577812e6ac229f155d53ccf4a8f5aa093103e56d05c677eb8904936b
7
- data.tar.gz: a04ab3bb0982d21d0d40d132b0b217ee4b95b214c848d970229e8f66bf0a4d5e2d07dce2006822ab07954f5a0de343a7769b1e36181778ccd501e5a02b6456fb
6
+ metadata.gz: 8c18b4e0b809e66350c952a8d50cb2e4f899e2844cd33b43bfd6120cc4560c6889e2cc8015fe9729ecb84c1af26a434ac566f69baa1a58cfcc5b6e2232b41151
7
+ data.tar.gz: de89179dffd53f575e148c2240d278eb101dde91f8f2288c9cefcae403eb4ce2f04547a030bebb3912d33df639e8e516c92878d6eb1fe1650cde8dc4c6903d25
@@ -0,0 +1,53 @@
1
+ name: Build YARD by version
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ push:
6
+ tags:
7
+ - "v*"
8
+ jobs:
9
+ main:
10
+
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ with:
16
+ fetch-depth: 0
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: 3.0.2
21
+ bundler-cache: true
22
+ - name: Set up git settings
23
+ run: |
24
+ git config --global user.email "action@github.com"
25
+ git config --global user.name "GitHub Action"
26
+ - name: Clone pages
27
+ env:
28
+ SSH_SECRET: ${{ secrets.SSH }}
29
+ GIT_SSH_COMMAND: ssh -i ~/ssh_secret
30
+ run: |
31
+ echo "$SSH_SECRET" > ~/ssh_secret
32
+ chmod 600 ~/ssh_secret
33
+ git clone git@github.com:discorb-lib/discorb-lib.github.io /tmp/pages
34
+ - name: Install dependencies
35
+ run: |
36
+ bundle config --local with 'docs'
37
+ bundle install
38
+ - name: Generate document
39
+ run: bundle exec rake document:build_all
40
+ - name: Push document
41
+ env:
42
+ SSH_SECRET: ${{ secrets.SSH }}
43
+ GIT_SSH_COMMAND: ssh -i ~/ssh_secret
44
+ run: |
45
+ echo "$SSH_SECRET" > ~/ssh_secret
46
+ chmod 600 ~/ssh_secret
47
+ cp -r ./doc/. /tmp/pages
48
+ cd /tmp/pages
49
+ git add -A
50
+ git commit -m "Update: Update document"
51
+ git update-ref -d refs/remotes/origin/user
52
+ git push origin main -f
53
+ continue-on-error: true
data/Changelog.md CHANGED
@@ -202,4 +202,13 @@ end
202
202
 
203
203
  ## 0.7.6
204
204
 
205
- - Fix: Fix heartbeating error
205
+ - Fix: Fix heartbeating error
206
+
207
+ ## 0.8.0
208
+
209
+ - Add: Add `Guild#fetch_members`
210
+ - Add: Add `Guild#fetch_member_list` as alias of `Guild#fetch_members`
211
+ - Add: Add `Intents#to_h`
212
+ - Add: Add `fetch_member` parameter to `Client#initialize`; Note you should set `false` if your bot doesn't have `GUILD_MEMBERS` intent
213
+ - Change: Change `ready` to `standby` event
214
+ - Change: `ready` will be fired when client receives `READY` event
data/README.md CHANGED
@@ -35,7 +35,7 @@ require "discorb"
35
35
 
36
36
  client = Discorb::Client.new
37
37
 
38
- client.once :ready do
38
+ client.once :standby do
39
39
  puts "Logged in as #{client.user}"
40
40
  end
41
41
 
data/docs/events.md CHANGED
@@ -22,7 +22,7 @@ Since v0.2.5, you can also register event handlers by adding a method to the cli
22
22
  client = Discorb::Client.new
23
23
 
24
24
  class << client
25
- def on_ready
25
+ def on_standby
26
26
  puts "Ready!"
27
27
  end
28
28
  end
@@ -43,7 +43,7 @@ end
43
43
  ```
44
44
 
45
45
  This example will print `Override!`, but not `This event handler is overrideable!`.
46
- This is useful for registering event handlers for default behaviour on errors.
46
+ This is useful for registering event handlers as default behaviour, such as error handlers.
47
47
 
48
48
  ```ruby
49
49
  # In the library...
@@ -55,7 +55,7 @@ end
55
55
  # In your code...
56
56
 
57
57
  client.on :command_error do |event, error|
58
- event.message.reply "An error occurred while executing that command!\n#{error.full_message}"
58
+ event.message.reply "An error occurred while executing the command!\n#{error.full_message}"
59
59
  end
60
60
  ```
61
61
 
@@ -73,7 +73,11 @@ Fires when a event is received.
73
73
 
74
74
  #### `ready()`
75
75
 
76
- Fires when the client is ready.
76
+ Fires when the client receives the `READY` event.
77
+
78
+ #### `standby()`
79
+
80
+ Fires when the client is standby. (When the client connects to Discord, and has cached guilds and members.)
77
81
 
78
82
  #### `resumed()`
79
83
 
data/docs/tutorial.md CHANGED
@@ -101,7 +101,7 @@ Dotenv.load # Loads .env file
101
101
 
102
102
  client = Discorb::Client.new # Create client for connecting to Discord
103
103
 
104
- client.once :ready do
104
+ client.once :standby do
105
105
  puts "Logged in as #{client.user}" # Prints username of logged in user
106
106
  end
107
107
 
@@ -3,7 +3,7 @@ require "json"
3
3
 
4
4
  client = Discorb::Client.new
5
5
 
6
- client.once :ready do
6
+ client.once :standby do
7
7
  puts "Logged in as #{client.user}"
8
8
  end
9
9
 
@@ -2,7 +2,7 @@ require "discorb"
2
2
 
3
3
  client = Discorb::Client.new
4
4
 
5
- client.once :ready do
5
+ client.once :standby do
6
6
  puts "Logged in as #{client.user}"
7
7
  end
8
8
 
@@ -8,7 +8,7 @@ def convert_role(guild, string)
8
8
  end
9
9
  end
10
10
 
11
- client.once :ready do
11
+ client.once :standby do
12
12
  puts "Logged in as #{client.user}"
13
13
  end
14
14
 
@@ -32,7 +32,7 @@ SECTIONS = [
32
32
 
33
33
  WIKIPEDIA_CREDIT = "(From: [Wikipedia](https://en.wikipedia.org/wiki/Ruby_(programming_language)))"
34
34
 
35
- client.once :ready do
35
+ client.once :standby do
36
36
  puts "Logged in as #{client.user}"
37
37
  end
38
38
 
@@ -3,7 +3,7 @@ require_relative "message_expander"
3
3
 
4
4
  client = Discorb::Client.new
5
5
 
6
- client.once :ready do
6
+ client.once :standby do
7
7
  puts "Logged in as #{client.user}"
8
8
  end
9
9
 
@@ -2,7 +2,7 @@ require "discorb"
2
2
 
3
3
  client = Discorb::Client.new
4
4
 
5
- client.once :ready do
5
+ client.once :standby do
6
6
  puts "Logged in as #{client.user}"
7
7
  end
8
8
 
@@ -2,7 +2,7 @@ require "discorb"
2
2
 
3
3
  client = Discorb::Client.new
4
4
 
5
- client.once :ready do
5
+ client.once :standby do
6
6
  puts "Logged in as #{client.user}"
7
7
  end
8
8
 
@@ -9,7 +9,7 @@ def convert_role(guild, string)
9
9
  end
10
10
  end
11
11
 
12
- client.once :ready do
12
+ client.once :standby do
13
13
  puts "Logged in as #{client.user}"
14
14
  end
15
15
 
@@ -2,7 +2,7 @@ require "discorb"
2
2
 
3
3
  client = Discorb::Client.new
4
4
 
5
- client.once :ready do
5
+ client.once :standby do
6
6
  puts "Logged in as #{client.user}"
7
7
  end
8
8
 
@@ -64,11 +64,13 @@ module Discorb
64
64
  # @param [Boolean] colorize_log Whether to colorize the log.
65
65
  # @param [:debug, :info, :warn, :error, :critical] log_level The log level.
66
66
  # @param [Boolean] wait_until_ready Whether to delay event dispatch until ready.
67
+ # @param [Boolean] fetch_member Whether to fetch member on ready. This may slow down the client.
68
+ # Defaultly, it's `false` if {Discorb::Intents#members} is false. Otherwise, it's `true`.
67
69
  #
68
70
  def initialize(
69
71
  allowed_mentions: nil, intents: nil, message_caches: 1000,
70
72
  log: nil, colorize_log: false, log_level: :info,
71
- wait_until_ready: true
73
+ wait_until_ready: true, fetch_member: nil
72
74
  )
73
75
  @allowed_mentions = allowed_mentions || AllowedMentions.new(everyone: true, roles: true, users: true)
74
76
  @intents = (intents or Intents.default)
@@ -91,6 +93,7 @@ module Discorb
91
93
  @commands = []
92
94
  @bottom_commands = []
93
95
  @status = :initialized
96
+ @fetch_member = fetch_member.nil? ? !(intents&.members) : fetch_member
94
97
  set_default_events
95
98
  end
96
99
 
@@ -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.7.6"
7
+ VERSION = "0.8.0"
8
8
  # @return [String] The user agent for the bot.
9
9
  USER_AGENT = "DiscordBot (https://github.com/discorb-lib/discorb #{VERSION}) Ruby/#{RUBY_VERSION}"
10
10
 
data/lib/discorb/error.rb CHANGED
@@ -92,10 +92,12 @@ module Discorb
92
92
  def initialize(resp, client)
93
93
  @client = client
94
94
  @client.close!
95
- DiscorbError.instance_method(:initialize).bind(self).call(<<~MESSAGE)
95
+ message = <<~MESSAGE
96
96
  The client is banned from CloudFlare.
97
97
  Hint: Try to decrease the number of requests per second, e.g. Use sleep in between requests.
98
98
  MESSAGE
99
+ $stderr.puts message
100
+ DiscorbError.instance_method(:initialize).bind(self).call(message)
99
101
  end
100
102
  end
101
103
 
@@ -16,7 +16,7 @@ FILES = {
16
16
 
17
17
  client = Discorb::Client.new # Create client for connecting to Discord
18
18
 
19
- client.once :ready do
19
+ client.once :standby do
20
20
  puts "Logged in as #{client.user}" # Prints username of logged in user
21
21
  end
22
22
 
@@ -23,7 +23,7 @@ opt.parse!(ARGV)
23
23
  client = Discorb::Client.new(intents: Discorb::Intents.from_value(intents_value))
24
24
  $messages = []
25
25
 
26
- client.on :ready do
26
+ client.on :standby do
27
27
  puts "\e[96mLogged in as #{client.user}\e[m"
28
28
 
29
29
  def message
data/lib/discorb/flag.rb CHANGED
@@ -8,6 +8,7 @@ module Discorb
8
8
  class Flag
9
9
  # @return [Hash{Symbol => Boolean}] the values of the flag.
10
10
  attr_reader :values
11
+ alias to_h values
11
12
  # @return [Integer] the value of the flag.
12
13
  attr_reader :value
13
14
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "async/http"
4
4
  require "async/websocket"
5
+ require "async/barrier"
6
+ require "async/semaphore"
5
7
  require "json"
6
8
  require "zlib"
7
9
 
@@ -610,19 +612,17 @@ module Discorb
610
612
  @user = ClientUser.new(self, data[:user])
611
613
  @uncached_guilds = data[:guilds].map { |g| g[:id] }
612
614
  if @uncached_guilds == [] or !@intents.guilds
613
- @ready = true
614
- dispatch(:ready)
615
- @log.info("Successfully connected to Discord.")
615
+ ready
616
616
  end
617
+ dispatch(:ready)
617
618
  @tasks << handle_heartbeat
618
619
  when "GUILD_CREATE"
619
620
  if @uncached_guilds.include?(data[:id])
620
621
  Guild.new(self, data, true)
621
622
  @uncached_guilds.delete(data[:id])
622
623
  if @uncached_guilds == []
623
- @ready = true
624
- dispatch(:ready)
625
- @log.info("Successfully connected to Discord, and cached all guilds.")
624
+ @log.debug "All guilds cached"
625
+ ready
626
626
  end
627
627
  elsif @guilds.has?(data[:id])
628
628
  @guilds[data[:id]].send(:_set_data, data)
@@ -1021,6 +1021,26 @@ module Discorb
1021
1021
  end
1022
1022
  end
1023
1023
  end
1024
+
1025
+ def ready
1026
+ Async do
1027
+ if @fetch_member
1028
+ @log.debug "Fetching members"
1029
+ barrier = Async::Barrier.new
1030
+ semaphore = Async::Semaphore.new(@guilds.length)
1031
+
1032
+ @guilds.each do |guild|
1033
+ semaphore.async(parent: barrier) do
1034
+ guild.fetch_members
1035
+ end
1036
+ end
1037
+ semaphore.__send__(:wait)
1038
+ end
1039
+ @ready = true
1040
+ dispatch(:standby)
1041
+ @log.info("Client is ready!")
1042
+ end
1043
+ end
1024
1044
  end
1025
1045
 
1026
1046
  #
data/lib/discorb/guild.rb CHANGED
@@ -502,7 +502,6 @@ module Discorb
502
502
  # Fetch a member in the guild.
503
503
  # @macro async
504
504
  # @macro http
505
- # @macro members_intent
506
505
  #
507
506
  # @param [#to_s] id The ID of the member to fetch.
508
507
  #
@@ -519,6 +518,39 @@ module Discorb
519
518
  end
520
519
  end
521
520
 
521
+ # Fetch members in the guild.
522
+ # @macro async
523
+ # @macro http
524
+ # @macro members_intent
525
+ #
526
+ # @param [Integer] limit The maximum number of members to fetch, 0 for all.
527
+ # @param [Integer] after The ID of the member to start fetching after.
528
+ #
529
+ # @return [Async::Task<Array<Discorb::Member>>] The list of members.
530
+ #
531
+ def fetch_members(limit: 0, after: nil)
532
+ Async do
533
+ unless limit == 0
534
+ _resp, data = @client.http.get("/guilds/#{@id}/members?#{URI.encode_www_form({ after: after, limit: limit })}").wait
535
+ next data[:members].map { |m| Member.new(@client, @id, m[:user], m) }
536
+ end
537
+ ret = []
538
+ after = 0
539
+ loop do
540
+ params = { after: after, limit: 100 }
541
+ _resp, data = @client.http.get("/guilds/#{@id}/members?#{URI.encode_www_form(params)}").wait
542
+ ret += data.map { |m| Member.new(@client, @id, m[:user], m) }
543
+ after = data.last[:user][:id]
544
+ if data.length != 1000
545
+ break
546
+ end
547
+ end
548
+ ret
549
+ end
550
+ end
551
+
552
+ alias fetch_member_list fetch_members
553
+
522
554
  #
523
555
  # Search for members by name in the guild.
524
556
  # @macro async
data/lib/discorb/http.rb CHANGED
@@ -242,12 +242,13 @@ module Discorb
242
242
  else
243
243
  API_BASE_URL + path
244
244
  end
245
- URI(full_path).path
245
+ uri = URI(full_path)
246
+ full_path.sub(uri.scheme + "://" + uri.host, "")
246
247
  end
247
248
 
248
249
  def get_response_data(resp)
249
- if resp["Via"].nil?
250
- raise CloudFlareBanError.new(@client, resp)
250
+ if resp["Via"].nil? && resp.code == "429"
251
+ raise CloudFlareBanError.new(resp, @client)
251
252
  end
252
253
  rd = resp.body
253
254
  if rd.nil? || rd.empty?
@@ -82,7 +82,7 @@ module Discorb
82
82
  if @raw_value.key?(name)
83
83
  @raw_value[name]
84
84
  elsif name.end_with?("=") && @raw_value.key?(name[0..-2].to_sym)
85
- raise ArgumentError, "true/false expected" if (!args.is_a? TrueClass) || args.is_a?(FalseClass)
85
+ raise ArgumentError, "true/false expected" unless args.is_a? TrueClass or args.is_a?(FalseClass)
86
86
 
87
87
  @raw_value[name[0..-2].to_sym] = args
88
88
  else
@@ -108,6 +108,10 @@ module Discorb
108
108
  "#<#{self.class} value=#{value}>"
109
109
  end
110
110
 
111
+ def to_h
112
+ @raw_value
113
+ end
114
+
111
115
  class << self
112
116
  # Create new intent object from raw value.
113
117
  # @param value [Integer] The value of the intent.
@@ -121,12 +125,12 @@ module Discorb
121
125
 
122
126
  # Create new intent object with default values.
123
127
  def default
124
- from_value(32_509)
128
+ from_value(32509)
125
129
  end
126
130
 
127
131
  # Create new intent object with all intents.
128
132
  def all
129
- from_value(32_767)
133
+ from_value(32767)
130
134
  end
131
135
 
132
136
  # Create new intent object with no intents.
data/po/yard.pot CHANGED
@@ -203,7 +203,7 @@ msgid "client = Discorb::Client.new"
203
203
  msgstr ""
204
204
 
205
205
  #: ../README.md:35
206
- msgid "client.once :ready do\n"
206
+ msgid "client.once :standby do\n"
207
207
  " puts \"Logged in as #{client.user}\"\n"
208
208
  "end"
209
209
  msgstr ""