discorb 0.2.5 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/build_main.yml +51 -0
- data/.gitignore +5 -1
- data/.yardopts +1 -1
- data/Changelog.md +22 -1
- data/Gemfile.lock +8 -7
- data/Rakefile +60 -27
- data/discorb.gemspec +4 -4
- data/docs/application_command.md +7 -1
- data/docs/cli/init.md +55 -0
- data/docs/{discord_irb.md → cli/irb.md} +5 -7
- data/docs/cli/run.md +46 -0
- data/docs/cli/setup.md +17 -0
- data/docs/cli.md +25 -0
- data/exe/discorb +31 -0
- data/lib/discorb/audit_logs.rb +3 -0
- data/lib/discorb/client.rb +59 -13
- data/lib/discorb/command.rb +0 -1
- data/lib/discorb/common.rb +1 -1
- data/lib/discorb/dictionary.rb +3 -0
- data/lib/discorb/error.rb +15 -0
- data/lib/discorb/exe/about.rb +14 -0
- data/lib/discorb/exe/init.rb +195 -0
- data/{exe/discord-irb → lib/discorb/exe/irb.rb} +11 -3
- data/lib/discorb/exe/run.rb +49 -0
- data/lib/discorb/exe/setup.rb +26 -0
- data/lib/discorb/exe/show.rb +8 -0
- data/lib/discorb/flag.rb +3 -0
- data/lib/discorb/gateway.rb +0 -1
- data/lib/discorb/http.rb +16 -24
- data/lib/discorb/intents.rb +3 -0
- data/lib/discorb/log.rb +4 -3
- data/lib/discorb/permission.rb +3 -0
- data/lib/discorb/utils/colored_puts.rb +14 -0
- data/template-replace/files/css/common.css +13 -2
- data/template-replace/resources/version_list.html +2 -2
- data/template-replace/scripts/version.rb +15 -3
- metadata +30 -18
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
data/docs/cli.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# @title CLI tools
|
2
|
+
|
3
|
+
# CLI tools
|
4
|
+
|
5
|
+
discorb has a CLI tool for developing.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```bash
|
10
|
+
bundle exec discorb <command> ...
|
11
|
+
```
|
12
|
+
|
13
|
+
## Commands
|
14
|
+
|
15
|
+
Currently, discorb has the following commands:
|
16
|
+
|
17
|
+
| Command | Description |
|
18
|
+
|---------|-------------|
|
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. |
|
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
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
script = ARGV[0]
|
4
|
+
|
5
|
+
if script.nil?
|
6
|
+
puts "\e[94mThis is a tool for discorb. Currently these tools are available:\e[m"
|
7
|
+
|
8
|
+
discorb_path = $:.find { |path| File.directory?(path + "/discorb") }
|
9
|
+
scripts = {}
|
10
|
+
Dir.glob(discorb_path + "/discorb/exe/*.rb") do |script|
|
11
|
+
name = File.basename(script, ".rb")
|
12
|
+
description = File.read(script).match(/# description: (.+)/)&.[](1) || "No description"
|
13
|
+
scripts[name] = description
|
14
|
+
end
|
15
|
+
max_length = scripts.keys.map { |key| key.length }.max
|
16
|
+
scripts.sort.each do |name, description|
|
17
|
+
puts "\e[90m#{name.rjust(max_length)}\e[m - #{description}"
|
18
|
+
end
|
19
|
+
|
20
|
+
puts "\e[94m\nTo run a tool, type:\e[m\n" +
|
21
|
+
"\e[34m discorb [script]\e[m"
|
22
|
+
|
23
|
+
exit 1
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require "discorb/exe/#{script}"
|
28
|
+
rescue LoadError
|
29
|
+
puts "\e[91mThis tool is not available: \e[90m#{script}\e[m"
|
30
|
+
exit 1
|
31
|
+
end
|
data/lib/discorb/audit_logs.rb
CHANGED
data/lib/discorb/client.rb
CHANGED
@@ -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
|
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
|
data/lib/discorb/command.rb
CHANGED
data/lib/discorb/common.rb
CHANGED
@@ -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
|
+
VERSION = "0.4.1"
|
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/dictionary.rb
CHANGED
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
|
@@ -0,0 +1,195 @@
|
|
1
|
+
# description: Make files for the discorb project.
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require_relative "../utils/colored_puts"
|
5
|
+
|
6
|
+
$path = Dir.pwd
|
7
|
+
|
8
|
+
# @!visibility private
|
9
|
+
FILES = {
|
10
|
+
"main.rb" => <<~'RUBY',
|
11
|
+
require "discorb"
|
12
|
+
require "dotenv"
|
13
|
+
|
14
|
+
Dotenv.load
|
15
|
+
|
16
|
+
client = Discorb::Client.new
|
17
|
+
|
18
|
+
client.once :ready do
|
19
|
+
puts "Logged in as #{client.user}"
|
20
|
+
end
|
21
|
+
|
22
|
+
client.run ENV["%<token>s"]
|
23
|
+
RUBY
|
24
|
+
".env" => <<~BASH,
|
25
|
+
%<token>s=Y0urB0tT0k3nHer3.Th1sT0ken.W0ntWorkB3c4useItH4sM34n1ng
|
26
|
+
BASH
|
27
|
+
".gitignore" => <<~GITIGNORE,
|
28
|
+
*.gem
|
29
|
+
*.rbc
|
30
|
+
/.config
|
31
|
+
/coverage/
|
32
|
+
/InstalledFiles
|
33
|
+
/pkg/
|
34
|
+
/spec/reports/
|
35
|
+
/spec/examples.txt
|
36
|
+
/test/tmp/
|
37
|
+
/test/version_tmp/
|
38
|
+
/tmp/
|
39
|
+
|
40
|
+
# Used by dotenv library to load environment variables.
|
41
|
+
.env
|
42
|
+
|
43
|
+
# Ignore Byebug command history file.
|
44
|
+
.byebug_history
|
45
|
+
|
46
|
+
## Specific to RubyMotion:
|
47
|
+
.dat*
|
48
|
+
.repl_history
|
49
|
+
build/
|
50
|
+
*.bridgesupport
|
51
|
+
build-iPhoneOS/
|
52
|
+
build-iPhoneSimulator/
|
53
|
+
|
54
|
+
## Specific to RubyMotion (use of CocoaPods):
|
55
|
+
#
|
56
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
57
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
58
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
59
|
+
#
|
60
|
+
# vendor/Pods/
|
61
|
+
|
62
|
+
## Documentation cache and generated files:
|
63
|
+
/.yardoc/
|
64
|
+
/_yardoc/
|
65
|
+
/doc/
|
66
|
+
/rdoc/
|
67
|
+
|
68
|
+
## Environment normalization:
|
69
|
+
/.bundle/
|
70
|
+
/vendor/bundle
|
71
|
+
/lib/bundler/man/
|
72
|
+
|
73
|
+
# for a library or gem, you might want to ignore these files since the code is
|
74
|
+
# intended to run in multiple environments; otherwise, check them in:
|
75
|
+
# Gemfile.lock
|
76
|
+
# .ruby-version
|
77
|
+
# .ruby-gemset
|
78
|
+
|
79
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
80
|
+
.rvmrc
|
81
|
+
|
82
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
83
|
+
# .rubocop-https?--*
|
84
|
+
|
85
|
+
# This gitignore is from github/gitignore.
|
86
|
+
# https://github.com/github/gitignore/blob/master/Ruby.gitignore
|
87
|
+
GITIGNORE
|
88
|
+
"Gemfile" => <<~RUBY,
|
89
|
+
# frozen_string_literal: true
|
90
|
+
|
91
|
+
source "https://rubygems.org"
|
92
|
+
|
93
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
94
|
+
|
95
|
+
gem "discorb", "~> 0.2.5"
|
96
|
+
gem "dotenv", "~> 2.7"
|
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")
|
117
|
+
iputs "Installing gems..."
|
118
|
+
system "bundle install"
|
119
|
+
sputs "Installed gems.\n"
|
120
|
+
end
|
121
|
+
|
122
|
+
# @!visibility private
|
123
|
+
def git_init
|
124
|
+
create_file(".gitignore")
|
125
|
+
iputs "Initializing git repository..."
|
126
|
+
system "git init"
|
127
|
+
system "git add ."
|
128
|
+
system "git commit -m \"Initial commit\""
|
129
|
+
sputs "Initialized repository, use " +
|
130
|
+
"\e[32mgit commit --amend -m '...'\e[92m" +
|
131
|
+
" to change commit message of initial commit.\n"
|
132
|
+
end
|
133
|
+
|
134
|
+
opt = OptionParser.new <<~BANNER
|
135
|
+
A tool to make a new project.
|
136
|
+
|
137
|
+
Usage: discorb init [options] [dir]
|
138
|
+
|
139
|
+
dir The directory to make the files in.
|
140
|
+
BANNER
|
141
|
+
|
142
|
+
$values = {
|
143
|
+
bundle: true,
|
144
|
+
git: true,
|
145
|
+
force: false,
|
146
|
+
token: "TOKEN",
|
147
|
+
}
|
148
|
+
|
149
|
+
opt.on("--[no-]bundle", "Whether to use bundle. Default to true.") do |v|
|
150
|
+
$values[:bundle] = v
|
151
|
+
end
|
152
|
+
|
153
|
+
opt.on("--[no-]git", "Whether to initialize git. Default to true.") do |v|
|
154
|
+
$values[:git] = v
|
155
|
+
end
|
156
|
+
|
157
|
+
opt.on("-t NAME", "--token NAME", "The name of token environment variable. Default to TOKEN.") do |v|
|
158
|
+
$values[:token] = v
|
159
|
+
end
|
160
|
+
|
161
|
+
opt.on("-f", "--force", "Whether to force use directory. Default to false.") do |v|
|
162
|
+
$values[:force] = v
|
163
|
+
end
|
164
|
+
|
165
|
+
ARGV.delete_at(0)
|
166
|
+
|
167
|
+
opt.parse!(ARGV)
|
168
|
+
|
169
|
+
if (dir = ARGV[0])
|
170
|
+
$path += "/#{dir}"
|
171
|
+
if Dir.exist?($path)
|
172
|
+
if Dir.empty?($path)
|
173
|
+
gputs "Found \e[30m#{dir}\e[90m and empty, using this directory."
|
174
|
+
else
|
175
|
+
if $values[:force]
|
176
|
+
gputs "Found \e[30m#{dir}\e[90m and not empty, but force is on, using this directory."
|
177
|
+
else
|
178
|
+
eputs "Directory \e[31m#{dir}\e[91m already exists and not empty. Use \e[31m-f\e[91m to force."
|
179
|
+
exit
|
180
|
+
end
|
181
|
+
end
|
182
|
+
else
|
183
|
+
Dir.mkdir($path)
|
184
|
+
gputs "Couldn't find \e[30m#{dir}\e[90m, created directory."
|
185
|
+
end
|
186
|
+
Dir.chdir($path)
|
187
|
+
end
|
188
|
+
|
189
|
+
bundle_init if $values[:bundle]
|
190
|
+
|
191
|
+
make_files
|
192
|
+
|
193
|
+
git_init if $values[:git]
|
194
|
+
|
195
|
+
sputs "\nSuccessfully made a new project at \e[32m#{$path}\e[92m."
|