discorb 0.9.1 → 0.9.6

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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build_main.yml +2 -0
  3. data/Changelog.md +30 -1
  4. data/README.md +6 -6
  5. data/Rakefile +7 -1
  6. data/assets/banner.svg +101 -101
  7. data/docs/application_command.md +8 -7
  8. data/docs/cli/run.md +3 -3
  9. data/docs/faq.md +1 -1
  10. data/docs/license.md +1 -1
  11. data/lib/discorb/app_command.rb +12 -10
  12. data/lib/discorb/application.rb +3 -3
  13. data/lib/discorb/asset.rb +2 -2
  14. data/lib/discorb/audit_logs.rb +7 -7
  15. data/lib/discorb/channel.rb +10 -10
  16. data/lib/discorb/common.rb +3 -3
  17. data/lib/discorb/components.rb +30 -2
  18. data/lib/discorb/embed.rb +2 -2
  19. data/lib/discorb/emoji.rb +3 -3
  20. data/lib/discorb/error.rb +2 -2
  21. data/lib/discorb/exe/init.rb +8 -8
  22. data/lib/discorb/exe/run.rb +1 -1
  23. data/lib/discorb/extend.rb +21 -0
  24. data/lib/discorb/extension.rb +1 -1
  25. data/lib/discorb/file.rb +19 -1
  26. data/lib/discorb/gateway.rb +9 -11
  27. data/lib/discorb/gateway_requests.rb +1 -1
  28. data/lib/discorb/guild.rb +7 -7
  29. data/lib/discorb/guild_template.rb +4 -4
  30. data/lib/discorb/http.rb +1 -1
  31. data/lib/discorb/integration.rb +3 -3
  32. data/lib/discorb/interaction.rb +108 -37
  33. data/lib/discorb/invite.rb +2 -2
  34. data/lib/discorb/log.rb +1 -1
  35. data/lib/discorb/member.rb +1 -1
  36. data/lib/discorb/message.rb +34 -10
  37. data/lib/discorb/modules.rb +15 -49
  38. data/lib/discorb/permission.rb +2 -2
  39. data/lib/discorb/presence.rb +8 -8
  40. data/lib/discorb/rate_limit.rb +1 -1
  41. data/lib/discorb/reaction.rb +1 -1
  42. data/lib/discorb/role.rb +2 -2
  43. data/lib/discorb/sticker.rb +3 -3
  44. data/lib/discorb/user.rb +2 -2
  45. data/lib/discorb/utils/colored_puts.rb +3 -3
  46. data/lib/discorb/utils.rb +1 -1
  47. data/lib/discorb/voice_state.rb +3 -3
  48. data/lib/discorb/webhook.rb +7 -8
  49. data/lib/discorb.rb +1 -1
  50. data/sig/discorb.rbs +2 -2
  51. data/template-replace/files/css/common.css +25 -0
  52. data/template-replace/files/favicon.png +0 -0
  53. data/template-replace/scripts/arrow.rb +7 -0
  54. data/template-replace/scripts/favicon.rb +9 -0
  55. metadata +5 -2
@@ -19,7 +19,7 @@ module Discorb
19
19
  @channel_type = nil
20
20
  @subclasses = []
21
21
 
22
- # @!visibility private
22
+ # @private
23
23
  def initialize(client, data, no_cache: false)
24
24
  @client = client
25
25
  @data = {}
@@ -44,12 +44,12 @@ module Discorb
44
44
  "#<#{self.class} \"##{@name}\" id=#{@id}>"
45
45
  end
46
46
 
47
- # @!visibility private
47
+ # @private
48
48
  def self.descendants
49
49
  ObjectSpace.each_object(Class).select { |klass| klass < self }
50
50
  end
51
51
 
52
- # @!visibility private
52
+ # @private
53
53
  def self.make_channel(client, data, no_cache: false)
54
54
  descendants.each do |klass|
55
55
  return klass.new(client, data, no_cache: no_cache) if !klass.channel_type.nil? && klass.channel_type == data[:type]
@@ -59,7 +59,7 @@ module Discorb
59
59
  end
60
60
 
61
61
  class << self
62
- # @!visibility private
62
+ # @private
63
63
  attr_reader :channel_type
64
64
  end
65
65
 
@@ -67,7 +67,7 @@ module Discorb
67
67
  self.class.channel_type
68
68
  end
69
69
 
70
- # @!visibility private
70
+ # @private
71
71
  def channel_id
72
72
  Async do
73
73
  @id
@@ -248,7 +248,7 @@ module Discorb
248
248
 
249
249
  @channel_type = 0
250
250
 
251
- # @!visibility private
251
+ # @private
252
252
  def initialize(client, data, no_cache: false)
253
253
  super
254
254
  @threads = Dictionary.new
@@ -697,7 +697,7 @@ module Discorb
697
697
  attr_reader :bitrate
698
698
  # @return [Integer] The user limit of the voice channel.
699
699
  attr_reader :user_limit
700
- # @!visibility private
700
+ # @private
701
701
  attr_reader :stage_instances
702
702
 
703
703
  include Connectable
@@ -706,7 +706,7 @@ module Discorb
706
706
  # @return [Discorb::StageInstance] The stage instance of the channel.
707
707
 
708
708
  @channel_type = 13
709
- # @!visibility private
709
+ # @private
710
710
  def initialize(...)
711
711
  @stage_instances = Dictionary.new
712
712
  super(...)
@@ -831,7 +831,7 @@ module Discorb
831
831
  include Messageable
832
832
  @channel_type = nil
833
833
 
834
- # @!visibility private
834
+ # @private
835
835
  def initialize(client, data, no_cache: false)
836
836
  @members = Dictionary.new
837
837
  super
@@ -1092,7 +1092,7 @@ module Discorb
1092
1092
  class DMChannel < Channel
1093
1093
  include Messageable
1094
1094
 
1095
- # @!visibility private
1095
+ # @private
1096
1096
  def channel_id
1097
1097
  Async do
1098
1098
  @id
@@ -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.9.1"
7
+ VERSION = "0.9.6"
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
 
@@ -25,7 +25,7 @@ module Discorb
25
25
  end
26
26
  end
27
27
 
28
- # @!visibility private
28
+ # @private
29
29
  def inspect
30
30
  super
31
31
  end
@@ -40,7 +40,7 @@ module Discorb
40
40
  #
41
41
  # @see https://discord.com/developers/docs/reference#snowflakes Official Discord API docs
42
42
  class Snowflake < DiscordModel
43
- # @!visibility private
43
+ # @private
44
44
  def initialize(value)
45
45
  @value = value.to_i
46
46
  end
@@ -36,6 +36,34 @@ module Discorb
36
36
  )
37
37
  end
38
38
  end
39
+
40
+ #
41
+ # Convert components to a hash.
42
+ #
43
+ # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components Components.
44
+ #
45
+ # @return [Array<Hash>] Hash data.
46
+ #
47
+ def to_payload(components)
48
+ tmp_components = []
49
+ tmp_row = []
50
+ components.each do |c|
51
+ case c
52
+ when Array
53
+ tmp_components << tmp_row
54
+ tmp_row = []
55
+ tmp_components << c
56
+ when SelectMenu
57
+ tmp_components << tmp_row
58
+ tmp_row = []
59
+ tmp_components << [c]
60
+ else
61
+ tmp_row << c
62
+ end
63
+ end
64
+ tmp_components << tmp_row
65
+ return tmp_components.filter { |c| c.length.positive? }.map { |c| { type: 1, components: c.map(&:to_hash) } }
66
+ end
39
67
  end
40
68
  end
41
69
 
@@ -115,7 +143,7 @@ module Discorb
115
143
  end
116
144
 
117
145
  class << self
118
- # @!visibility private
146
+ # @private
119
147
  attr_reader :styles
120
148
  end
121
149
 
@@ -236,7 +264,7 @@ module Discorb
236
264
  }
237
265
  end
238
266
 
239
- # @!visibility private
267
+ # @private
240
268
  def hash_emoji(emoji)
241
269
  case emoji
242
270
  when UnicodeEmoji
data/lib/discorb/embed.rb CHANGED
@@ -317,7 +317,7 @@ module Discorb
317
317
  # @return [Integer] The width of video.
318
318
  attr_reader :width
319
319
 
320
- # @!visibility private
320
+ # @private
321
321
  def initialize(data)
322
322
  @url = data[:url]
323
323
  @proxy_url = data[:proxy_url]
@@ -335,7 +335,7 @@ module Discorb
335
335
  # @return [String] The url of provider.
336
336
  attr_reader :url
337
337
 
338
- # @!visibility private
338
+ # @private
339
339
  def initialize(data)
340
340
  @name = data[:name]
341
341
  @url = data[:url]
data/lib/discorb/emoji.rb CHANGED
@@ -40,7 +40,7 @@ module Discorb
40
40
  # @!attribute [r] roles?
41
41
  # @return [Boolean] whether or not this emoji is restricted to certain roles.
42
42
 
43
- # @!visibility private
43
+ # @private
44
44
  def initialize(client, guild, data)
45
45
  @client = client
46
46
  @guild = guild
@@ -147,7 +147,7 @@ module Discorb
147
147
  attr_reader :deleted
148
148
  alias deleted? deleted
149
149
 
150
- # @!visibility private
150
+ # @private
151
151
  def initialize(data)
152
152
  @id = Snowflake.new(data[:id])
153
153
  @name = data[:name]
@@ -189,7 +189,7 @@ module Discorb
189
189
  # @return [Integer] The skin tone of the emoji.
190
190
  attr_reader :skin_tone
191
191
 
192
- # @!visibility private
192
+ # @private
193
193
  def initialize(name, tone: 0)
194
194
  if EmojiTable::DISCORD_TO_UNICODE.key?(name)
195
195
  @name = name
data/lib/discorb/error.rb CHANGED
@@ -43,7 +43,7 @@ module Discorb
43
43
  # @return [Net::HTTPResponse] the HTTP response.
44
44
  attr_reader :response
45
45
 
46
- # @!visibility private
46
+ # @private
47
47
  def initialize(resp, data)
48
48
  @code = data[:code]
49
49
  @response = resp
@@ -55,7 +55,7 @@ module Discorb
55
55
  # Represents a 400 error.
56
56
  #
57
57
  class BadRequestError < HTTPError
58
- # @!visibility private
58
+ # @private
59
59
  def initialize(resp, data)
60
60
  @code = data[:code]
61
61
  @response = resp
@@ -6,7 +6,7 @@ require_relative "../utils/colored_puts"
6
6
 
7
7
  $path = Dir.pwd
8
8
 
9
- # @!visibility private
9
+ # @private
10
10
  FILES = {
11
11
  "main.rb" => <<~'RUBY',
12
12
  require "discorb"
@@ -133,12 +133,12 @@ FILES = {
133
133
  MARKDOWN
134
134
  }
135
135
 
136
- # @!visibility private
136
+ # @private
137
137
  def create_file(name)
138
138
  File.write($path + "/#{name}", format(FILES[name], token: $values[:token], name: $values[:name]), mode: "wb")
139
139
  end
140
140
 
141
- # @!visibility private
141
+ # @private
142
142
  def make_files
143
143
  iputs "Making files..."
144
144
  create_file("main.rb")
@@ -146,7 +146,7 @@ def make_files
146
146
  sputs "Made files.\n"
147
147
  end
148
148
 
149
- # @!visibility private
149
+ # @private
150
150
  def bundle_init
151
151
  iputs "Initializing bundle..."
152
152
  create_file("Gemfile")
@@ -155,7 +155,7 @@ def bundle_init
155
155
  sputs "Installed gems.\n"
156
156
  end
157
157
 
158
- # @!visibility private
158
+ # @private
159
159
  def git_init
160
160
  create_file(".gitignore")
161
161
  iputs "Initializing git repository..."
@@ -167,7 +167,7 @@ def git_init
167
167
  " to change commit message of initial commit.\n"
168
168
  end
169
169
 
170
- # @!visibility private
170
+ # @private
171
171
  def make_descs
172
172
  iputs "Making descriptions..."
173
173
  create_file(".env.sample")
@@ -246,8 +246,8 @@ bundle_init if $values[:bundle]
246
246
 
247
247
  make_files
248
248
 
249
- git_init if $values[:git]
250
-
251
249
  make_descs if $values[:descs]
252
250
 
251
+ git_init if $values[:git]
252
+
253
253
  sputs "\nSuccessfully made a new project at \e[32m#{$path}\e[92m."
@@ -5,7 +5,7 @@ require "discorb/utils/colored_puts"
5
5
  require "io/console"
6
6
 
7
7
  ARGV.delete_at 0
8
- # @!visibility private
8
+ # @private
9
9
  LOG_LEVELS = %w[none debug info warn error fatal]
10
10
 
11
11
  opt = OptionParser.new <<~BANNER
@@ -16,3 +16,24 @@ class Time
16
16
  end
17
17
  end
18
18
  end
19
+
20
+ # @private
21
+ class Async::Node
22
+ def description
23
+ @object_name ||= "#{self.class}:0x#{object_id.to_s(16)}#{@transient ? " transient" : nil}"
24
+
25
+ if @annotation
26
+ "#{@object_name} #{@annotation}"
27
+ elsif line = self.backtrace(0, 1)&.first
28
+ "#{@object_name} #{line}"
29
+ else
30
+ @object_name
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ "\#<#{self.description}>"
36
+ end
37
+
38
+ alias inspect to_s
39
+ end
@@ -54,7 +54,7 @@ module Discorb
54
54
  # @private
55
55
  attr_reader :bottom_commands
56
56
 
57
- # @!visibility private
57
+ # @private
58
58
  attr_accessor :client
59
59
 
60
60
  def self.extended(obj)
data/lib/discorb/file.rb CHANGED
@@ -32,7 +32,7 @@ module Discorb
32
32
  # @!attribute [r] image?
33
33
  # @return [Boolean] whether the file is an image.
34
34
 
35
- # @!visibility private
35
+ # @private
36
36
  def initialize(data)
37
37
  @id = Snowflake.new(data[:id])
38
38
  @filename = data[:filename]
@@ -60,6 +60,14 @@ module Discorb
60
60
  # @return [String] The content type of the file. If not set, it is guessed from the filename.
61
61
  attr_accessor :content_type
62
62
 
63
+ #
64
+ # Creates a new file from IO.
65
+ #
66
+ # @param [#read] io The IO of the file.
67
+ # @param [String] filename The filename of the file. If not set, path or object_id of the IO is used.
68
+ # @param [String] content_type The content type of the file. If not set, it is guessed from the filename.
69
+ # If failed to guess, it is set to `application/octet-stream`.
70
+ #
63
71
  def initialize(io, filename = nil, content_type: nil)
64
72
  @io = io
65
73
  @filename = filename || (io.respond_to?(:path) ? io.path : io.object_id)
@@ -67,8 +75,18 @@ module Discorb
67
75
  @content_type = "application/octet-stream" if @content_type == ""
68
76
  end
69
77
 
78
+ #
79
+ # Creates a new file from a string.
80
+ #
81
+ # @param [String] string The string to create the file from.
82
+ # @param [String] filename The filename of the file. object_id of the string is used if not set.
83
+ # @param [String] content_type The content type of the file. If not set, it is guessed from the filename.
84
+ #
85
+ # @return [File] The new file.
86
+ #
70
87
  def self.from_string(string, filename: nil, content_type: nil)
71
88
  io = StringIO.new(string)
89
+ filename ||= string.object_id.to_s + ".txt"
72
90
  new(io, filename, content_type: content_type)
73
91
  end
74
92
  end
@@ -3,7 +3,6 @@
3
3
  require "async/http"
4
4
  require "async/websocket"
5
5
  require "async/barrier"
6
- require "async/semaphore"
7
6
  require "json"
8
7
  require "zlib"
9
8
 
@@ -17,7 +16,7 @@ module Discorb
17
16
  # Represents an event.
18
17
  #
19
18
  class GatewayEvent
20
- # @!visibility private
19
+ # @private
21
20
  def initialize(data)
22
21
  @data = data
23
22
  end
@@ -62,7 +61,7 @@ module Discorb
62
61
  alias reactor fired_by
63
62
  alias from fired_by
64
63
 
65
- # @!visibility private
64
+ # @private
66
65
  def initialize(client, data)
67
66
  @client = client
68
67
  @data = data
@@ -130,7 +129,7 @@ module Discorb
130
129
  # @return [Discorb::Message] The message the reaction was sent in.
131
130
  attr_reader :message
132
131
 
133
- # @!visibility private
132
+ # @private
134
133
  def initialize(client, data)
135
134
  @client = client
136
135
  @data = data
@@ -181,7 +180,7 @@ module Discorb
181
180
  # @return [Discorb::UnicodeEmoji, Discorb::PartialEmoji] The emoji that was reacted with.
182
181
  attr_reader :emoji
183
182
 
184
- # @!visibility private
183
+ # @private
185
184
  def initialize(client, data)
186
185
  @client = client
187
186
  @data = data
@@ -297,7 +296,7 @@ module Discorb
297
296
  # @macro client_cache
298
297
  # @return [Discorb::Guild] The guild the message was sent in.
299
298
 
300
- # @!visibility private
299
+ # @private
301
300
  def initialize(client, id, data)
302
301
  @client = client
303
302
  @id = id
@@ -329,7 +328,7 @@ module Discorb
329
328
  # @macro client_cache
330
329
  # @return [Discorb::Guild] The guild the message was sent in.
331
330
 
332
- # @!visibility private
331
+ # @private
333
332
  def initialize(client, data)
334
333
  @client = client
335
334
  @data = data
@@ -382,7 +381,7 @@ module Discorb
382
381
  # @macro client_cache
383
382
  # @return [Discorb::Member, Discorb::User] The member or user that started typing.
384
383
 
385
- # @!visibility private
384
+ # @private
386
385
  def initialize(client, data)
387
386
  @client = client
388
387
  @data = data
@@ -459,7 +458,7 @@ module Discorb
459
458
  # @macro client_cache
460
459
  # @return [Discorb::Guild] The guild where the webhook was updated.
461
460
 
462
- # @!visibility private
461
+ # @private
463
462
  def initialize(client, data)
464
463
  @client = client
465
464
  @data = data
@@ -1035,10 +1034,9 @@ module Discorb
1035
1034
  if @fetch_member
1036
1035
  @log.debug "Fetching members"
1037
1036
  barrier = Async::Barrier.new
1038
- semaphore = Async::Semaphore.new(@guilds.length)
1039
1037
 
1040
1038
  @guilds.each do |guild|
1041
- semaphore.async(parent: barrier) do
1039
+ barrier.async(parent: barrier) do
1042
1040
  guild.fetch_members
1043
1041
  end
1044
1042
  end
@@ -40,7 +40,7 @@ module Discorb
40
40
  end
41
41
 
42
42
  class << self
43
- # @!visibility private
43
+ # @private
44
44
  attr_reader :types
45
45
  end
46
46
  end
data/lib/discorb/guild.rb CHANGED
@@ -118,14 +118,14 @@ module Discorb
118
118
  @default_message_notifications = %i[all_messages only_mentions].freeze
119
119
  @explicit_content_filter = %i[disabled_in_text members_without_roles all_members].freeze
120
120
 
121
- # @!visibility private
121
+ # @private
122
122
  def initialize(client, data, is_create_event)
123
123
  @client = client
124
124
  @data = {}
125
125
  _set_data(data, is_create_event)
126
126
  end
127
127
 
128
- # @!visibility private
128
+ # @private
129
129
  def update!
130
130
  Async do
131
131
  _, data = @client.get("/guilds/#{@id}").wait
@@ -952,7 +952,7 @@ module Discorb
952
952
  # @!attribute [r] url
953
953
  # @return [String] The vanity URL.
954
954
 
955
- # @!visibility private
955
+ # @private
956
956
  def initialize(client, guild, data)
957
957
  @client = client
958
958
  @guild = guild
@@ -987,7 +987,7 @@ module Discorb
987
987
  # @!attribute [r] json_url
988
988
  # @return [String] The JSON URL.
989
989
 
990
- # @!visibility private
990
+ # @private
991
991
  def initialize(client, guild_id, data)
992
992
  @client = client
993
993
  @enabled = data[:enabled]
@@ -1050,7 +1050,7 @@ module Discorb
1050
1050
  # @return [String] The reason for the ban.
1051
1051
  attr_reader :reason
1052
1052
 
1053
- # @!visibility private
1053
+ # @private
1054
1054
  def initialize(client, guild, data)
1055
1055
  @client = client
1056
1056
  @guild = guild
@@ -1060,7 +1060,7 @@ module Discorb
1060
1060
  end
1061
1061
 
1062
1062
  class << self
1063
- # @!visibility private
1063
+ # @private
1064
1064
  attr_reader :nsfw_levels, :mfa_levels, :verification_levels, :default_message_notifications, :explicit_content_filter
1065
1065
 
1066
1066
  #
@@ -1175,7 +1175,7 @@ module Discorb
1175
1175
  # @return [Discorb::Guild] The guild the welcome screen belongs to.
1176
1176
  attr_reader :guild
1177
1177
 
1178
- # @!visibility private
1178
+ # @private
1179
1179
  def initialize(client, guild, data)
1180
1180
  @client = client
1181
1181
  @description = data[:description]
@@ -33,7 +33,7 @@ module Discorb
33
33
  # @return [Discorb::Guild] The guild this template is based on.
34
34
  # @return [nil] Client wasn't able to find the guild this template is based on.
35
35
 
36
- # @!visibility private
36
+ # @private
37
37
  def initialize(client, data)
38
38
  @client = client
39
39
  _set_data(data)
@@ -117,7 +117,7 @@ module Discorb
117
117
  attr_reader :widget_enabled
118
118
  alias widget_enabled? widget_enabled
119
119
 
120
- # @!visibility private
120
+ # @private
121
121
  def initialize(data)
122
122
  @name = data[:name]
123
123
  @description = data[:description]
@@ -143,7 +143,7 @@ module Discorb
143
143
  # @return [Discorb::Color] The color of the role.
144
144
  attr_reader :color
145
145
 
146
- # @!visibility private
146
+ # @private
147
147
  def initialize(data)
148
148
  @name = data[:name]
149
149
  @permissions = Permission.new(data[:permissions])
@@ -174,7 +174,7 @@ module Discorb
174
174
  # @return [Class] The class of the channel.
175
175
  attr_reader :type
176
176
 
177
- # @!visibility private
177
+ # @private
178
178
  def initialize(data)
179
179
  @name = data[:name]
180
180
  @position = data[:position]
data/lib/discorb/http.rb CHANGED
@@ -10,7 +10,7 @@ module Discorb
10
10
  class HTTP
11
11
  @nil_body = nil
12
12
 
13
- # @!visibility private
13
+ # @private
14
14
  def initialize(client)
15
15
  @client = client
16
16
  @ratelimit_handler = RatelimitHandler.new(client)
@@ -43,7 +43,7 @@ module Discorb
43
43
  1 => :kick,
44
44
  }
45
45
 
46
- # @!visibility private
46
+ # @private
47
47
  def initialize(client, data, guild_id, no_cache: false)
48
48
  @client = client
49
49
  @data = data
@@ -88,7 +88,7 @@ module Discorb
88
88
  end
89
89
 
90
90
  class << self
91
- # @!visibility private
91
+ # @private
92
92
  attr_reader :expire_behavior
93
93
  end
94
94
 
@@ -101,7 +101,7 @@ module Discorb
101
101
  # @return [String] The name of the account.
102
102
  attr_reader :name
103
103
 
104
- # @!visibility private
104
+ # @private
105
105
  def initialize(data)
106
106
  @id = data[:id]
107
107
  @name = data[:name]