discorb 0.1.0 → 0.2.4

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: d381f2f57b9406b242c04ef9dba26469f5a24b07b9666b1bbc3c28ee8fa9ba4b
4
+ data.tar.gz: c3734ddee2014e8de6a92f121d2e3267c72e1ca0c501f6744012c206ad453663
5
5
  SHA512:
6
- metadata.gz: 906cc3412b9cb6189947bf3446f3b9129b4665ec4247896f450f4e7bcbab3243764a246956180d5d9dc905ad86132fcadbea0659318d7fd47a30a47a4101f4f0
7
- data.tar.gz: 3615cb4feb58e8c5e7c24999a6e6af7596645f3dbebcaafe658c5c0b7c50a9a5aca467160560c316a2a7fa51ab79b6f214da0590d937f0ed2899d9ee79210346
6
+ metadata.gz: 35078668f904112c86baf31c99dc325c323e7045b13e45e3d42754e2cbf702298e2b32f589c8fe880912aa4ed850bf1ce0b41deffa91375a55e3a63621566a0d
7
+ data.tar.gz: aad1dbe26e32f6d86c2990bd614d40a44e00c4f95861b7494656333ca6b6cd5d7e14a02135bb9396eacfe66bbe0000cf3ec671f76fa52a726f1499a69f4a77b6
data/.yardopts CHANGED
@@ -3,4 +3,5 @@
3
3
  --markup markdown
4
4
  --tag flags:"Flags"
5
5
  -
6
- docs/*.md
6
+ docs/*.md
7
+ Changelog.md
data/Changelog.md CHANGED
@@ -42,7 +42,26 @@
42
42
  - Add: Add `User#created_at`
43
43
  - Add: Add `Member#to_s_user`
44
44
  - Add: Add `DefaultAvatar`
45
- - Add: Suppot application commands
45
+ - Add: Support application commands
46
46
  - Add: Add `Client#ping`
47
47
  - Add: Allow `String` for `Embed#initialize`
48
- - Change: Change log format
48
+ - Change: Change log format
49
+
50
+ ## 0.2.0
51
+
52
+ - Fix: Fix unused dependency
53
+ - Add: Add `Client#close!`
54
+ - Add: Add discord-irb
55
+
56
+ ## 0.2.1
57
+
58
+ - Fix: Fix NoMethodError in reaction event
59
+ - Add: Add Changelog.md to document
60
+
61
+ ## 0.2.2 (yanked)
62
+
63
+ - Add: Add `Snowflake#to_str`
64
+
65
+ ## 0.2.3
66
+
67
+ - Fix: Fix critical error
data/Gemfile CHANGED
@@ -7,12 +7,6 @@ gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
9
 
10
- gem "async"
11
- gem "async-http"
12
- gem "async-websocket"
13
-
14
- gem "mime-types", "~> 3.3"
15
-
16
10
  group :debug, optional: true do
17
11
  gem "rufo", "~> 0.13.0"
18
12
  end
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.4)
5
5
  async
6
6
  async-http
7
7
  async-websocket
@@ -10,7 +10,7 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- async (1.30.0)
13
+ async (1.30.1)
14
14
  console (~> 1.10)
15
15
  nio4r (~> 2.3)
16
16
  timers (~> 4.1)
@@ -56,11 +56,7 @@ PLATFORMS
56
56
  x86-mingw32
57
57
 
58
58
  DEPENDENCIES
59
- async
60
- async-http
61
- async-websocket
62
59
  discorb!
63
- mime-types (~> 3.3)
64
60
  rake (~> 13.0)
65
61
  redcarpet
66
62
  rufo (~> 0.13.0)
@@ -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.4"
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
 
@@ -73,6 +73,8 @@ module Discorb
73
73
  @value.to_s
74
74
  end
75
75
 
76
+ alias to_str to_s
77
+
76
78
  #
77
79
  # Integerize snowflake.
78
80
  #
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
  #
@@ -926,7 +926,7 @@ module Discorb
926
926
  if (target_reaction = target_message.reactions.find do |r|
927
927
  r.emoji.is_a?(UnicodeEmoji) ? r.emoji.value == data[:emoji][:name] : r.emoji.id == data[:emoji][:id]
928
928
  end)
929
- target_reaction.set_instance_variable(:@count, target_reaction.count + 1)
929
+ target_reaction.instance_variable_set(:@count, target_reaction.count + 1)
930
930
  else
931
931
  target_message.reactions << Reaction.new(
932
932
  self,
@@ -944,7 +944,7 @@ module Discorb
944
944
  (target_reaction = target_message.reactions.find do |r|
945
945
  data[:emoji][:id].nil? ? r.emoji.name == data[:emoji][:name] : r.emoji.id == data[:emoji][:id]
946
946
  end)
947
- target_reaction.set_instance_variable(:@count, target_reaction.count - 1)
947
+ target_reaction.instance_variable_set(:@count, target_reaction.count - 1)
948
948
  target_message.reactions.delete(target_reaction) if target_reaction.count.zero?
949
949
  end
950
950
  dispatch(:reaction_remove, ReactionEvent.new(self, data))
@@ -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.4
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