discorb 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6281613f37f7da894dcdcb0df3070fd7c9efa685cf3a0412b6822fd60124cab9
4
- data.tar.gz: b584a8ef44f9f18758c18a8a065c003cac6f54beadb8c3c29081cac7c98fa416
3
+ metadata.gz: 6bdf66383e4ecb0161c962e9621861a07f49c515ce7754c8f7b630a80acbf009
4
+ data.tar.gz: 7035f001fd4ffb4523f9ca5b64c9cda440bd3d7e755174264c9f9cd74f8928a1
5
5
  SHA512:
6
- metadata.gz: 906cc3412b9cb6189947bf3446f3b9129b4665ec4247896f450f4e7bcbab3243764a246956180d5d9dc905ad86132fcadbea0659318d7fd47a30a47a4101f4f0
7
- data.tar.gz: 3615cb4feb58e8c5e7c24999a6e6af7596645f3dbebcaafe658c5c0b7c50a9a5aca467160560c316a2a7fa51ab79b6f214da0590d937f0ed2899d9ee79210346
6
+ metadata.gz: e8101e84ffd48fdf3c20cbef1fe31365f0f606e05d6a031fe38a8810dd51dc5abbccf86416268a07a74b1a57e8623c2c888c9077973f3f44c7e5b15c8fa3aa0c
7
+ data.tar.gz: 39b55c52514f0c80149f7a4453ea489bd1fb9baf440cfb6fcecd1956d6837d3a666566edeecf2da35e254eab73a7e5c279c225b9937ee2259c1b605b6b94b549
data/Gemfile CHANGED
@@ -8,7 +8,6 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "async"
11
- gem "async-http"
12
11
  gem "async-websocket"
13
12
 
14
13
  gem "mime-types", "~> 3.3"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- discorb (0.1.0)
4
+ discorb (0.2.0)
5
5
  async
6
6
  async-http
7
7
  async-websocket
@@ -57,7 +57,6 @@ PLATFORMS
57
57
 
58
58
  DEPENDENCIES
59
59
  async
60
- async-http
61
60
  async-websocket
62
61
  discorb!
63
62
  mime-types (~> 3.3)
@@ -0,0 +1,39 @@
1
+ # @title discord-irb
2
+
3
+ # discord-irb
4
+
5
+ discord-irb is a command line tool for interacting with the Discord API.
6
+
7
+
8
+ ## Usage
9
+
10
+ ```
11
+ $ bundle exec discord-irb
12
+ ```
13
+
14
+ To start.
15
+
16
+ ### Load a token
17
+
18
+ discord-irb will load a token from...
19
+ 1. the `DISCORD_BOT_TOKEN` environment variable
20
+ 2. the `DISCORD_TOKEN` environment variable
21
+ 3. `token` file in the current directory(customizable with `-f` option)
22
+ 4. your input
23
+
24
+ ### Arguments
25
+
26
+ #### `-i`, `--intents`
27
+
28
+ Intents to use.
29
+ Specify intents with integers.
30
+
31
+ #### `-t`, `--token-file`
32
+
33
+ Token file to load.
34
+
35
+ ### Variables
36
+
37
+ #### `message`
38
+
39
+ Last message received.
data/docs/events.md CHANGED
@@ -28,11 +28,11 @@ Fires when a event is received.
28
28
  |`event_name`| Symbol | The name of the event. |
29
29
  |`data` | Hash | The data of the event. |
30
30
 
31
- #### `ready(task)`
31
+ #### `ready()`
32
32
 
33
33
  Fires when the client is ready.
34
34
 
35
- #### `resumed(task)`
35
+ #### `resumed()`
36
36
 
37
37
  Fires when the client is resumed connection.
38
38
 
data/docs/extension.md CHANGED
@@ -37,3 +37,17 @@ end
37
37
  ## Load extension
38
38
 
39
39
  Use {Client#extend} to load extension.
40
+
41
+ ## Access Client from extension
42
+
43
+ You can access {Client} from extension with `@client`.
44
+
45
+ ```ruby
46
+ module MyExtension
47
+ extend Discorb::Extension
48
+
49
+ event :ready do |message|
50
+ puts "Logged in as #{client.user}"
51
+ end
52
+ end
53
+ ```
data/exe/discord-irb ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "io/console"
4
+ require "discorb"
5
+ require "optparse"
6
+
7
+ intents_value = Discorb::Intents.all.value
8
+ token_file = "token"
9
+
10
+ opt = OptionParser.new
11
+ opt.on("-i", "--intents", "intents to use, default to all") { |v| intents_value = v }
12
+ opt.on("-t", "--token-file", "token file to load, default to \"token\"") { |v| token_file = v }
13
+ opt.parse(ARGV)
14
+
15
+ client = Discorb::Client.new(intents: Discorb::Intents.from_value(intents_value))
16
+ $messages = []
17
+
18
+ client.on :ready do
19
+ puts "\e[96mLogged in as #{client.user}\e[m"
20
+
21
+ def message
22
+ $messages.last
23
+ end
24
+
25
+ def dirb_help
26
+ puts <<~EOS
27
+ \e[96mDiscord-IRB\e[m
28
+ This is a debug client for Discord.
29
+ \e[90mmessage\e[m to get latest message.
30
+
31
+ \e[36mhttps://rubydoc.info/gems/discorb/file/docs/discord_irb.md\e[m for more information.
32
+ EOS
33
+ end
34
+
35
+ puts <<~FIRST_MESSAGE
36
+ Running on \e[31mRuby #{RUBY_VERSION}\e[m, disco\e[31mrb #{Discorb::VERSION}\e[m
37
+ Type \e[90mdirb_help\e[m to help.
38
+ FIRST_MESSAGE
39
+
40
+ binding.irb
41
+
42
+ client.close!
43
+ end
44
+
45
+ client.on :message do |message|
46
+ $messages << message
47
+ end
48
+
49
+ token = ENV["DISCORD_BOT_TOKEN"] || ENV["DISCORD_TOKEN"]
50
+ if token.nil?
51
+ if File.exists?(token_file)
52
+ token = File.read(token_file)
53
+ else
54
+ print "\e[90mToken?\e[m : "
55
+ token = $stdin.noecho(&:gets).chomp
56
+ puts
57
+ end
58
+ end
59
+
60
+ client.run token
@@ -68,9 +68,9 @@ module Discorb
68
68
  end
69
69
 
70
70
  # @!visibility private
71
- def base_url
71
+ def channel_id
72
72
  Async do
73
- "/channels/#{@id}"
73
+ @id
74
74
  end
75
75
  end
76
76
 
@@ -1090,9 +1090,9 @@ module Discorb
1090
1090
  include Messageable
1091
1091
 
1092
1092
  # @!visibility private
1093
- def base_url
1093
+ def channel_id
1094
1094
  Async do
1095
- "/channels/#{@id}"
1095
+ @id
1096
1096
  end
1097
1097
  end
1098
1098
 
@@ -47,6 +47,8 @@ module Discorb
47
47
  # @note This will be calculated from heartbeat and heartbeat_ack.
48
48
  # @return [nil] If not connected to the gateway.
49
49
  attr_reader :ping
50
+ # @return [:initialized, :running, :closed] The status of the client.
51
+ attr_reader :status
50
52
 
51
53
  #
52
54
  # Initializes a new client.
@@ -85,6 +87,7 @@ module Discorb
85
87
  @conditions = {}
86
88
  @commands = []
87
89
  @overwrite_application_commands = overwrite_application_commands
90
+ @status = :initialized
88
91
  end
89
92
 
90
93
  #
@@ -177,16 +180,6 @@ module Discorb
177
180
  end
178
181
  end
179
182
 
180
- #
181
- # Starts the client.
182
- #
183
- # @param [String] token The token to use.
184
- #
185
- def run(token)
186
- @token = token.to_s
187
- connect_gateway(true)
188
- end
189
-
190
183
  #
191
184
  # Fetch user from ID.
192
185
  # @macro async
@@ -376,5 +369,37 @@ module Discorb
376
369
 
377
370
  include Discorb::Gateway::Handler
378
371
  include Discorb::Command::Handler
372
+
373
+ #
374
+ # Starts the client.
375
+ #
376
+ # @param [String] token The token to use.
377
+ #
378
+ def run(token)
379
+ Async do |task|
380
+ @token = token.to_s
381
+ @close_condition = Async::Condition.new
382
+ main_task = Async do
383
+ @status = :running
384
+ connect_gateway(true).wait
385
+ rescue
386
+ @status = :stopped
387
+ @close_condition.signal
388
+ raise
389
+ end
390
+ @close_condition.wait
391
+ main_task.stop
392
+ end
393
+ end
394
+
395
+ #
396
+ # Stops the client.
397
+ #
398
+ def close!
399
+ @connection.send_close
400
+ @tasks.each(&:stop)
401
+ @status = :closed
402
+ @close_condition.signal
403
+ end
379
404
  end
380
405
  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.1.0"
7
+ VERSION = "0.2.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
 
@@ -213,6 +213,33 @@ module Discorb
213
213
  !@updated_at.nil?
214
214
  end
215
215
 
216
+ #
217
+ # Edit the message.
218
+ #
219
+ # @param [String] content The message content.
220
+ # @param [Discorb::Embed] embed The embed to send.
221
+ # @param [Array<Discord::Embed>] embeds The embeds to send.
222
+ # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions.
223
+ # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
224
+ # @param [Boolean] supress Whether to supress embeds.
225
+ #
226
+ def edit(...)
227
+ Async do
228
+ channel.edit_message(@id, ...).wait
229
+ end
230
+ end
231
+
232
+ #
233
+ # Delete the message.
234
+ #
235
+ # @param [String] reason The reason for deleting the message.
236
+ #
237
+ def delete!(reason: nil)
238
+ Async do
239
+ channel.delete_message!(@id, reason: reason).wait
240
+ end
241
+ end
242
+
216
243
  #
217
244
  # Convert the message to reference object.
218
245
  #
@@ -7,6 +7,8 @@ module Discorb
7
7
  module Messageable
8
8
  #
9
9
  # Post a message to the channel.
10
+ # @macro async
11
+ # @macro http
10
12
  #
11
13
  # @param [String] content The message content.
12
14
  # @param [Boolean] tts Whether the message is tts.
@@ -61,13 +63,82 @@ module Discorb
61
63
  else
62
64
  headers = {}
63
65
  end
64
- _resp, data = @client.http.post("#{base_url.wait}/messages", payload, headers: headers).wait
66
+ _resp, data = @client.http.post("/channels/#{channel_id.wait}/messages", payload, headers: headers).wait
65
67
  Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
66
68
  end
67
69
  end
68
70
 
71
+ #
72
+ # Edit a message.
73
+ # @macro async
74
+ # @macro http
75
+ #
76
+ # @param [#to_s] message_id The message id.
77
+ # @param [String] content The message content.
78
+ # @param [Discorb::Embed] embed The embed to send.
79
+ # @param [Array<Discord::Embed>] embeds The embeds to send.
80
+ # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions.
81
+ # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
82
+ # @param [Boolean] supress Whether to supress embeds.
83
+ #
84
+ def edit_message(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil,
85
+ components: nil, supress: nil)
86
+ Async do
87
+ payload = {}
88
+ payload[:content] = content if content
89
+ tmp_embed = if embed
90
+ [embed]
91
+ elsif embeds
92
+ embeds
93
+ end
94
+ payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
95
+ payload[:allowed_mentions] =
96
+ allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash
97
+ if components
98
+ tmp_components = []
99
+ tmp_row = []
100
+ components.each do |c|
101
+ case c
102
+ when Array
103
+ tmp_components << tmp_row
104
+ tmp_row = []
105
+ tmp_components << c
106
+ when SelectMenu
107
+ tmp_components << tmp_row
108
+ tmp_row = []
109
+ tmp_components << [c]
110
+ else
111
+ tmp_row << c
112
+ end
113
+ end
114
+ tmp_components << tmp_row
115
+ payload[:flags] = (supress ? 1 << 2 : 0) unless flags.nil?
116
+ payload[:components] = tmp_components.filter { |c| c.length.positive? }.map { |c| { type: 1, components: c.map(&:to_hash) } }
117
+ end
118
+ @client.http.patch("/channels/#{channel_id.wait}/messages/#{message_id}", payload).wait
119
+ end
120
+ end
121
+
122
+ #
123
+ # Delete a message.
124
+ # @macro async
125
+ # @macro http
126
+ #
127
+ # @param [#to_s] message_id The message id.
128
+ # @param [String] reason The reason for deleting the message.
129
+ #
130
+ def delete_message!(message_id, reason: nil)
131
+ Async do
132
+ @client.http.delete("/channels/#{channel_id.wait}/messages/#{message_id}", reason: reason).wait
133
+ end
134
+ end
135
+
136
+ alias destroy_message! delete_message!
137
+
69
138
  #
70
139
  # Fetch a message from ID.
140
+ # @macro async
141
+ # @macro http
71
142
  #
72
143
  # @param [Discorb::Snowflake] id The ID of the message.
73
144
  #
@@ -76,13 +147,15 @@ module Discorb
76
147
  #
77
148
  def fetch_message(id)
78
149
  Async do
79
- _resp, data = @client.http.get("#{base_url.wait}/messages/#{id}").wait
150
+ _resp, data = @client.http.get("/channels/#{channel_id.wait}/messages/#{id}").wait
80
151
  Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
81
152
  end
82
153
  end
83
154
 
84
155
  #
85
156
  # Fetch a message history.
157
+ # @macro async
158
+ # @macro http
86
159
  #
87
160
  # @param [Integer] limit The number of messages to fetch.
88
161
  # @param [Discorb::Snowflake] before The ID of the message to fetch before.
@@ -99,7 +172,7 @@ module Discorb
99
172
  after: Discorb::Utils.try(around, :id),
100
173
  around: Discorb::Utils.try(before, :id),
101
174
  }.filter { |_k, v| !v.nil? }.to_h
102
- _resp, messages = @client.http.get("#{base_url.wait}/messages?#{URI.encode_www_form(params)}").wait
175
+ _resp, messages = @client.http.get("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}").wait
103
176
  messages.map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) }
104
177
  end
105
178
  end
data/lib/discorb/user.rb CHANGED
@@ -74,13 +74,13 @@ module Discorb
74
74
  alias app_owner? bot_owner?
75
75
 
76
76
  # @!visibility private
77
- def base_url
77
+ def channel_id
78
78
  Async do
79
79
  next @dm_channel_id if @dm_channel_id
80
80
 
81
81
  dm_channel = @client.http.post("/users/#{@id}/channels", { recipient_id: @client.user.id }).wait
82
82
  @dm_channel_id = dm_channel[:id]
83
- "/channels/#{@dm_channel_id}"
83
+ @dm_channel_id
84
84
  end
85
85
  end
86
86
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discorb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
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-08-31 00:00:00.000000000 Z
11
+ date: 2021-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
@@ -69,7 +69,8 @@ dependencies:
69
69
  description:
70
70
  email:
71
71
  - sevenc-nanashi@sevenbot.jp
72
- executables: []
72
+ executables:
73
+ - discord-irb
73
74
  extensions: []
74
75
  extra_rdoc_files: []
75
76
  files:
@@ -86,6 +87,7 @@ files:
86
87
  - discorb.gemspec
87
88
  - docs/Examples.md
88
89
  - docs/application_command.md
90
+ - docs/discord_irb.md
89
91
  - docs/events.md
90
92
  - docs/extension.md
91
93
  - docs/voice_events.md
@@ -100,6 +102,7 @@ files:
100
102
  - examples/simple/ping_pong.rb
101
103
  - examples/simple/rolepanel.rb
102
104
  - examples/simple/wait_for_message.rb
105
+ - exe/discord-irb
103
106
  - lib/discorb.rb
104
107
  - lib/discorb/application.rb
105
108
  - lib/discorb/asset.rb