discorb 0.2.3 → 0.3.1

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.
@@ -1,24 +1,22 @@
1
- # @title discord-irb
1
+ # @title CLI: discorb irb
2
2
 
3
- # discord-irb
3
+ # discorb irb
4
4
 
5
- discord-irb is a command line tool for interacting with the Discord API.
5
+ discorb irb is a command line tool for interacting with the Discord API.
6
6
 
7
7
 
8
8
  ## Usage
9
9
 
10
10
  ```
11
- $ bundle exec discord-irb
11
+ discorb irb [options]
12
12
  ```
13
13
 
14
- To start.
15
-
16
14
  ### Load a token
17
15
 
18
- discord-irb will load a token from...
16
+ discorb irb will load a token from...
19
17
  1. the `DISCORD_BOT_TOKEN` environment variable
20
18
  2. the `DISCORD_TOKEN` environment variable
21
- 3. `token` file in the current directory(customizable with `-f` option)
19
+ 3. `token` file in the current directory(customizable with `-t` option)
22
20
  4. your input
23
21
 
24
22
  ### Arguments
data/docs/cli.md ADDED
@@ -0,0 +1,21 @@
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
+ | `show` | Show your environment. |
data/docs/events.md CHANGED
@@ -5,8 +5,8 @@
5
5
  ## How to use events
6
6
 
7
7
  discorb uses event driven programming.
8
- You can register event handlers with {Client#on}.
9
- Alternatively, you can use {Client#once} to register a one-time event handler.
8
+ You can register event handlers with {Discorb::Client#on}.
9
+ Alternatively, you can use {Discorb::Client#once} to register a one-time event handler.
10
10
 
11
11
  ```ruby
12
12
  client.on :message do |event|
@@ -16,6 +16,20 @@ end
16
16
 
17
17
  This example will print the content of every message received.
18
18
 
19
+ Since v0.2.5, you can also register event handlers by adding a method to the client, with the prefix `on_` and the event name as the method name.
20
+
21
+ ```ruby
22
+ client = Discorb::Client.new
23
+
24
+ class << client
25
+ def on_ready
26
+ puts "Ready!"
27
+ end
28
+ end
29
+ ```
30
+
31
+ If you want to seperate event handlers from the client, consider using {Discorb::Extension}. {file:docs/extension.md Learn more about extensions}.
32
+
19
33
  ## Event reference
20
34
 
21
35
  ### Client events
@@ -139,7 +153,7 @@ Fires when a webhook is updated.
139
153
 
140
154
  | Parameter | Type | Description |
141
155
  | ---------- | ----- | ----------- |
142
- |`event` | {Discorb::WebhooksUpdateEvent} | The webhook update event. |
156
+ |`event` | {Discorb::Gateway::WebhooksUpdateEvent} | The webhook update event. |
143
157
 
144
158
  #### `thread_new(thread)`
145
159
 
@@ -182,8 +196,8 @@ Fires when a thread's members are updated.
182
196
  | Parameter | Type | Description |
183
197
  | ---------- | ----- | ----------- |
184
198
  |`thread` | {Discorb::ThreadChannel} | The thread that the members were updated for. |
185
- |`added` | Array<{ThreadChannel::Member}> | An array of {Discorb::ThreadChannel::Member} objects that were added to the thread. |
186
- |`removed` | Array<{ThreadChannel::Member}> | An array of {Discorb::ThreadChannel::Member} objects that were removed from the thread. |
199
+ |`added` | Array<{Discorb::ThreadChannel::Member}> | An array of {Discorb::ThreadChannel::Member} objects that were added to the thread. |
200
+ |`removed` | Array<{Discorb::ThreadChannel::Member}> | An array of {Discorb::ThreadChannel::Member} objects that were removed from the thread. |
187
201
 
188
202
  #### `thread_member_update(before, after)`
189
203
 
@@ -258,7 +272,7 @@ This will fire when cached messages are deleted.
258
272
  #### `message_delete_id(message_id, channel, guild)`
259
273
 
260
274
  Fires when a message is deleted.
261
- Not like {#message_delete} this will fire even message is not cached.
275
+ Not like {file:#message_delete} this will fire even message is not cached.
262
276
 
263
277
  | Parameter | Type | Description |
264
278
  | ---------- | ----- | ----------- |
@@ -280,7 +294,7 @@ Fires when a message is pinned or unpinned.
280
294
 
281
295
  | Parameter | Type | Description |
282
296
  | ---------- | ----- | ----------- |
283
- |`event` | {Discorb::Gateway::MessagePinUpdateEvent}| The event object. |
297
+ |`event` | {Discorb::Gateway::MessagePinEvent}| The event object. |
284
298
 
285
299
  #### `typing_start(event)`
286
300
 
data/docs/extension.md CHANGED
@@ -18,7 +18,7 @@ end
18
18
 
19
19
  ## Register Event
20
20
 
21
- Use {Extension#event} to register event, or {Extension#once_event} to register event only once.
21
+ Use {Discorb::Extension.event} to register event, or {Discorb::Extension.once_event} to register event only once.
22
22
 
23
23
  ```ruby
24
24
  module MyExtension
@@ -36,11 +36,23 @@ end
36
36
 
37
37
  ## Load extension
38
38
 
39
- Use {Client#extend} to load extension.
39
+ Use {Discorb::Client#extend} to load extension.
40
+
41
+ ```ruby
42
+ module MyExtension
43
+ extend Discorb::Extension
44
+
45
+ event :message do |message|
46
+ # ...
47
+ end
48
+ end
49
+
50
+ client.extend MyExtension
51
+ ```
40
52
 
41
53
  ## Access Client from extension
42
54
 
43
- You can access {Client} from extension with `@client`.
55
+ You can access {Discorb::Client} from extension with `@client`.
44
56
 
45
57
  ```ruby
46
58
  module MyExtension
@@ -18,7 +18,7 @@ client.user_command("Info", guild_ids: [857373681096327180]) do |interaction|
18
18
  ],
19
19
  thumbnail: interaction.target.avatar&.url,
20
20
 
21
- ), hide: true)
21
+ ), ephemeral: true)
22
22
  end
23
23
 
24
24
  client.run(ENV["DISCORD_BOT_TOKEN"])
data/exe/discorb ADDED
@@ -0,0 +1,28 @@
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
+ exit 1
21
+ end
22
+
23
+ begin
24
+ require "discorb/exe/#{script}"
25
+ rescue LoadError
26
+ puts "\e[91mThis tool is not available: \e[90m#{script}\e[m"
27
+ exit 1
28
+ end
@@ -279,6 +279,9 @@ module Discorb
279
279
  @new_value = data[:new_value].then(&method)
280
280
  end
281
281
 
282
+ #
283
+ # Send a message to the new value.
284
+ #
282
285
  def method_missing(method, ...)
283
286
  @new_value.__send__(method, ...)
284
287
  end
@@ -155,15 +155,27 @@ module Discorb
155
155
  end
156
156
  end
157
157
  end
158
- if @events[event_name].nil?
158
+ events = @events[event_name].dup || []
159
+ if respond_to?("on_" + event_name.to_s)
160
+ event_method = method("on_" + event_name.to_s)
161
+ class << event_method
162
+ def id
163
+ "method"
164
+ end
165
+ end
166
+ events << event_method
167
+ end
168
+ if events.nil?
159
169
  @log.debug "Event #{event_name} doesn't have any proc, skipping"
160
170
  next
161
171
  end
162
172
  @log.debug "Dispatching event #{event_name}"
163
- @events[event_name].each do |block|
173
+ events.each do |block|
164
174
  lambda { |event_args|
165
175
  Async(annotation: "Discorb event: #{event_name}") do |task|
166
- @events[event_name].delete(block) if block.discriminator[:once]
176
+ if block.is_a?(Discorb::Event)
177
+ @events[event_name].delete(block) if block.discriminator[:once]
178
+ end
167
179
  block.call(*event_args)
168
180
  @log.debug "Dispatched proc with ID #{block.id.inspect}"
169
181
  rescue StandardError, ScriptError => e
data/lib/discorb/color.rb CHANGED
@@ -169,5 +169,6 @@ module Discorb
169
169
  end
170
170
  end
171
171
 
172
+ # @return [Class] The alias of `Discorb::Color`.
172
173
  Colour = Color
173
174
  end
@@ -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.2.3"
7
+ VERSION = "0.3.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
 
@@ -93,6 +93,9 @@ module Discorb
93
93
  !self[id].nil?
94
94
  end
95
95
 
96
+ #
97
+ # Send a message to the array of values.
98
+ #
96
99
  def method_missing(name, ...)
97
100
  if values.respond_to?(name)
98
101
  values.send(name, ...)
data/lib/discorb/embed.rb CHANGED
@@ -79,11 +79,11 @@ module Discorb
79
79
  end
80
80
 
81
81
  def image=(value)
82
- @image = value.is_a? String ? Image.new(value) : value
82
+ @image = (value.is_a?(String)) ? Image.new(value) : value
83
83
  end
84
84
 
85
85
  def thumbnail=(value)
86
- @thumbnail = value.is_a? String ? Thumbnail.new(value) : value
86
+ @thumbnail = (value.is_a?(String)) ? Thumbnail.new(value) : value
87
87
  end
88
88
 
89
89
  #
@@ -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."