discorb 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/docs/cli.md CHANGED
@@ -18,4 +18,9 @@ Currently, discorb has the following commands:
18
18
  |---------|-------------|
19
19
  | {file:docs/cli/init.md `init`} | Create a new project. |
20
20
  | {file:docs/cli/irb.md `irb`} | Start an interactive Ruby shell with connected client. |
21
- | `show` | Show your environment. |
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
+ | `about` | Show discorb's information. (No document) |
25
+
26
+ Click the command name to see the document.
data/docs/tutorial.md ADDED
@@ -0,0 +1,194 @@
1
+ # @title Tutorial
2
+
3
+ # Tutorial
4
+
5
+ Welcome to discorb! This lib allows you to create a discord bot with ease. So, let's get started!
6
+
7
+ ## Requirements
8
+
9
+ - Ruby 3.0.0+
10
+ - Basic knowledge of ruby
11
+ These documents will help you:
12
+ - [Ruby in Twenty Minutes](https://www.ruby-lang.org/en/documentation/quickstart/)
13
+ - [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
14
+ - [Try ruby!](https://try.ruby-lang.org/)
15
+
16
+ ### Recommended
17
+
18
+ - Good editor
19
+ They are recommended:
20
+ - [VSCode](https://code.visualstudio.com/)
21
+ - [Atom](https://atom.io/)
22
+ - [Sublime Text](https://www.sublimetext.com/)
23
+ - [Brackets](https://brackets.io/)
24
+ - [Notepad++](https://notepad-plus-plus.org/)
25
+ - Git
26
+ - Bundler
27
+
28
+ Once you have all of these, you can start coding!
29
+
30
+ ## Start creating your bot
31
+
32
+ ### Create a Bot account
33
+
34
+ You must have a bot account to use this lib. First, go to [Discord Developer Portal](https://discord.com/developers/applications) and click on `New Application`.
35
+ ![](./assets/01_new_app.png)
36
+
37
+ And then type a name for your bot, and click `Create`.
38
+ ![](./assets/02_bot_name.png)
39
+
40
+ You will be redirected to the `General Information` page.
41
+ Then, click `Bot` and then `Add Bot` and then `Yes, do it!`.
42
+ ![](./assets/03_add_bot.png)
43
+
44
+ You will see bot information, and click `Copy` button in TOKEN section.
45
+ ![](./assets/04_token_copy.png)
46
+
47
+ **DO NOT SHARE THIS TOKEN, OR YOUR BOT BAN EVERYONE IN EVERY SERVER!**
48
+ This is serious security risk.
49
+
50
+ Click `Regenerate` button to regenerate your token. Do this immediately when you accidentally share your token.
51
+
52
+ You did it! Now, you have a bot account.
53
+
54
+ #### Invite your bot to a server
55
+
56
+ Go to `OAuth2` page and scroll down, and check `bot` and `applications.commands` permissions.
57
+ ![](./assets/05_oauth.png)
58
+ Then, click `Copy` button and paste it to your browser.
59
+ Choose a server you want to invite your bot to, and follow the instructions.
60
+
61
+ ### Code your bot
62
+
63
+ #### Install gems
64
+
65
+ Open terminal and type:
66
+
67
+ ```
68
+ gem install bundler discorb
69
+ ```
70
+
71
+ #### Setup files
72
+
73
+ Create a new directory and go to it.
74
+ Open terminal and type:
75
+
76
+ ```
77
+ discorb init
78
+ ```
79
+
80
+ Specify `--git` if you want to use git.
81
+
82
+ You will get some files in your directory.
83
+
84
+ - `main.rb`: The main file of your bot.
85
+ - `.env`: The environment variables of your bot. **You must keep this file secret!**
86
+ - `Gemfile`: Gemfile for bundler.
87
+ - `Gemfile.lock`: Gemfile.lock for bundler.
88
+
89
+ You will get other files if you specify `--git`.
90
+ {file:docs/cli/init.md Learn more here}.
91
+
92
+ #### Start your bot
93
+
94
+ Open `main.rb`, you will see the following code:
95
+
96
+ ```ruby
97
+ require "discorb"
98
+ require "dotenv"
99
+
100
+ Dotenv.load # Loads .env file
101
+
102
+ client = Discorb::Client.new # Create client for connecting to Discord
103
+
104
+ client.once :ready do
105
+ puts "Logged in as #{client.user}" # Prints username of logged in user
106
+ end
107
+
108
+ client.run ENV["TOKEN"] # Starts client
109
+ ```
110
+
111
+ Open `.env`, you will see:
112
+
113
+ ```
114
+ TOKEN=Y0urB0tT0k3nHer3.Th1sT0ken.W0ntWorkB3c4useItH4sM34n1ng
115
+ ```
116
+
117
+ Replace `Y0urB0tT0k3nHer3.Th1sT0ken.W0ntWorkB3c4useItH4sM34n1ng` with your bot token.
118
+ Remember to keep this file secret!
119
+
120
+ Open terminal and type:
121
+
122
+ ```sh
123
+ bundle exec ruby main.rb
124
+ # or
125
+ bundle exec discorb run main.rb
126
+ ```
127
+
128
+ Yay! Your bot is online!
129
+ ![](./assets/06_online.png)
130
+
131
+ But your bot won't do anything.
132
+ So add your bot some greetings!
133
+ `Ctrl + C` to stop your bot.
134
+
135
+ #### Add a greeting
136
+
137
+ You can do some action on message by typing like this:
138
+
139
+ ```ruby
140
+ client.on :message do |message|
141
+ # ...
142
+ end
143
+ ```
144
+
145
+ `message` is a {Discorb::Message} object. It contains information about the message.
146
+ You can get the message content by {Discorb::Message#content}.
147
+ Add `if` statement, and reply to the message with {Discorb::Message#reply}.
148
+
149
+ ```ruby
150
+ client.on :message do |message|
151
+ if message.content.downcase.include? "hello"
152
+ message.reply "Hello!"
153
+ end
154
+ end
155
+ ```
156
+
157
+ Save your bot and restart it.
158
+
159
+ You can see your bot's response by typing `hello` in your server...
160
+
161
+ ![](./assets/07_hello_infinite.png)
162
+
163
+ Oh no! Your bot is responding to bot's messages, and it doesn't stop!
164
+
165
+ Terminate your bot by typing `Ctrl + C` in terminal.
166
+
167
+ #### Ignore bot's messages
168
+
169
+ You can access author information by {Discorb::Message#author}, and it has {Discorb::User#bot?}.
170
+ So, you can ignore bot's messages by adding `if` statement:
171
+
172
+ ```ruby
173
+ client.on :message do |message|
174
+ next if message.author.bot?
175
+
176
+ # ...
177
+ end
178
+ ```
179
+
180
+ Note you must use `next` to exit the block.
181
+
182
+ Save your bot and start it.
183
+
184
+ ![](./assets/08_hello_once.png)
185
+
186
+ You did it! Your bot won't respond to bot's messages anymore.
187
+
188
+ ## Finally
189
+
190
+ This is the end of tutorial.
191
+
192
+ To learn more, check out the [documentation](https://discorb-lib.github.io/).
193
+
194
+ We hope you enjoy this lib! Thanks for reading!
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.1"
7
+ VERSION = "0.5.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
@@ -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,15 @@
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
+ "License" => "MIT License",
11
+ }
12
+
13
+ informations.each do |key, value|
14
+ puts "\e[90m#{key}:\e[m #{value}"
15
+ end
@@ -1,6 +1,7 @@
1
1
  # description: Make files for the discorb project.
2
2
 
3
3
  require "optparse"
4
+ require "discorb"
4
5
  require_relative "../utils/colored_puts"
5
6
 
6
7
  $path = Dir.pwd
@@ -11,15 +12,15 @@ FILES = {
11
12
  require "discorb"
12
13
  require "dotenv"
13
14
 
14
- Dotenv.load
15
+ Dotenv.load # Loads .env file
15
16
 
16
- client = Discorb::Client.new
17
+ client = Discorb::Client.new # Create client for connecting to Discord
17
18
 
18
19
  client.once :ready do
19
- puts "Logged in as #{client.user}"
20
+ puts "Logged in as #{client.user}" # Prints username of logged in user
20
21
  end
21
22
 
22
- client.run ENV["%<token>s"]
23
+ client.run ENV["%<token>s"] # Starts client
23
24
  RUBY
24
25
  ".env" => <<~BASH,
25
26
  %<token>s=Y0urB0tT0k3nHer3.Th1sT0ken.W0ntWorkB3c4useItH4sM34n1ng
@@ -90,9 +91,9 @@ FILES = {
90
91
 
91
92
  source "https://rubygems.org"
92
93
 
93
- git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
94
+ git_source(:github) { |repo_name| "https://github.com/\#{repo_name}" }
94
95
 
95
- gem "discorb", "~> 0.2.5"
96
+ gem "discorb", "~> #{Discorb::VERSION}"
96
97
  gem "dotenv", "~> 2.7"
97
98
  RUBY
98
99
  }
@@ -141,7 +142,7 @@ opt = OptionParser.new <<~BANNER
141
142
 
142
143
  $values = {
143
144
  bundle: true,
144
- git: true,
145
+ git: false,
145
146
  force: false,
146
147
  token: "TOKEN",
147
148
  }
@@ -150,7 +151,7 @@ opt.on("--[no-]bundle", "Whether to use bundle. Default to true.") do |v|
150
151
  $values[:bundle] = v
151
152
  end
152
153
 
153
- opt.on("--[no-]git", "Whether to initialize git. Default to true.") do |v|
154
+ opt.on("--[no-]git", "Whether to initialize git. Default to false.") do |v|
154
155
  $values[:git] = v
155
156
  end
156
157
 
@@ -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