discorb 0.3.0 → 0.4.2

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: 4f5c9c3f2c3e472300d1fe8462b9e667008bce0a30d11b2ce6cf866f697aedb0
4
- data.tar.gz: d2c2fa2edd51a84a1001675d0572bfae3464f5d25b85865a95a7c7f5daebf109
3
+ metadata.gz: 7322e6131e0e5d070df790e4a4b7ea6ea6ea7a286c975b0732c0c7eff2e9903d
4
+ data.tar.gz: 045ef3ab63628e0e0fe867de4aef8b1f872076cd74912b38e035c8cb6907c731
5
5
  SHA512:
6
- metadata.gz: ed4bb639464a3adc7a5982088149421d6d15f7e3c4be37bb59855a5d106ee5a1e1756b10ba401fd3d3ced9008c15e99bb3967f93eb7ff197903fb6cf95e19699
7
- data.tar.gz: fc6809db6b8e7a7e65e70fb49ce3b25a2aa4f42145477730e234511a95f278879759d76f5ee9547fa213f34ce3c413f5ba167c7f042d7f73a954718ba22e51d4
6
+ metadata.gz: f2b045e2b31b6a8fb95d74f87de931b30e67c8f34f647a0994b4feb4f3c3d8c270f2fddab03e995cce4eaf2f770d2698ae537e0e5d328152dc0244f3e6f5c59d
7
+ data.tar.gz: 3582014d604c85091c3c6180a13766ccab7dce8e0df0319278eb0d89e7cd0d0df2d236b3a58763cc55e5ccf80ef6c46317ad62028c30f0d89002bf48f1bc4b8b
@@ -0,0 +1,51 @@
1
+ name: Build YARD with main
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ schedule:
6
+ - cron: "*/8 * * * *"
7
+
8
+ jobs:
9
+ main:
10
+
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: 3.0.2
19
+ bundler-cache: true
20
+ - name: Set up git settings
21
+ run: |
22
+ git config --global user.email "action@github.com"
23
+ git config --global user.name "GitHub Action"
24
+ - name: Clone pages
25
+ env:
26
+ SSH_SECRET: ${{ secrets.SSH }}
27
+ GIT_SSH_COMMAND: ssh -i ~/ssh_secret
28
+ run: |
29
+ echo "$SSH_SECRET" > ~/ssh_secret
30
+ chmod 600 ~/ssh_secret
31
+ git clone git@github.com:discorb-lib/discorb-lib.github.io /tmp/pages
32
+ - name: Install dependencies
33
+ run: |
34
+ bundle config --local with 'docs'
35
+ bundle install
36
+ - name: Generate document
37
+ run: bundle exec rake document
38
+ - name: Push document
39
+ env:
40
+ SSH_SECRET: ${{ secrets.SSH }}
41
+ GIT_SSH_COMMAND: ssh -i ~/ssh_secret
42
+ run: |
43
+ echo "$SSH_SECRET" > ~/ssh_secret
44
+ chmod 600 ~/ssh_secret
45
+ cp -r ./doc/. /tmp/pages
46
+ cd /tmp/pages
47
+ git add -A
48
+ git commit -m "Update: Update document for ${{ github.sha }} by cron"
49
+ git update-ref -d refs/remotes/origin/user
50
+ git push origin main -f
51
+ continue-on-error: true
data/.gitignore CHANGED
@@ -35,7 +35,7 @@ build-iPhoneSimulator/
35
35
  ## Documentation cache and generated files:
36
36
  /.yardoc/
37
37
  /_yardoc/
38
- /doc/
38
+ # /doc/
39
39
  /rdoc/
40
40
 
41
41
  ## Environment normalization:
@@ -54,4 +54,8 @@ build-iPhoneSimulator/
54
54
 
55
55
  # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
56
  # .rubocop-https?--*
57
+
58
+ # For documentation generation.
57
59
  tmp-template-replace
60
+ tmp-doc
61
+ doc
data/Changelog.md CHANGED
@@ -73,4 +73,29 @@
73
73
  ## 0.2.5
74
74
 
75
75
  - Add: Add way to add event listener
76
- - Change: Move document to https://discorb-lib.github.io/
76
+ - Change: Move document to https://discorb-lib.github.io/
77
+
78
+ ## 0.3.0
79
+
80
+ - Add: Improve CLI tools
81
+ - Add: Add `discorb init`
82
+ - Change: Change `discord-irb` to `discorb irb`
83
+
84
+ ## 0.3.1
85
+
86
+ - Add: Add `discorb show`
87
+ - Fix: Fix documenting
88
+
89
+ ## 0.4.0
90
+
91
+ - Add: Add `discorb setup`
92
+ - Add: Add `discorb run`
93
+ - Add: Add realtime documentation
94
+
95
+ ## 0.4.1
96
+
97
+ - Add: Add `-s` option to `discorb run`
98
+
99
+ ## 0.4.2
100
+
101
+ - Fix: Fix error in `discorb run`
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- discorb (0.3.0)
5
- async
6
- async-http
7
- async-websocket
4
+ discorb (0.4.2)
5
+ async (~> 1.30.1)
6
+ async-http (~> 0.56.5)
7
+ async-websocket (~> 0.19.0)
8
8
  mime-types (~> 3.3)
9
9
 
10
10
  GEM
@@ -54,6 +54,7 @@ GEM
54
54
 
55
55
  PLATFORMS
56
56
  x86-mingw32
57
+ x86_64-linux
57
58
 
58
59
  DEPENDENCIES
59
60
  discorb!
data/Rakefile CHANGED
@@ -1,30 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
+ require_relative "lib/discorb/utils/colored_puts"
4
5
  task default: %i[]
5
6
 
7
+ # @!visibility private
6
8
  def get_version
7
9
  require_relative "lib/discorb/common"
8
- latest_commit = `git log --oneline`.force_encoding("utf-8").split("\n")[0]
9
- version = Discorb::VERSION
10
- unless latest_commit.downcase.include?("update version")
11
- version += "-dev"
10
+ tag = `git tag --points-at HEAD`.force_encoding("utf-8").strip
11
+ if tag.empty?
12
+ version = "main"
13
+ else
14
+ version = Discorb::VERSION
12
15
  end
13
16
  version
14
17
  end
15
18
 
16
- def gputs(text)
17
- puts "\e[90m#{text}\e[m"
18
- end
19
-
20
- def sputs(text)
21
- puts "\e[92m#{text}\e[m"
22
- end
23
-
19
+ desc "Build emoji_table.rb"
24
20
  task :emoji_table do
25
21
  require_relative "lib/discorb"
26
22
 
27
- gputs "Building emoji_table.rb"
23
+ iputs "Building emoji_table.rb"
28
24
  res = {}
29
25
  Discorb::EmojiTable::DISCORD_TO_UNICODE.each do |discord, unicode|
30
26
  res[unicode] ||= []
@@ -47,11 +43,12 @@ task :emoji_table do
47
43
  sputs "Successfully made emoji_table.rb"
48
44
  end
49
45
 
46
+ desc "Format files"
50
47
  task :format do
51
48
  Dir.glob("**/*.rb").each do |file|
52
49
  next if file.start_with?("vendor")
53
50
 
54
- gputs "Formatting #{file}"
51
+ iputs "Formatting #{file}"
55
52
  `rufo ./#{file}`
56
53
  content = ""
57
54
  File.open(file, "rb") do |f|
@@ -63,27 +60,37 @@ task :format do
63
60
  end
64
61
  end
65
62
  end
63
+
64
+ desc "Generate document and replace"
66
65
  namespace :document do
67
66
  version = get_version
67
+
68
+ desc "Just generate document"
68
69
  task :yard do
69
- sh "yardoc -o doc/#{version}"
70
+ sh "bundle exec yardoc -o doc/#{version}"
70
71
  end
72
+
73
+ desc "Replace files"
71
74
  namespace :replace do
72
75
  require "fileutils"
76
+
77
+ desc "Replace CSS"
73
78
  task :css do
74
- gputs "Replacing css"
79
+ iputs "Replacing css"
75
80
  Dir.glob("template-replace/files/**/*.*")
76
81
  .map { |f| f.delete_prefix("template-replace/files") }.each do |file|
77
82
  FileUtils.cp("template-replace/files" + file, "doc/#{version}/#{file}")
78
83
  end
79
84
  sputs "Successfully replaced css"
80
85
  end
86
+
87
+ desc "Replace HTML"
81
88
  task :html do
82
89
  require_relative "template-replace/scripts/sidebar.rb"
83
90
  require_relative "template-replace/scripts/version.rb"
84
91
  require_relative "template-replace/scripts/index.rb"
85
92
  require_relative "template-replace/scripts/yard_replace.rb"
86
- gputs "Resetting changes"
93
+ iputs "Resetting changes"
87
94
  Dir.glob("doc/#{version}/**/*.html") do |f|
88
95
  next if (m = f.match(/[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+)?/)) && m[0] != version
89
96
 
@@ -91,21 +98,23 @@ namespace :document do
91
98
  content.gsub!(/<!--od-->[\s\S]*<!--eod-->/, "")
92
99
  File.write(f, content)
93
100
  end
94
- gputs "Adding version tab"
101
+ iputs "Adding version tab"
95
102
  %w[file_list class_list method_list].each do |f|
96
103
  replace_sidebar("doc/#{version}/#{f}.html")
97
104
  end
98
105
 
99
- gputs "Building version tab"
100
- build_version_sidebar("doc/#{version}")
101
- gputs "Replacing _index.html"
106
+ iputs "Building version tab"
107
+ build_version_sidebar("doc/#{version}", version)
108
+ iputs "Replacing _index.html"
102
109
  replace_index("doc/#{version}", version)
103
- gputs "Replacing YARD credits"
110
+ iputs "Replacing YARD credits"
104
111
  yard_replace("doc/#{version}", version)
105
- gputs "Successfully replaced htmls"
112
+ iputs "Successfully replaced htmls"
106
113
  end
114
+
115
+ desc "Replace EOL"
107
116
  task :eol do
108
- gputs "Replacing CRLF with LF"
117
+ iputs "Replacing CRLF with LF"
109
118
  Dir.glob("doc/**/*.*") do |file|
110
119
  next unless File.file?(file)
111
120
 
@@ -122,20 +131,36 @@ namespace :document do
122
131
  end
123
132
  end
124
133
  task :replace => %i[replace:css replace:html replace:eol]
134
+
135
+ desc "Build all versions"
125
136
  task :build_all do
126
137
  require "fileutils"
127
- gputs "Building all versions"
138
+
139
+ class Bundler::Definition
140
+ def validate_platforms!
141
+ # noop
142
+ end
143
+ end
144
+
145
+ iputs "Building all versions"
146
+ FileUtils.rm_rf("doc") rescue nil
128
147
  FileUtils.cp_r("./template-replace/.", "./tmp-template-replace")
148
+ Rake::Task["document:yard"].execute
149
+ Rake::Task["document:replace:html"].execute
150
+ Rake::Task["document:replace:css"].execute
151
+ Rake::Task["document:replace:eol"].execute
129
152
  tags = `git tag`.force_encoding("utf-8").split("\n")
130
153
  tags.each do |tag|
131
154
  sh "git checkout #{tag} -f"
132
- gputs "Building #{tag}"
155
+ iputs "Building #{tag}"
133
156
  FileUtils.cp_r("./tmp-template-replace/.", "./template-replace")
134
157
  version = tag.delete_prefix("v")
135
158
  Rake::Task["document:yard"].execute
136
159
  Rake::Task["document:replace:html"].execute
137
160
  Rake::Task["document:replace:css"].execute
138
161
  Rake::Task["document:replace:eol"].execute
162
+ FileUtils.cp_r("./doc/.", "./tmp-doc")
163
+ FileUtils.rm_rf("doc")
139
164
  end
140
165
  version = "."
141
166
  Rake::Task["document:yard"].execute
@@ -143,11 +168,19 @@ namespace :document do
143
168
  Rake::Task["document:replace:css"].execute
144
169
  Rake::Task["document:replace:eol"].execute
145
170
  sh "git switch main -f"
171
+ FileUtils.cp_r("./tmp-doc/.", "./doc")
146
172
  sputs "Successfully built all versions"
173
+ rescue => e
174
+ sh "git switch main -f"
175
+ raise e
147
176
  end
177
+
178
+ desc "Push to discorb-lib/discorb-lib.github.io"
148
179
  task :push do
149
- gputs "Pushing documents"
180
+ iputs "Pushing documents"
150
181
  Dir.chdir("doc") do
182
+ sh "git init"
183
+ sh "git remote add origin git@github.com:discorb-lib/discorb-lib.github.io"
151
184
  sh "git add ."
152
185
  sh "git commit -m \"Update: Update document\""
153
186
  sh "git push -f"
data/discorb.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.metadata["homepage_uri"] = spec.homepage
19
19
  spec.metadata["source_code_uri"] = "https://github.com/discorb-lib/discorb"
20
- spec.metadata["changelog_uri"] = "https://github.com/discorb-lib/discorb/blob/main/Changelog.md"
20
+ spec.metadata["changelog_uri"] = "https://discorb-lib.github.io/file.Changelog.html"
21
21
  spec.metadata["documentation_uri"] = "https://discorb-lib.github.io"
22
22
 
23
23
  # Specify which files should be added to the gem when it is released.
@@ -30,9 +30,9 @@ Gem::Specification.new do |spec|
30
30
  spec.require_paths = ["lib"]
31
31
 
32
32
  # Uncomment to register a new dependency of your gem
33
- spec.add_dependency "async"
34
- spec.add_dependency "async-http"
35
- spec.add_dependency "async-websocket"
33
+ spec.add_dependency "async", "~> 1.30.1"
34
+ spec.add_dependency "async-http", "~> 0.56.5"
35
+ spec.add_dependency "async-websocket", "~> 0.19.0"
36
36
 
37
37
  spec.add_dependency "mime-types", "~> 3.3"
38
38
  # For more information and examples about making a new gem, checkout our
@@ -10,7 +10,13 @@ From: [Discord API docs](https://discord.com/developers/docs/interactions/applic
10
10
 
11
11
  ## How do I register an application command?
12
12
 
13
- Use {Discorb::Command::Handler.slash}, {Discorb::Command::Handler.slash_group} for slash commands, {Discorb::Command::Handler.user_command} for user menu commands, and {Discorb::Command::Handler.message_command} for message menu commands.
13
+ Write to a your script:
14
+ - {Discorb::Command::Handler.slash}, {Discorb::Command::Handler.slash_group} for slash commands,
15
+ - {Discorb::Command::Handler.user_command} for user menu commands,
16
+ - {Discorb::Command::Handler.message_command} for message menu commands.
17
+
18
+ And then run `discorb setup` to register your application commands.
19
+ {file:docs/cli/setup.md Learn more about `discorb setup`}.
14
20
 
15
21
  ### Note
16
22
 
data/docs/cli/irb.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # discorb irb
4
4
 
5
- discorb irb is a command line tool for interacting with the Discord API.
5
+ This command will start an interactive Ruby shell with connected client.
6
6
 
7
7
 
8
8
  ## Usage
data/docs/cli/run.md ADDED
@@ -0,0 +1,46 @@
1
+ # @title CLI: discorb run
2
+
3
+ # discorb run
4
+
5
+ This command will run a client.
6
+
7
+
8
+ ## Usage
9
+
10
+ ```
11
+ discorb run [options] [script]
12
+ ```
13
+
14
+ ### Arguments
15
+
16
+ #### `script`
17
+
18
+ The script to run. Defaults to `main.rb`.
19
+
20
+ #### `-d`, `--deamon`
21
+
22
+ Run the client in deamon mode.
23
+
24
+ #### `-l`, `--log-level`
25
+
26
+ Specify the log level.
27
+ Should be one of the following:
28
+
29
+ * `none`
30
+ * `debug`
31
+ * `info`
32
+ * `warn`
33
+ * `error`
34
+ * `fatal`
35
+
36
+ #### `-f`, `--log-file`
37
+
38
+ Specify the file to write logs to.
39
+ You can use `stdout` to write to the standard output, and `stderr` to write to the standard error.
40
+
41
+ #### `-c`, `--[no-]log-color`
42
+
43
+ Whether to colorize the log output.
44
+ If not specified, the default will be:
45
+ - `true` if the file to write logs to is `stdout` or `stderr`.
46
+ - `false` otherwise.
data/docs/cli/setup.md ADDED
@@ -0,0 +1,17 @@
1
+ # @title CLI: discorb setup
2
+
3
+ # discorb setup
4
+
5
+ This command will setup application commands.
6
+
7
+ ## Usage
8
+
9
+ ```
10
+ discorb setup [script]
11
+ ```
12
+
13
+ ### Arguments
14
+
15
+ #### `script`
16
+
17
+ The script to setup. Defaults to `main.rb`.
data/docs/cli.md CHANGED
@@ -17,4 +17,9 @@ Currently, discorb has the following commands:
17
17
  | Command | Description |
18
18
  |---------|-------------|
19
19
  | {file:docs/cli/init.md `init`} | Create a new project. |
20
- | {file:docs/cli/irb.md `irb`} | Start an interactive Ruby shell with connected client. |
20
+ | {file:docs/cli/irb.md `irb`} | Start an interactive Ruby shell with connected client. |
21
+ | {file:docs/cli/run.md `run`} | Run a client. |
22
+ | {file:docs/cli/setup.md `setup`} | Setup application commands. |
23
+ | `show` | Show your environment. (No document) |
24
+
25
+ Click the command name to see the document.
data/exe/discorb CHANGED
@@ -17,6 +17,9 @@ if script.nil?
17
17
  puts "\e[90m#{name.rjust(max_length)}\e[m - #{description}"
18
18
  end
19
19
 
20
+ puts "\e[94m\nTo run a tool, type:\e[m\n" +
21
+ "\e[34m discorb [script]\e[m"
22
+
20
23
  exit 1
21
24
  end
22
25
 
@@ -60,12 +60,11 @@ module Discorb
60
60
  # @param [Boolean] colorize_log Whether to colorize the log.
61
61
  # @param [:debug, :info, :warn, :error, :critical] log_level The log level.
62
62
  # @param [Boolean] wait_until_ready Whether to delay event dispatch until ready.
63
- # @param [Boolean] overwrite_application_commands Whether to overwrite application commands on ready.
64
63
  #
65
64
  def initialize(
66
65
  allowed_mentions: nil, intents: nil, message_caches: 1000,
67
66
  log: nil, colorize_log: false, log_level: :info,
68
- wait_until_ready: true, overwrite_application_commands: true
67
+ wait_until_ready: true
69
68
  )
70
69
  @allowed_mentions = allowed_mentions || AllowedMentions.new(everyone: true, roles: true, users: true)
71
70
  @intents = (intents or Intents.default)
@@ -86,7 +85,6 @@ module Discorb
86
85
  @tasks = []
87
86
  @conditions = {}
88
87
  @commands = []
89
- @overwrite_application_commands = overwrite_application_commands
90
88
  @status = :initialized
91
89
  end
92
90
 
@@ -384,11 +382,69 @@ module Discorb
384
382
 
385
383
  #
386
384
  # Starts the client.
385
+ # @note This method behavior will change by CLI.
386
+ # @see file:docs/cli.md
387
387
  #
388
388
  # @param [String] token The token to use.
389
389
  #
390
390
  def run(token)
391
+ case ENV["DISCORB_CLI_FLAG"]
392
+ when nil
393
+ start_client(token)
394
+ when "run"
395
+ require "json"
396
+ options = JSON.parse(ENV["DISCORB_CLI_OPTIONS"], symbolize_names: true)
397
+ Process.daemon if options[:daemon]
398
+ setup_commands(token) if options[:setup]
399
+ if options[:log_level]
400
+ if options[:log_level] == "none"
401
+ @log.out = nil
402
+ else
403
+ @log.out = case options[:log_file]
404
+ when nil, "stderr"
405
+ $stderr
406
+ when "stdout"
407
+ $stdout
408
+ else
409
+ ::File.open(options[:log_file], "a")
410
+ end
411
+ @log.level = options[:log_level].to_sym
412
+ @log.colorize_log = case options[:log_color]
413
+ when nil
414
+ if @log.out == $stdout || @log.out == $stderr
415
+ true
416
+ else
417
+ false
418
+ end
419
+ when true, false
420
+ options[:log_color]
421
+ end
422
+ end
423
+ end
424
+ start_client(token)
425
+ when "setup_command"
426
+ setup_commands(token)
427
+ end
428
+ end
429
+
430
+ #
431
+ # Stops the client.
432
+ #
433
+ def close!
434
+ @connection.send_close
435
+ @tasks.each(&:stop)
436
+ @status = :closed
437
+ @close_condition.signal
438
+ end
439
+
440
+ private
441
+
442
+ def start_client(token)
391
443
  Async do |task|
444
+ trap(:SIGINT) {
445
+ @log.info "SIGINT received, closing..."
446
+ close!
447
+ }
392
448
  @token = token.to_s
393
449
  @close_condition = Async::Condition.new
394
450
  main_task = Async do
@@ -403,15 +459,5 @@ module Discorb
403
459
  main_task.stop
404
460
  end
405
461
  end
406
-
407
- #
408
- # Stops the client.
409
- #
410
- def close!
411
- @connection.send_close
412
- @tasks.each(&:stop)
413
- @status = :closed
414
- @close_condition.signal
415
- end
416
462
  end
417
463
  end
@@ -92,7 +92,6 @@ module Discorb
92
92
 
93
93
  #
94
94
  # Setup commands.
95
- # @note This method is called automatically if overwrite_application_commands is set to true.
96
95
  # @see Client#initialize
97
96
  #
98
97
  # @param [String] token Bot token.
@@ -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.3.0"
7
+ VERSION = "0.4.2"
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
@@ -35,6 +35,7 @@ module Discorb
35
35
 
36
36
  #
37
37
  # Represents a HTTP error.
38
+ # @abstract
38
39
  #
39
40
  class HTTPError < DiscorbError
40
41
  # @return [String] the HTTP response code.
@@ -78,6 +79,20 @@ module Discorb
78
79
  class NotFoundError < HTTPError
79
80
  end
80
81
 
82
+ #
83
+ # Represents a error because of a cloudflare ban.
84
+ #
85
+ class CloudFlareBanError < HTTPError
86
+ def initialize(resp, client)
87
+ @client = client
88
+ @client.close!
89
+ DiscorbError.instance_method(:initialize).bind(self).call(<<~MESSAGE)
90
+ The client is banned from CloudFlare.
91
+ Hint: Try to increase the number of requests per second, e.g. Use sleep in between requests.
92
+ MESSAGE
93
+ end
94
+ end
95
+
81
96
  #
82
97
  # Represents a error in client-side.
83
98
  #
@@ -0,0 +1,14 @@
1
+ # description: Show information of discorb.
2
+
3
+ puts " disco\e[31mrb\e[m - A new Discord API wrapper in Ruby. \n\n"
4
+
5
+ informations = {
6
+ "GitHub" => "https://github.com/discorb-lib/discorb",
7
+ "Documentation" => "https://discorb-lib.github.io",
8
+ "RubyGems" => "https://rubygems.org/gems/discorb",
9
+ "Changelog" => "https://discorb-lib.github.io/file.Changelog.html",
10
+ }
11
+
12
+ informations.each do |key, value|
13
+ puts "\e[90m#{key}:\e[m #{value}"
14
+ end
@@ -5,6 +5,7 @@ require_relative "../utils/colored_puts"
5
5
 
6
6
  $path = Dir.pwd
7
7
 
8
+ # @!visibility private
8
9
  FILES = {
9
10
  "main.rb" => <<~'RUBY',
10
11
  require "discorb"
@@ -84,19 +85,7 @@ FILES = {
84
85
  # This gitignore is from github/gitignore.
85
86
  # https://github.com/github/gitignore/blob/master/Ruby.gitignore
86
87
  GITIGNORE
87
- }
88
-
89
- def make_files
90
- iputs "Making files..."
91
- FILES.each do |file, content|
92
- File.write($path + "/#{file}", format(content, token: $values[:token]), mode: "wb")
93
- end
94
- sputs "Made files.\n"
95
- end
96
-
97
- def bundle_init
98
- iputs "Initializing bundle..."
99
- File.write($path + "/Gemfile", <<~'RUBY', mode: "wb")
88
+ "Gemfile" => <<~RUBY,
100
89
  # frozen_string_literal: true
101
90
 
102
91
  source "https://rubygems.org"
@@ -106,12 +95,33 @@ def bundle_init
106
95
  gem "discorb", "~> 0.2.5"
107
96
  gem "dotenv", "~> 2.7"
108
97
  RUBY
98
+ }
99
+
100
+ # @!visibility private
101
+ def create_file(name)
102
+ File.write($path + "/#{name}", format(FILES[name], token: $values[:token]), mode: "wb")
103
+ end
104
+
105
+ # @!visibility private
106
+ def make_files
107
+ iputs "Making files..."
108
+ create_file("main.rb")
109
+ create_file(".env")
110
+ sputs "Made files.\n"
111
+ end
112
+
113
+ # @!visibility private
114
+ def bundle_init
115
+ iputs "Initializing bundle..."
116
+ create_file("Gemfile")
109
117
  iputs "Installing gems..."
110
118
  system "bundle install"
111
119
  sputs "Installed gems.\n"
112
120
  end
113
121
 
122
+ # @!visibility private
114
123
  def git_init
124
+ create_file(".gitignore")
115
125
  iputs "Initializing git repository..."
116
126
  system "git init"
117
127
  system "git add ."
@@ -9,10 +9,16 @@ require "optparse"
9
9
  intents_value = Discorb::Intents.all.value
10
10
  token_file = "token"
11
11
 
12
- opt = OptionParser.new
12
+ ARGV.delete_at 0
13
+
14
+ opt = OptionParser.new <<~BANNER
15
+ This command will start an interactive Ruby shell with connected client.
16
+
17
+ Usage: discorb irb [options]
18
+ BANNER
13
19
  opt.on("-i", "--intents", "intents to use, default to all") { |v| intents_value = v }
14
20
  opt.on("-t", "--token-file", "token file to load, default to \"token\"") { |v| token_file = v }
15
- opt.parse(ARGV)
21
+ opt.parse!(ARGV)
16
22
 
17
23
  client = Discorb::Client.new(intents: Discorb::Intents.from_value(intents_value))
18
24
  $messages = []
@@ -30,7 +36,7 @@ client.on :ready do
30
36
  This is a debug client for Discord.
31
37
  \e[90mmessage\e[m to get latest message.
32
38
 
33
- \e[36mhttps://rubydoc.info/gems/discorb/file/docs/discord_irb.md\e[m for more information.
39
+ \e[36mhttps://discorb-lib.github.io/#{Discorb::VERSION}/file.irb.html\e[m for more information.
34
40
  EOS
35
41
  end
36
42
 
@@ -0,0 +1,49 @@
1
+ # description: Run a client.
2
+ require "optparse"
3
+ require "json"
4
+ require "discorb/utils/colored_puts"
5
+
6
+ ARGV.delete_at 0
7
+ # @!visibility private
8
+ LOG_LEVELS = %w[none debug info warn error fatal]
9
+
10
+ opt = OptionParser.new <<~BANNER
11
+ This command will run a client.
12
+
13
+ Usage: discorb run [options] [script]
14
+
15
+ script The script to run. Defaults to 'main.rb'.
16
+ BANNER
17
+ options = {
18
+ deamon: false,
19
+ log_level: nil,
20
+ log_file: nil,
21
+ log_color: nil,
22
+ setup: nil,
23
+ }
24
+ opt.on("-d", "--deamon", "Run as a daemon.") { |v| options[:daemon] = v }
25
+ opt.on("-l", "--log-level LEVEL", "Log level.") do |v|
26
+ unless LOG_LEVELS.include? v.downcase
27
+ eputs "Invalid log level: \e[31m#{v}\e[91m"
28
+ eputs "Valid log levels: \e[31m#{LOG_LEVELS.join("\e[91m, \e[31m")}\e[91m"
29
+ exit 1
30
+ end
31
+ options[:log_level] = v.downcase
32
+ end
33
+ opt.on("-f", "--log-file FILE", "File to write log to.") { |v| options[:log_file] = v }
34
+ opt.on("-c", "--[no-]log-color", "Whether to colorize log output.") { |v| options[:log_color] = v }
35
+ opt.on("-s", "--setup", "Whether to setup application commands.") { |v| options[:setup] = v }
36
+ opt.parse!(ARGV)
37
+
38
+ script = ARGV[0]
39
+
40
+ script ||= "main.rb"
41
+
42
+ ENV["DISCORB_CLI_FLAG"] = "run"
43
+ ENV["DISCORB_CLI_OPTIONS"] = JSON.generate(options)
44
+
45
+ begin
46
+ load script
47
+ rescue LoadError
48
+ eputs "Could not load script: \e[31m#{script}\e[91m"
49
+ end
@@ -0,0 +1,26 @@
1
+ # description: Setup application commands.
2
+ require "optparse"
3
+ require "discorb/utils/colored_puts"
4
+
5
+ ARGV.delete_at 0
6
+
7
+ opt = OptionParser.new <<~BANNER
8
+ This command will setup application commands.
9
+
10
+ Usage: discorb setup [script]
11
+
12
+ script The script to setup.
13
+ BANNER
14
+ opt.parse!(ARGV)
15
+
16
+ script = ARGV[0]
17
+ script ||= "main.rb"
18
+ ENV["DISCORB_CLI_FLAG"] = "setup"
19
+
20
+ begin
21
+ load script
22
+ rescue LoadError
23
+ eputs "Could not load script: \e[31m#{script}\e[m"
24
+ else
25
+ sputs "Successfully set up commands for \e[32m#{script}\e[m."
26
+ end
@@ -0,0 +1,8 @@
1
+ # description: Show information of your environment.
2
+
3
+ require "etc"
4
+ require "discorb"
5
+ puts "\e[90mRuby:\e[m #{RUBY_VERSION}"
6
+ puts "\e[90mdiscorb:\e[m #{Discorb::VERSION}"
7
+ uname = Etc.uname
8
+ puts "\e[90mSystem:\e[m #{uname[:sysname]} #{uname[:release]}"
@@ -574,7 +574,6 @@ module Discorb
574
574
  @uncached_guilds.delete(data[:id])
575
575
  if @uncached_guilds == []
576
576
  @ready = true
577
- setup_commands.wait if @overwrite_application_commands
578
577
  dispatch(:ready)
579
578
  @log.info("Guilds were cached")
580
579
  end
data/lib/discorb/http.rb CHANGED
@@ -32,12 +32,7 @@ module Discorb
32
32
  def get(path, headers: nil, audit_log_reason: nil, **kwargs)
33
33
  Async do |task|
34
34
  resp = http.get(get_path(path), get_headers(headers, "", audit_log_reason), **kwargs)
35
- rd = resp.body
36
- data = if rd.nil? || rd.empty?
37
- nil
38
- else
39
- JSON.parse(rd, symbolize_names: true)
40
- end
35
+ data = get_response_data(resp)
41
36
  test_error(if resp.code == "429"
42
37
  @client.log.warn "Ratelimit exceeded for #{path}, trying again in #{data[:retry_after]} seconds."
43
38
  task.sleep(data[:retry_after])
@@ -66,12 +61,7 @@ module Discorb
66
61
  def post(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
67
62
  Async do |task|
68
63
  resp = http.post(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs)
69
- rd = resp.body
70
- data = if rd.nil? || rd.empty?
71
- nil
72
- else
73
- JSON.parse(rd, symbolize_names: true)
74
- end
64
+ data = get_response_data(resp)
75
65
  test_error(if resp.code == "429"
76
66
  task.sleep(data[:retry_after])
77
67
  post(path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
@@ -132,12 +122,7 @@ module Discorb
132
122
  def put(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
133
123
  Async do |task|
134
124
  resp = http.put(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs)
135
- rd = resp.body
136
- data = if rd.nil? || rd.empty?
137
- nil
138
- else
139
- JSON.parse(rd, symbolize_names: true)
140
- end
125
+ data = get_response_data(resp)
141
126
  test_error(if resp.code == "429"
142
127
  task.sleep(data[:retry_after])
143
128
  put(path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
@@ -164,12 +149,7 @@ module Discorb
164
149
  def delete(path, headers: nil, audit_log_reason: nil, **kwargs)
165
150
  Async do |task|
166
151
  resp = http.delete(get_path(path), get_headers(headers, "", audit_log_reason))
167
- rd = resp.body
168
- data = if rd.nil? || rd.empty?
169
- nil
170
- else
171
- JSON.parse(rd, symbolize_names: true)
172
- end
152
+ data = get_response_data(resp)
173
153
  test_error(if resp.code == "429"
174
154
  task.sleep(data[:retry_after])
175
155
  delete(path, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
@@ -257,6 +237,18 @@ module Discorb
257
237
  URI(full_path).path
258
238
  end
259
239
 
240
+ def get_response_data(resp)
241
+ if resp["Via"].nil?
242
+ raise CloudFlareBanError.new(@client, resp)
243
+ end
244
+ rd = resp.body
245
+ if rd.nil? || rd.empty?
246
+ nil
247
+ else
248
+ JSON.parse(rd, symbolize_names: true)
249
+ end
250
+ end
251
+
260
252
  def http
261
253
  https = Net::HTTP.new("discord.com", 443)
262
254
  https.use_ssl = true
data/lib/discorb/log.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Discorb
4
4
  # @!visibility private
5
5
  class Logger
6
- attr_reader :out, :colorize_log
6
+ attr_accessor :out, :colorize_log
7
7
 
8
8
  @levels = %i[debug info warn error fatal].freeze
9
9
 
@@ -14,11 +14,12 @@ module Discorb
14
14
  end
15
15
 
16
16
  def level
17
- @levels[@level]
17
+ self.class.levels[@level]
18
18
  end
19
19
 
20
20
  def level=(level)
21
- @level = @levels.index(level)
21
+ @level = self.class.levels.index(level)
22
+ raise ArgumentError, "Invalid log level: #{level}" unless @level
22
23
  end
23
24
 
24
25
  def debug(message)
@@ -1,11 +1,14 @@
1
+ # @!visibility private
1
2
  def sputs(text)
2
3
  puts "\e[92m#{text}\e[m"
3
4
  end
4
5
 
6
+ # @!visibility private
5
7
  def eputs(text)
6
8
  puts "\e[91m#{text}\e[m"
7
9
  end
8
10
 
11
+ # @!visibility private
9
12
  def iputs(text)
10
13
  puts "\e[90m#{text}\e[m"
11
14
  end
@@ -216,8 +216,8 @@ tt {
216
216
  font-family: var(--monospace-font);
217
217
  }
218
218
 
219
- pre.code {
220
- color: var(--header-secondary);
219
+ pre.code code {
220
+ color: var(--header-secondary) !important;
221
221
  tab-size: 2;
222
222
  }
223
223
 
@@ -310,6 +310,11 @@ li.even {
310
310
  background: var(--background-secondary);
311
311
  }
312
312
 
313
+ li.current {
314
+ background: var(--background-tertiary);
315
+ }
316
+
317
+
313
318
  .item:hover {
314
319
  background: var(--background-secondary-alt);
315
320
  }
@@ -516,4 +521,10 @@ li.r2 {
516
521
  border-color: var(--background-tertiary);
517
522
  color: var(--interactive-active);
518
523
  fill: var(--interactive-active);
524
+ }
525
+
526
+ dd pre.code {
527
+ padding: 9px 14px;
528
+ border-radius: 4px;
529
+ border: 1px solid var(--background-tertiary);
519
530
  }
@@ -47,9 +47,9 @@
47
47
  <ul id="full_list" class="method">
48
48
  <!--replace-->
49
49
  <!--template-->
50
- <li class="!eo!">
50
+ <li class="!class!">
51
51
  <div class="item">
52
- <span class='object_link'><a href="../!version!/index.html" title="!version!">!version!</a></span>
52
+ <span class='object_link'><a href="!path!/index.html" title="!version!">!version!</a></span>
53
53
  <small class='git_sha'>!sha!</small>
54
54
  </div>
55
55
  </li>
@@ -1,12 +1,24 @@
1
- def build_version_sidebar(dir)
1
+ def build_version_sidebar(dir, version)
2
2
  raw = File.read("template-replace/resources/version_list.html")
3
3
  template = raw.match(/<!--template-->(.*)<!--endtemplate-->/m)[1]
4
4
  raw.gsub!(template, "")
5
5
  res = +""
6
- `git tag`.force_encoding("utf-8").split("\n").each.with_index do |tag, i|
6
+ i = 0
7
+ `git tag`.force_encoding("utf-8").split("\n").each.with_index do |tag|
8
+ i += 1
7
9
  sha = `git rev-parse #{tag}`.force_encoding("utf-8").strip
8
10
  version = tag.delete_prefix("v")
9
- res += template.gsub("!version!", version).gsub("!eo!", i % 2 == 0 ? "even" : "odd").gsub("!sha!", sha)
11
+ cls = i % 2 == 0 ? "even" : "odd"
12
+ if version == "."
13
+ cls += " current"
14
+ end
15
+ res += template.gsub("!version!", version).gsub("!path!", "../" + version).gsub("!class!", cls).gsub("!sha!", sha)
10
16
  end
17
+ i += 1
18
+ cls = i % 2 == 0 ? "even" : "odd"
19
+ if version == "main"
20
+ cls += " current"
21
+ end
22
+ res += template.gsub("!version!", "main").gsub("!path!", "../main").gsub("!class!", cls).gsub("!sha!", "(Latest on GitHub)")
11
23
  File.write(dir + "/version_list.html", raw.gsub("<!--replace-->", res))
12
24
  end
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discorb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.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-09-05 00:00:00.000000000 Z
11
+ date: 2021-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 1.30.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 1.30.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: async-http
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.56.5
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.56.5
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: async-websocket
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.19.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.19.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mime-types
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -74,6 +74,7 @@ executables:
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
+ - ".github/workflows/build_main.yml"
77
78
  - ".gitignore"
78
79
  - ".yardopts"
79
80
  - Changelog.md
@@ -91,6 +92,8 @@ files:
91
92
  - docs/cli.md
92
93
  - docs/cli/init.md
93
94
  - docs/cli/irb.md
95
+ - docs/cli/run.md
96
+ - docs/cli/setup.md
94
97
  - docs/events.md
95
98
  - docs/extension.md
96
99
  - docs/voice_events.md
@@ -122,8 +125,12 @@ files:
122
125
  - lib/discorb/emoji_table.rb
123
126
  - lib/discorb/error.rb
124
127
  - lib/discorb/event.rb
128
+ - lib/discorb/exe/about.rb
125
129
  - lib/discorb/exe/init.rb
126
130
  - lib/discorb/exe/irb.rb
131
+ - lib/discorb/exe/run.rb
132
+ - lib/discorb/exe/setup.rb
133
+ - lib/discorb/exe/show.rb
127
134
  - lib/discorb/extend.rb
128
135
  - lib/discorb/extension.rb
129
136
  - lib/discorb/file.rb
@@ -166,7 +173,7 @@ metadata:
166
173
  allowed_push_host: https://rubygems.org
167
174
  homepage_uri: https://github.com/discorb-lib/discorb
168
175
  source_code_uri: https://github.com/discorb-lib/discorb
169
- changelog_uri: https://github.com/discorb-lib/discorb/blob/main/Changelog.md
176
+ changelog_uri: https://discorb-lib.github.io/file.Changelog.html
170
177
  documentation_uri: https://discorb-lib.github.io
171
178
  post_install_message:
172
179
  rdoc_options: []