discorb 0.13.4 → 0.15.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.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +2 -0
  3. data/.github/workflows/build_version.yml +1 -1
  4. data/.github/workflows/codeql-analysis.yml +70 -0
  5. data/.github/workflows/lint-push.yml +20 -0
  6. data/.github/workflows/lint.yml +16 -0
  7. data/.rubocop.yml +74 -0
  8. data/Changelog.md +30 -0
  9. data/Gemfile +7 -3
  10. data/Rakefile +28 -22
  11. data/discorb.gemspec +1 -0
  12. data/docs/events.md +50 -0
  13. data/docs/faq.md +8 -8
  14. data/examples/commands/bookmarker.rb +2 -1
  15. data/examples/commands/hello.rb +1 -0
  16. data/examples/commands/inspect.rb +3 -2
  17. data/examples/components/authorization_button.rb +2 -1
  18. data/examples/components/select_menu.rb +2 -1
  19. data/examples/extension/main.rb +1 -0
  20. data/examples/extension/message_expander.rb +1 -0
  21. data/examples/simple/eval.rb +3 -2
  22. data/examples/simple/ping_pong.rb +1 -0
  23. data/examples/simple/rolepanel.rb +1 -0
  24. data/examples/simple/wait_for_message.rb +4 -3
  25. data/exe/discorb +8 -7
  26. data/lib/discorb/allowed_mentions.rb +71 -0
  27. data/lib/discorb/app_command/command.rb +336 -0
  28. data/lib/discorb/app_command/handler.rb +168 -0
  29. data/lib/discorb/app_command.rb +2 -426
  30. data/lib/discorb/application.rb +16 -7
  31. data/lib/discorb/asset.rb +11 -0
  32. data/lib/discorb/{file.rb → attachment.rb} +55 -33
  33. data/lib/discorb/audit_logs.rb +45 -7
  34. data/lib/discorb/channel.rb +65 -15
  35. data/lib/discorb/client.rb +34 -27
  36. data/lib/discorb/common.rb +19 -27
  37. data/lib/discorb/components/button.rb +105 -0
  38. data/lib/discorb/components/select_menu.rb +143 -0
  39. data/lib/discorb/components/text_input.rb +96 -0
  40. data/lib/discorb/components.rb +11 -276
  41. data/lib/discorb/dictionary.rb +5 -0
  42. data/lib/discorb/embed.rb +73 -40
  43. data/lib/discorb/emoji.rb +48 -5
  44. data/lib/discorb/error.rb +9 -5
  45. data/lib/discorb/event.rb +36 -24
  46. data/lib/discorb/exe/about.rb +1 -0
  47. data/lib/discorb/exe/irb.rb +4 -3
  48. data/lib/discorb/exe/new.rb +6 -7
  49. data/lib/discorb/exe/run.rb +2 -1
  50. data/lib/discorb/exe/setup.rb +8 -5
  51. data/lib/discorb/exe/show.rb +1 -0
  52. data/lib/discorb/extend.rb +19 -14
  53. data/lib/discorb/extension.rb +5 -1
  54. data/lib/discorb/gateway.rb +112 -51
  55. data/lib/discorb/gateway_requests.rb +4 -7
  56. data/lib/discorb/guild.rb +73 -41
  57. data/lib/discorb/guild_template.rb +26 -5
  58. data/lib/discorb/http.rb +38 -18
  59. data/lib/discorb/integration.rb +24 -9
  60. data/lib/discorb/intents.rb +16 -11
  61. data/lib/discorb/interaction/autocomplete.rb +6 -5
  62. data/lib/discorb/interaction/command.rb +66 -12
  63. data/lib/discorb/interaction/components.rb +19 -3
  64. data/lib/discorb/interaction/modal.rb +33 -0
  65. data/lib/discorb/interaction/response.rb +45 -4
  66. data/lib/discorb/interaction/root.rb +16 -0
  67. data/lib/discorb/interaction.rb +2 -1
  68. data/lib/discorb/invite.rb +11 -7
  69. data/lib/discorb/log.rb +5 -5
  70. data/lib/discorb/member.rb +22 -3
  71. data/lib/discorb/message.rb +39 -234
  72. data/lib/discorb/message_meta.rb +186 -0
  73. data/lib/discorb/modules.rb +39 -15
  74. data/lib/discorb/permission.rb +16 -7
  75. data/lib/discorb/presence.rb +45 -9
  76. data/lib/discorb/rate_limit.rb +7 -4
  77. data/lib/discorb/reaction.rb +6 -0
  78. data/lib/discorb/role.rb +12 -0
  79. data/lib/discorb/sticker.rb +22 -14
  80. data/lib/discorb/user.rb +12 -1
  81. data/lib/discorb/utils/colored_puts.rb +1 -0
  82. data/lib/discorb/voice_state.rb +23 -2
  83. data/lib/discorb/webhook.rb +54 -3
  84. data/lib/discorb.rb +5 -2
  85. data/sig/discorb.rbs +838 -702
  86. data/template-replace/scripts/arrow.rb +1 -0
  87. data/template-replace/scripts/favicon.rb +1 -0
  88. data/template-replace/scripts/index.rb +2 -1
  89. data/template-replace/scripts/locale_ja.rb +5 -4
  90. data/template-replace/scripts/sidebar.rb +1 -0
  91. data/template-replace/scripts/version.rb +7 -10
  92. data/template-replace/scripts/yard_replace.rb +5 -4
  93. metadata +17 -3
data/lib/discorb/embed.rb CHANGED
@@ -45,45 +45,51 @@ module Discorb
45
45
  # @param [Discorb::Embed::Thumbnail, String] thumbnail The thumbnail of embed.
46
46
  #
47
47
  def initialize(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil,
48
- fields: nil, footer: nil, image: nil, thumbnail: nil, data: nil)
49
- if data.nil?
50
- @title = title
51
- @description = description
52
- @url = url
53
- @timestamp = timestamp
54
- @color = color
55
- @author = author
56
- @fields = fields || []
57
- @footer = footer
58
- @image = image && (image.is_a?(String) ? Image.new(image) : image)
59
- @thumbnail = thumbnail && (thumbnail.is_a?(String) ? Thumbnail.new(thumbnail) : thumbnail)
60
- @type = "rich"
61
- else
62
- @title = data[:title]
63
- @description = data[:description]
64
- @url = data[:url]
65
- @timestamp = data[:timestamp] && Time.iso8601(data[:timestamp])
66
- @type = data[:type]
67
- @color = data[:color] && Color.new(data[:color])
68
- @footer = data[:footer] && Footer.new(data[:footer][:text], icon: data[:footer][:icon_url])
69
- @author = if data[:author]
70
- Author.new(data[:author][:name], icon: data[:author][:icon_url],
71
- url: data[:author][:url])
72
- end
73
- @thumbnail = data[:thumbnail] && Thumbnail.new(data[:thumbnail])
74
- @image = data[:image] && Image.new(data[:image])
75
- @video = data[:video] && Video.new(data[:video])
76
- @provider = data[:provider] && Provider.new(data[:provider])
77
- @fields = data[:fields] ? data[:fields].map { |f| Field.new(f[:name], f[:value], inline: f[:inline]) } : []
78
- end
48
+ fields: nil, footer: nil, image: nil, thumbnail: nil)
49
+ @title = title
50
+ @description = description
51
+ @url = url
52
+ @timestamp = timestamp
53
+ @color = color
54
+ @author = author
55
+ @fields = fields || []
56
+ @footer = footer
57
+ @image = image && (image.is_a?(String) ? Image.new(image) : image)
58
+ @thumbnail = thumbnail && (thumbnail.is_a?(String) ? Thumbnail.new(thumbnail) : thumbnail)
59
+ @type = "rich"
60
+ end
61
+
62
+ #
63
+ # Initialize embed from hash.
64
+ # @private
65
+ #
66
+ # @param [Hash] data The hash data to initialize embed.
67
+ #
68
+ def initialize_hash(data)
69
+ @title = data[:title]
70
+ @description = data[:description]
71
+ @url = data[:url]
72
+ @timestamp = data[:timestamp] && Time.iso8601(data[:timestamp])
73
+ @type = data[:type]
74
+ @color = data[:color] && Color.new(data[:color])
75
+ @footer = data[:footer] && Footer.new(data[:footer][:text], icon: data[:footer][:icon_url])
76
+ @author = if data[:author]
77
+ Author.new(data[:author][:name], icon: data[:author][:icon_url],
78
+ url: data[:author][:url])
79
+ end
80
+ @thumbnail = data[:thumbnail] && Thumbnail.new(data[:thumbnail])
81
+ @image = data[:image] && Image.new(data[:image])
82
+ @video = data[:video] && Video.new(data[:video])
83
+ @provider = data[:provider] && Provider.new(data[:provider])
84
+ @fields = data[:fields] ? data[:fields].map { |f| Field.new(f[:name], f[:value], inline: f[:inline]) } : []
79
85
  end
80
86
 
81
87
  def image=(value)
82
- @image = (value.is_a?(String)) ? Image.new(value) : value
88
+ @image = value.is_a?(String) ? Image.new(value) : value
83
89
  end
84
90
 
85
91
  def thumbnail=(value)
86
- @thumbnail = (value.is_a?(String)) ? Thumbnail.new(value) : value
92
+ @thumbnail = value.is_a?(String) ? Thumbnail.new(value) : value
87
93
  end
88
94
 
89
95
  def inspect
@@ -111,10 +117,27 @@ module Discorb
111
117
  ret
112
118
  end
113
119
 
120
+ def self.from_hash(data)
121
+ inst = allocate
122
+ inst.initialize_hash(data)
123
+ inst
124
+ end
125
+
126
+ #
127
+ # Represents an entry in embed.
128
+ # @abstract
129
+ # @private
130
+ #
131
+ class Entry
132
+ def inspect
133
+ "#<#{self.class}>"
134
+ end
135
+ end
136
+
114
137
  #
115
138
  # Represents an author of embed.
116
139
  #
117
- class Author
140
+ class Author < Entry
118
141
  # @return [String] The name of author.
119
142
  attr_accessor :name
120
143
  # @return [String, nil] The url of author.
@@ -153,7 +176,7 @@ module Discorb
153
176
  #
154
177
  # Represemts a footer of embed.
155
178
  #
156
- class Footer
179
+ class Footer < Entry
157
180
  attr_accessor :text, :icon
158
181
 
159
182
  #
@@ -184,7 +207,7 @@ module Discorb
184
207
  #
185
208
  # Represents a field of embed.
186
209
  #
187
- class Field
210
+ class Field < Entry
188
211
  # @return [String] The name of field.
189
212
  attr_accessor :name
190
213
  # @return [String] The value of field.
@@ -223,7 +246,7 @@ module Discorb
223
246
  #
224
247
  # Represents an image of embed.
225
248
  #
226
- class Image
249
+ class Image < Entry
227
250
  # @return [String] The url of image.
228
251
  attr_accessor :url
229
252
  # @return [String] The proxy url of image.
@@ -267,7 +290,7 @@ module Discorb
267
290
  #
268
291
  # Represents a thumbnail of embed.
269
292
  #
270
- class Thumbnail
293
+ class Thumbnail < Entry
271
294
  # @return [String] The url of thumbnail.
272
295
  attr_accessor :url
273
296
  # @return [String] The proxy url of thumbnail.
@@ -311,7 +334,7 @@ module Discorb
311
334
  #
312
335
  # Represents a video of embed.
313
336
  #
314
- class Video
337
+ class Video < Entry
315
338
  # @return [String] The url of video.
316
339
  attr_reader :url
317
340
  # @return [String] The proxy url of video.
@@ -321,7 +344,12 @@ module Discorb
321
344
  # @return [Integer] The width of video.
322
345
  attr_reader :width
323
346
 
347
+ #
348
+ # Initialize a new Video object.
324
349
  # @private
350
+ #
351
+ # @param [Hash] data The data of video.
352
+ #
325
353
  def initialize(data)
326
354
  @url = data[:url]
327
355
  @proxy_url = data[:proxy_url]
@@ -333,13 +361,18 @@ module Discorb
333
361
  #
334
362
  # Represents a provider of embed.
335
363
  #
336
- class Provider
364
+ class Provider < Entry
337
365
  # @return [String] The name of provider.
338
366
  attr_reader :name
339
367
  # @return [String] The url of provider.
340
368
  attr_reader :url
341
369
 
370
+ #
371
+ # Initialize a new Provider object.
342
372
  # @private
373
+ #
374
+ # @param [Hash] data The data of provider.
375
+ #
343
376
  def initialize(data)
344
377
  @name = data[:name]
345
378
  @url = data[:url]
data/lib/discorb/emoji.rb CHANGED
@@ -44,7 +44,14 @@ module Discorb
44
44
  # @!attribute [r] roles?
45
45
  # @return [Boolean] whether or not this emoji is restricted to certain roles.
46
46
 
47
+ #
48
+ # Initialize a new custom emoji.
47
49
  # @private
50
+ #
51
+ # @param [Discorb::Client] client The client that owns this emoji.
52
+ # @param [Discorb::Guild] guild The guild that owns this emoji.
53
+ # @param [Hash] data The data of the emoji.
54
+ #
48
55
  def initialize(client, guild, data)
49
56
  @client = client
50
57
  @guild = guild
@@ -121,12 +128,26 @@ module Discorb
121
128
 
122
129
  alias destroy! delete!
123
130
 
131
+ #
132
+ # Converts the object to a hash.
133
+ # @private
134
+ #
135
+ # @return [Hash] The hash represents the object.
136
+ #
137
+ def to_hash
138
+ {
139
+ name: @name,
140
+ id: @id,
141
+ animated: @animated,
142
+ }
143
+ end
144
+
124
145
  private
125
146
 
126
147
  def _set_data(data)
127
148
  @id = Snowflake.new(data[:id])
128
149
  @name = data[:name]
129
- @roles = data[:role] ? data[:role].map { |r| Role.new(@client, r) } : []
150
+ @roles = data[:role] ? data[:role].map { |r| Role.new(@client, guild, r) } : []
130
151
  @user = User.new(@client, data[:user]) if data[:user]
131
152
  @require_colons = data[:require_colons]
132
153
  @managed = data[:managed]
@@ -149,7 +170,12 @@ module Discorb
149
170
  attr_reader :deleted
150
171
  alias deleted? deleted
151
172
 
173
+ #
174
+ # Initialize a new partial custom emoji.
152
175
  # @private
176
+ #
177
+ # @param [Hash] data The data of the emoji.
178
+ #
153
179
  def initialize(data)
154
180
  @id = Snowflake.new(data[:id])
155
181
  @name = data[:name]
@@ -191,7 +217,12 @@ module Discorb
191
217
  # @return [Integer] The skin tone of the emoji.
192
218
  attr_reader :skin_tone
193
219
 
194
- # @private
220
+ #
221
+ # Initialize a new unicode emoji.
222
+ #
223
+ # @param [String] name The name of the emoji.
224
+ # @param [Integer] tone The skin tone of the emoji.
225
+ #
195
226
  def initialize(name, tone: 0)
196
227
  if EmojiTable::DISCORD_TO_UNICODE.key?(name)
197
228
  @name = name
@@ -202,9 +233,7 @@ module Discorb
202
233
  else
203
234
  raise ArgumentError, "No such emoji: #{name}"
204
235
  end
205
- if tone > 0
206
- @value += EmojiTable::SKIN_TONES[tone]
207
- end
236
+ @value += EmojiTable::SKIN_TONES[tone] if tone.positive?
208
237
  end
209
238
 
210
239
  # @return [String] The unicode string of the emoji.
@@ -225,6 +254,20 @@ module Discorb
225
254
  "#<#{self.class} :#{@name}:>"
226
255
  end
227
256
 
257
+ #
258
+ # Converts the object to a hash.
259
+ # @private
260
+ #
261
+ # @return [Hash] The hash represents the object.
262
+ #
263
+ def to_hash
264
+ {
265
+ name: @value,
266
+ id: nil,
267
+ animated: false,
268
+ }
269
+ end
270
+
228
271
  class << self
229
272
  alias [] new
230
273
  end
data/lib/discorb/error.rb CHANGED
@@ -11,9 +11,7 @@ module Discorb
11
11
  def enumerate_errors(hash)
12
12
  res = {}
13
13
  _recr_items([], hash, res)
14
- if res == { "" => nil }
15
- res = {}
16
- end
14
+ res = {} if res == { "" => nil }
17
15
  res
18
16
  end
19
17
 
@@ -44,7 +42,10 @@ module Discorb
44
42
  # @return [Net::HTTPResponse] the HTTP response.
45
43
  attr_reader :response
46
44
 
45
+ #
46
+ # Initialize a new instance of the HTTPError class.
47
47
  # @private
48
+ #
48
49
  def initialize(resp, data)
49
50
  @code = data[:code]
50
51
  @response = resp
@@ -56,7 +57,10 @@ module Discorb
56
57
  # Represents a 400 error.
57
58
  #
58
59
  class BadRequestError < HTTPError
60
+ #
61
+ # Initialize a new instance of the BadRequestError class.
59
62
  # @private
63
+ #
60
64
  def initialize(resp, data)
61
65
  @code = data[:code]
62
66
  @response = resp
@@ -92,14 +96,14 @@ module Discorb
92
96
  # Represents a error because of a cloudflare ban.
93
97
  #
94
98
  class CloudFlareBanError < HTTPError
95
- def initialize(resp, client)
99
+ def initialize(_resp, client)
96
100
  @client = client
97
101
  @client.close!
98
102
  message = <<~MESSAGE
99
103
  The client is banned from CloudFlare.
100
104
  Hint: Try to decrease the number of requests per second, e.g. Use sleep in between requests.
101
105
  MESSAGE
102
- $stderr.puts message
106
+ warn message
103
107
  DiscorbError.instance_method(:initialize).bind(self).call(message)
104
108
  end
105
109
  end
data/lib/discorb/event.rb CHANGED
@@ -1,19 +1,29 @@
1
+ # frozen_string_literal: true
1
2
  module Discorb
3
+ #
4
+ # Represents an event in guild.
5
+ #
2
6
  class ScheduledEvent < DiscordModel
3
- @privacy_level = {
7
+ # @private
8
+ # @return [{Integer => Symbol}] The mapping of privacy level.
9
+ PRIVACY_LEVEL = {
4
10
  2 => :guild_only,
5
- }
6
- @status = {
11
+ }.freeze
12
+ # @private
13
+ # @return [{Integer => Symbol}] The mapping of status.
14
+ STATUS = {
7
15
  1 => :scheduled,
8
16
  2 => :active,
9
17
  3 => :completed,
10
18
  4 => :canceled,
11
- }
12
- @entity_type = {
19
+ }.freeze
20
+ # @private
21
+ # @return [{Integer => Symbol}] The mapping of entity_type.
22
+ ENTITY_TYPE = {
13
23
  1 => :stage_instance,
14
24
  2 => :voice,
15
25
  3 => :external,
16
- }
26
+ }.freeze
17
27
 
18
28
  # @!visibility private
19
29
  def initialize(client, data)
@@ -32,6 +42,10 @@ module Discorb
32
42
  def initialize(data)
33
43
  @location = data[:location]
34
44
  end
45
+
46
+ def inspect
47
+ "#<#{self.class.name} #{@name}>"
48
+ end
35
49
  end
36
50
 
37
51
  # @return [Discorb::Snowflake] The ID of the event.
@@ -120,7 +134,7 @@ module Discorb
120
134
  status: Discorb::Unset
121
135
  )
122
136
  Async do
123
- payload = case (type == Discorb::Unset) ? @entity_type : type
137
+ payload = case type == Discorb::Unset ? @entity_type : type
124
138
  when :stage_instance
125
139
  raise ArgumentError, "channel must be provided for stage_instance events" unless channel
126
140
  {
@@ -128,10 +142,10 @@ module Discorb
128
142
  description: description,
129
143
  scheduled_start_time: start_time.iso8601,
130
144
  scheduled_end_time: end_time&.iso8601,
131
- privacy_level: Discorb::ScheduledEvent.privacy_level.key(privacy_level) || Discorb::Unset,
145
+ privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
132
146
  channel_id: channel&.id,
133
- entity_type: Discorb::ScheduledEvent.entity_type.key(:stage_instance),
134
- status: Discorb::ScheduledEvent.status.key(status) || Discorb::Unset,
147
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:stage_instance),
148
+ status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
135
149
  }.reject { |_, v| v == Discorb::Unset }
136
150
  when :voice
137
151
  raise ArgumentError, "channel must be provided for voice events" unless channel
@@ -140,10 +154,10 @@ module Discorb
140
154
  description: description,
141
155
  scheduled_start_time: start_time.iso8601,
142
156
  scheduled_end_time: end_time&.iso8601,
143
- privacy_level: Discorb::ScheduledEvent.privacy_level.key(privacy_level) || Discorb::Unset,
157
+ privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
144
158
  channel_id: channel&.id,
145
- entity_type: Discorb::ScheduledEvent.entity_type.key(:voice),
146
- status: Discorb::ScheduledEvent.status.key(status) || Discorb::Unset,
159
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:voice),
160
+ status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
147
161
  }.reject { |_, v| v == Discorb::Unset }
148
162
  when :external
149
163
  raise ArgumentError, "location must be provided for external events" unless location
@@ -154,12 +168,12 @@ module Discorb
154
168
  channel_id: nil,
155
169
  scheduled_start_time: start_time.iso8601,
156
170
  scheduled_end_time: end_time.iso8601,
157
- privacy_level: Discorb::ScheduledEvent.privacy_level.key(privacy_level) || Discorb::Unset,
158
- entity_type: Discorb::ScheduledEvent.entity_type.key(:external),
171
+ privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
172
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:external),
159
173
  entity_metadata: {
160
174
  location: location,
161
175
  },
162
- status: Discorb::ScheduledEvent.status.key(status) || Discorb::Unset,
176
+ status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
163
177
  }.reject { |_, v| v == Discorb::Unset }
164
178
  else
165
179
  raise ArgumentError, "Invalid scheduled event type: #{type}"
@@ -226,11 +240,9 @@ module Discorb
226
240
  if limit.nil?
227
241
  after = 0
228
242
  res = []
229
- while true
243
+ loop do
230
244
  _resp, users = @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}/users?limit=100&after=#{after}&with_member=true", "//guilds/:guild_id/scheduled-events/:scheduled_event_id/users", :get)).wait
231
- if users.empty?
232
- break
233
- end
245
+ break if users.empty?
234
246
  res += users.map { |u| Member.new(@client, @guild_id, u[:user], u[:member]) }
235
247
  after = users.last[:user][:id]
236
248
  end
@@ -238,8 +250,8 @@ module Discorb
238
250
  else
239
251
  params = {
240
252
  limit: limit,
241
- before: Discorb::Utils.try(after, :id),
242
- after: Discorb::Utils.try(around, :id),
253
+ before: Discorb::Utils.try(before, :id),
254
+ after: Discorb::Utils.try(after, :id),
243
255
  with_member: with_member,
244
256
  }.filter { |_k, v| !v.nil? }.to_h
245
257
  _resp, messages = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}", "//channels/:channel_id/messages", :get)).wait
@@ -262,8 +274,8 @@ module Discorb
262
274
  @scheduled_start_time = Time.iso8601(data[:scheduled_start_time])
263
275
  @scheduled_end_time = data[:scheduled_end_time] && Time.iso8601(data[:scheduled_end_time])
264
276
  @privacy_level = :guild_only # data[:privacy_level]
265
- @status = self.class.status[data[:status]]
266
- @entity_type = self.class.entity_type[data[:entity_type]]
277
+ @status = STATUS[data[:status]]
278
+ @entity_type = ENTITY_TYPE[data[:entity_type]]
267
279
  @entity_id = data[:entity_id] && Snowflake.new(data[:entity_id])
268
280
  @entity_metadata = data[:entity_metadata] && Metadata.new(data[:entity_metadata])
269
281
  @creator = @client.users[@creator_id] || (data[:creator] && User.new(@client, data[:creator]))
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # description: Show information of discorb.
2
3
 
3
4
  puts " disco\e[31mrb\e[m - A new Discord API wrapper in Ruby. \n\n"
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  # description: Connect to discord and start IRB.
4
5
 
@@ -31,13 +32,13 @@ client.on :standby do
31
32
  end
32
33
 
33
34
  def dirb_help
34
- puts <<~EOS
35
+ puts <<~MESSAGE
35
36
  \e[96mDiscord-IRB\e[m
36
37
  This is a debug client for Discord.
37
38
  \e[90mmessage\e[m to get latest message.
38
39
 
39
40
  \e[36mhttps://discorb-lib.github.io/#{Discorb::VERSION}/file.irb.html\e[m for more information.
40
- EOS
41
+ MESSAGE
41
42
  end
42
43
 
43
44
  puts <<~FIRST_MESSAGE
@@ -56,7 +57,7 @@ end
56
57
 
57
58
  token = ENV["DISCORD_BOT_TOKEN"] || ENV["DISCORD_TOKEN"]
58
59
  if token.nil?
59
- if File.exists?(token_file)
60
+ if File.exist?(token_file)
60
61
  token = File.read(token_file)
61
62
  else
62
63
  print "\e[90mToken?\e[m : "
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # description: Make files for the discorb project.
2
3
 
3
4
  require "optparse"
@@ -131,7 +132,7 @@ FILES = {
131
132
  https://rubygems.org/gems/license-cli may be useful.
132
133
 
133
134
  MARKDOWN
134
- }
135
+ }.freeze
135
136
 
136
137
  # @private
137
138
  def create_file(name)
@@ -162,8 +163,8 @@ def git_init
162
163
  system "git init"
163
164
  system "git add ."
164
165
  system "git commit -m \"Initial commit\""
165
- sputs "Initialized repository, use " +
166
- "\e[32mgit commit --amend -m '...'\e[92m" +
166
+ sputs "Initialized repository, use " \
167
+ "\e[32mgit commit --amend -m '...'\e[92m" \
167
168
  " to change commit message of initial commit.\n"
168
169
  end
169
170
 
@@ -225,13 +226,11 @@ if (dir = ARGV[0])
225
226
  if Dir.exist?($path)
226
227
  if Dir.empty?($path)
227
228
  iputs "Found \e[30m#{dir}\e[90m and empty, using this directory."
228
- else
229
- if $values[:force]
230
- iputs "Found \e[30m#{dir}\e[90m and not empty, but force is on, using this directory."
229
+ elsif $values[:force]
230
+ iputs "Found \e[30m#{dir}\e[90m and not empty, but force is on, using this directory."
231
231
  else
232
232
  eputs "Directory \e[31m#{dir}\e[91m already exists and not empty. Use \e[31m-f\e[91m to force."
233
233
  exit
234
- end
235
234
  end
236
235
  else
237
236
  Dir.mkdir($path)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # description: Run a client.
2
3
  require "optparse"
3
4
  require "json"
@@ -6,7 +7,7 @@ require "io/console"
6
7
 
7
8
  ARGV.delete_at 0
8
9
  # @private
9
- LOG_LEVELS = %w[none debug info warn error fatal]
10
+ LOG_LEVELS = %w[none debug info warn error fatal].freeze
10
11
 
11
12
  opt = OptionParser.new <<~BANNER
12
13
  This command will run a client.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # description: Setup application commands.
2
3
  require "optparse"
3
4
  require "discorb/utils/colored_puts"
@@ -6,6 +7,7 @@ ARGV.delete_at 0
6
7
 
7
8
  options = {
8
9
  guilds: nil,
10
+ script: true,
9
11
  }
10
12
 
11
13
  opt = OptionParser.new <<~BANNER
@@ -16,20 +18,21 @@ opt = OptionParser.new <<~BANNER
16
18
  script The script to setup.
17
19
  BANNER
18
20
  opt.on("-g", "--guild ID", Array, "The guild ID to setup, use comma for setup commands in multiple guilds, or `global` for setup global commands.") { |v| options[:guilds] = v }
21
+ opt.on("-s", "--[no-]script", "Whether to run `:setup` event. This may be useful if setup script includes operation that shouldn't run twice. Default to true.") { |v| options[:script] = v }
19
22
  opt.parse!(ARGV)
20
23
 
21
24
  script = ARGV[0]
22
25
  script ||= "main.rb"
23
26
  ENV["DISCORB_CLI_FLAG"] = "setup"
24
27
 
25
- if options[:guilds] == ["global"]
26
- ENV["DISCORB_SETUP_GUILDS"] = "global"
28
+ ENV["DISCORB_SETUP_GUILDS"] = if options[:guilds] == ["global"]
29
+ "global"
27
30
  elsif options[:guilds]
28
- ENV["DISCORB_SETUP_GUILDS"] = options[:guilds].join(",")
29
- else
30
- ENV["DISCORB_SETUP_GUILDS"] = nil
31
+ options[:guilds].join(",")
31
32
  end
32
33
 
34
+ ENV["DISCORB_SETUP_SCRIPT"] = options[:script].to_s if options[:script]
35
+
33
36
  begin
34
37
  load script
35
38
  rescue LoadError
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # description: Show information of your environment.
2
3
 
3
4
  require "etc"
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop: disable Style/Documentation
2
3
 
3
4
  class Time
4
5
  #
@@ -18,22 +19,26 @@ class Time
18
19
  end
19
20
 
20
21
  # @private
21
- class Async::Node
22
- def description
23
- @object_name ||= "#{self.class}:0x#{object_id.to_s(16)}#{@transient ? " transient" : nil}"
22
+ module Async
23
+ class Node
24
+ def description
25
+ @object_name ||= "#{self.class}:0x#{object_id.to_s(16)}#{@transient ? " transient" : nil}"
24
26
 
25
- if @annotation
26
- "#{@object_name} #{@annotation}"
27
- elsif line = self.backtrace(0, 1)&.first
28
- "#{@object_name} #{line}"
29
- else
30
- @object_name
27
+ if @annotation
28
+ "#{@object_name} #{@annotation}"
29
+ elsif line = self.backtrace(0, 1)&.first
30
+ "#{@object_name} #{line}"
31
+ else
32
+ @object_name
33
+ end
31
34
  end
32
- end
33
35
 
34
- def to_s
35
- "\#<#{self.description}>"
36
- end
36
+ def to_s
37
+ "\#<#{self.description}>"
38
+ end
37
39
 
38
- alias inspect to_s
40
+ alias inspect to_s
41
+ end
39
42
  end
43
+
44
+ # rubocop: enable Style/Documentation
@@ -17,7 +17,7 @@ module Discorb
17
17
  ret = {}
18
18
  self.class.events.each do |event, handlers|
19
19
  ret[event] = handlers.map do |handler|
20
- Discorb::EventHandler.new(Proc.new { |*args, **kwargs| instance_exec(*args, **kwargs, &handler[2]) }, handler[0], handler[1])
20
+ Discorb::EventHandler.new(proc { |*args, **kwargs| instance_exec(*args, **kwargs, &handler[2]) }, handler[0], handler[1])
21
21
  end
22
22
  end
23
23
  @events = ret
@@ -27,6 +27,10 @@ module Discorb
27
27
  base.extend(ClassMethods)
28
28
  end
29
29
 
30
+ #
31
+ # @private
32
+ # Module for adding class methods to the extension class.
33
+ #
30
34
  module ClassMethods
31
35
  include Discorb::ApplicationCommand::Handler
32
36
  undef setup_commands