discorb 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/discorb/http.rb CHANGED
@@ -227,7 +227,7 @@ module Discorb
227
227
  end
228
228
 
229
229
  def get_headers(headers, body = "", audit_log_reason = nil)
230
- ret = if body.nil? || body.empty?
230
+ ret = if body.nil? || body == ""
231
231
  { "User-Agent" => USER_AGENT, "authorization" => "Bot #{@client.token}" }
232
232
  else
233
233
  { "User-Agent" => USER_AGENT, "authorization" => "Bot #{@client.token}",
@@ -75,11 +75,15 @@ module Discorb
75
75
 
76
76
  # @!visibility private
77
77
  def make_interaction(client, data)
78
+ interaction = nil
78
79
  descendants.each do |klass|
79
- return klass.make_interaction(client, data) if !klass.interaction_type.nil? && klass.interaction_type == data[:type]
80
+ interaction = klass.make_interaction(client, data) if !klass.interaction_type.nil? && klass.interaction_type == data[:type]
80
81
  end
81
- client.log.warn("Unknown interaction type #{data[:type]}, initialized Interaction")
82
- Interaction.new(client, data)
82
+ if interaction.nil?
83
+ client.log.warn("Unknown interaction type #{data[:type]}, initialized Interaction")
84
+ interaction = Interaction.new(client, data)
85
+ end
86
+ interaction
83
87
  end
84
88
 
85
89
  # @!visibility private
@@ -95,14 +99,14 @@ module Discorb
95
99
  #
96
100
  # Response with `DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE`(`5`).
97
101
  #
98
- # @param [Boolean] hide Whether to hide the response (ephemeral).
102
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
99
103
  #
100
- def defer_source(hide: false)
104
+ def defer_source(ephemeral: false)
101
105
  Async do
102
106
  @client.http.post("/interactions/#{@id}/#{@token}/callback", {
103
107
  type: 5,
104
108
  data: {
105
- flags: (hide ? 1 << 6 : 0),
109
+ flags: (ephemeral ? 1 << 6 : 0),
106
110
  },
107
111
  }).wait
108
112
  @defered = true
@@ -118,9 +122,9 @@ module Discorb
118
122
  # @param [Array<Discorb::Embed>] embeds The embeds to send. (max: 10)
119
123
  # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
120
124
  # @param [Array<Discorb::Components>, Array<Array<Discorb::Components>>] components The components to send.
121
- # @param [Boolean] hide Whether to hide the response (ephemeral).
125
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
122
126
  #
123
- def post(content, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, hide: false)
127
+ def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, ephemeral: false)
124
128
  payload = {}
125
129
  payload[:content] = content if content
126
130
  payload[:tts] = tts
@@ -151,7 +155,7 @@ module Discorb
151
155
  tmp_components << tmp_row
152
156
  payload[:components] = tmp_components.filter { |c| c.length.positive? }.map { |c| { type: 1, components: c.map(&:to_hash) } }
153
157
  end
154
- payload[:flags] = (hide ? 1 << 6 : 0)
158
+ payload[:flags] = (ephemeral ? 1 << 6 : 0)
155
159
  if @responded
156
160
  @client.http.post("/webhooks/#{@id}/#{@token}", { type: 4, data: payload }).wait
157
161
  elsif @defered
@@ -170,14 +174,14 @@ module Discorb
170
174
  #
171
175
  # Response with `DEFERRED_UPDATE_MESSAGE`(`6`).
172
176
  #
173
- # @param [Boolean] hide Whether to hide the response (ephemeral).
177
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
174
178
  #
175
- def defer_update(hide: false)
179
+ def defer_update(ephemeral: false)
176
180
  Async do
177
181
  @client.http.post("/interactions/#{@id}/#{@token}/callback", {
178
182
  type: 7,
179
183
  data: {
180
- flags: (hide ? 1 << 6 : 0),
184
+ flags: (ephemeral ? 1 << 6 : 0),
181
185
  },
182
186
  }).wait
183
187
  end
@@ -192,9 +196,9 @@ module Discorb
192
196
  # @param [Array<Discorb::Embed>] embeds The embeds to send. (max: 10)
193
197
  # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
194
198
  # @param [Array<Discorb::Components>, Array<Array<Discorb::Components>>] components The components to send.
195
- # @param [Boolean] hide Whether to hide the response (ephemeral).
199
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
196
200
  #
197
- def edit(content, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, hide: false)
201
+ def edit(content, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, ephemeral: false)
198
202
  payload = {}
199
203
  payload[:content] = content if content
200
204
  payload[:tts] = tts
@@ -225,7 +229,7 @@ module Discorb
225
229
  tmp_components << tmp_row
226
230
  payload[:components] = tmp_components.filter { |c| c.length.positive? }.map { |c| { type: 1, components: c.map(&:to_hash) } }
227
231
  end
228
- payload[:flags] = (hide ? 1 << 6 : 0)
232
+ payload[:flags] = (ephemeral ? 1 << 6 : 0)
229
233
  @client.http.post("/interactions/#{@id}/#{@token}/callback", { type: 6, data: payload }).wait
230
234
  end
231
235
  end
@@ -238,15 +242,123 @@ module Discorb
238
242
  end
239
243
 
240
244
  #
241
- # Represents a slash command interaction.
245
+ # Represents a command interaction.
242
246
  # @todo Implement this.
243
247
  #
244
- class SlashCommandInteraction < Interaction
248
+ class CommandInteraction < Interaction
245
249
  @interaction_type = 2
246
- @interaction_name = :slash_command
250
+ @interaction_name = :application_command
251
+ include Interaction::SourceResponse
252
+
253
+ #
254
+ # Represents a slash command interaction.
255
+ #
256
+ class SlashCommand < CommandInteraction
257
+ @command_type = 1
258
+
259
+ def _set_data(data)
260
+ super
261
+ Sync do
262
+ name = data[:name]
263
+ options = nil
264
+ if (option = data[:options]&.first)
265
+ case option[:type]
266
+ when 1
267
+ name += " #{option[:name]}"
268
+ options = option[:options]
269
+ when 2
270
+ name += " #{option[:name]}"
271
+ if (option_sub = option[:options]&.first)
272
+ if option_sub[:type] == 1
273
+ name += " #{option_sub[:name]}"
274
+ options = option_sub[:options]
275
+ else
276
+ options = option[:options]
277
+ end
278
+ end
279
+ else
280
+ options = data[:options]
281
+ end
282
+ end
283
+ options ||= []
284
+ options.map! do |option|
285
+ case option[:type]
286
+ when 3, 4, 5, 10
287
+ option[:value]
288
+ when 6
289
+ guild.members[option[:value]] || guild.fetch_member(option[:value]).wait
290
+ when 7
291
+ guild.channels[option[:value]] || guild.fetch_channels.wait.find { |channel| channel.id == option[:value] }
292
+ when 8
293
+ guild.roles[option[:value]] || guild.fetch_roles.wait.find { |role| role.id == option[:value] }
294
+ when 9
295
+ guild.members[option[:value]] || guild.roles[option[:value]] || guild.fetch_member(option[:value]).wait || guild.fetch_roles.wait.find { |role| role.id == option[:value] }
296
+ end
297
+ end
298
+
299
+ unless (command = @client.commands.find { |c| c.to_s == name })
300
+ @client.log.warn "Unknown command name #{name}, ignoreing"
301
+ next
302
+ end
303
+
304
+ command.block.call(self, *options)
305
+ end
306
+ end
307
+ end
308
+
309
+ #
310
+ # Represents a user context menu interaction.
311
+ #
312
+ class UserMenuCommand < CommandInteraction
313
+ @command_type = 2
314
+
315
+ # @return [Discorb::Member, Discorb::User] The target user.
316
+ attr_reader :target
317
+
318
+ def _set_data(data)
319
+ @target = guild.members[data[:target_id]] || Discorb::Member.new(@client, @guild_id, data[:resolved][:users][data[:target_id].to_sym], data[:resolved][:members][data[:target_id].to_sym])
320
+ @client.commands.find { |c| c.name == data[:name] && c.type_raw == 2 }.block.call(self, @target)
321
+ end
322
+ end
323
+
324
+ #
325
+ # Represents a message context menu interaction.
326
+ #
327
+ class MessageMenuCommand < CommandInteraction
328
+ @command_type = 3
329
+
330
+ # @return [Discorb::Message] The target message.
331
+ attr_reader :target
332
+
333
+ def _set_data(data)
334
+ @target = Message.new(@client, data[:resolved][:messages][data[:target_id].to_sym].merge({ guild_id: @guild_id.to_s }))
335
+ @client.commands.find { |c| c.name == data[:name] && c.type_raw == 3 }.block.call(self, @target)
336
+ end
337
+ end
338
+
339
+ private
247
340
 
248
341
  def _set_data(data)
249
- p data
342
+ @name = data[:name]
343
+ end
344
+
345
+ class << self
346
+ # @!visibility private
347
+ attr_reader :command_type
348
+
349
+ # @!visibility private
350
+ def make_interaction(client, data)
351
+ nested_classes.each do |klass|
352
+ return klass.new(client, data) if !klass.command_type.nil? && klass.command_type == data[:data][:type]
353
+ end
354
+ client.log.warn("Unknown command type #{data[:type]}, initialized CommandInteraction")
355
+ CommandInteraction.new(client, data)
356
+ end
357
+
358
+ # @!visibility private
359
+ def nested_classes
360
+ constants.select { |c| const_get(c).is_a? Class }.map { |c| const_get(c) }
361
+ end
250
362
  end
251
363
  end
252
364
 
data/lib/discorb/log.rb CHANGED
@@ -61,7 +61,7 @@ module Discorb
61
61
  return unless @out
62
62
 
63
63
  if @colorize_log
64
- @out.puts("[#{Time.now.iso8601}] #{color}#{name}\e[m -- #{message}")
64
+ @out.puts("\e[2;90m[#{Time.now.iso8601}] #{color}#{name}\e[m -- #{message}")
65
65
  else
66
66
  @out.puts("[#{Time.now.iso8601}] #{name} -- #{message}")
67
67
  end
@@ -78,6 +78,15 @@ module Discorb
78
78
  "@#{name}"
79
79
  end
80
80
 
81
+ #
82
+ # Format the member to `Username#Discriminator` style.
83
+ #
84
+ # @return [String] The formatted member.
85
+ #
86
+ def to_s_user
87
+ "#{username}##{discriminator}"
88
+ end
89
+
81
90
  def name
82
91
  @nick || @username
83
92
  end
@@ -223,8 +232,8 @@ module Discorb
223
232
  @hoisted_role_id = member_data[:hoisted_role]
224
233
  @deaf = member_data[:deaf]
225
234
  @custom_avatar = member_data[:avatar] && Asset.new(member_data[:avatar])
226
- @display_avatar = Asset.new(self, member_data[:avatar] || user_data[:avatar])
227
235
  super(user_data)
236
+ @display_avatar = @avatar || @custom_avatar
228
237
  @client.guilds[@guild_id].members[@id] = self unless @guild_id.nil?
229
238
  @_member_data.update(member_data)
230
239
  end
data/lib/discorb/user.rb CHANGED
@@ -21,6 +21,8 @@ module Discorb
21
21
  # @return [Boolean] Whether the user is a bot.
22
22
  attr_reader :bot
23
23
  alias bot? bot
24
+ # @return [Time] The time the user was created.
25
+ attr_reader :created_at
24
26
 
25
27
  include Discorb::Messageable
26
28
 
@@ -41,6 +43,8 @@ module Discorb
41
43
  "#{@username}##{@discriminator}"
42
44
  end
43
45
 
46
+ alias to_s_user to_s
47
+
44
48
  def inspect
45
49
  "#<#{self.class} #{self}>"
46
50
  end
@@ -123,10 +127,11 @@ module Discorb
123
127
  @id = Snowflake.new(data[:id])
124
128
  @flag = User::Flag.new(data[:public_flags] | (data[:flags] || 0))
125
129
  @discriminator = data[:discriminator]
126
- @avatar = Asset.new(self, data[:avatar])
130
+ @avatar = data[:avatar] ? Asset.new(self, data[:avatar]) : DefaultAvatar.new(data[:discriminator])
127
131
  @bot = data[:bot]
128
132
  @raw_data = data
129
133
  @client.users[@id] = self if !data[:no_cache] && data.is_a?(User)
134
+ @created_at = @id.timestamp
130
135
  @data.update(data)
131
136
  end
132
137
  end
@@ -377,9 +377,20 @@ module Discorb
377
377
  @bot = data[:bot]
378
378
  @id = Snowflake.new(data[:id])
379
379
  @username = data[:username]
380
- @avatar = data[:avatar]
380
+ @avatar = data[:avatar] ? Asset.new(self, data[:avatar]) : DefaultAvatar.new(data[:discriminator])
381
381
  @discriminator = data[:discriminator]
382
382
  end
383
+
384
+ #
385
+ # Format author with `Name#Discriminator` style.
386
+ #
387
+ # @return [String] Formatted author.
388
+ #
389
+ def to_s
390
+ "#{@username}##{@discriminator}"
391
+ end
392
+
393
+ alias to_s_user to_s
383
394
  end
384
395
  end
385
396
 
data/lib/discorb.rb CHANGED
@@ -24,17 +24,17 @@ module Discorb
24
24
  #
25
25
  def macro
26
26
  # NOTE: this method is only for YARD.
27
- puts 'Wow, You found the easter egg!\n\n'
27
+ puts "Wow, You found the easter egg!\n"
28
28
  red = "\e[31m"
29
29
  reset = "\e[m"
30
30
  puts <<~"EASTEREGG"
31
- | . #{red} | #{reset}
32
- __| #{red} |__ #{reset}
33
- / | | __ __ _ #{red} _ | \\ #{reset}
34
- ( | | (__ / / \\ #{red}|/ | ) #{reset}
31
+ . #{red} #{reset}
32
+ | #{red} | #{reset}
33
+ __| | __ __ _ #{red} _ |__ #{reset}
34
+ / | | (__ / / \\ #{red}|/ | \\ #{reset}
35
35
  \\__| | __) \\__ \\_/ #{red}| |__/ #{reset}
36
36
 
37
- https://github.com/discorb-lib/discorb
37
+ Thank you for using this library!
38
38
  EASTEREGG
39
39
  end
40
40
  end
@@ -44,7 +44,7 @@ require_order = %w[common flag dictionary error http intents emoji_table modules
44
44
  %w[application audit_logs color components event extension] +
45
45
  %w[file guild_template image integration interaction invite log permission] +
46
46
  %w[presence reaction role sticker utils voice_state webhook] +
47
- %w[gateway_requests gateway] +
47
+ %w[gateway_requests gateway command] +
48
48
  %w[asset client extend]
49
49
  require_order.each do |name|
50
50
  require_relative "discorb/#{name}.rb"
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.0.8
4
+ version: 0.1.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-29 00:00:00.000000000 Z
11
+ date: 2021-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
@@ -85,8 +85,13 @@ files:
85
85
  - bin/setup
86
86
  - discorb.gemspec
87
87
  - docs/Examples.md
88
+ - docs/application_command.md
88
89
  - docs/events.md
90
+ - docs/extension.md
89
91
  - docs/voice_events.md
92
+ - examples/commands/bookmarker.rb
93
+ - examples/commands/hello.rb
94
+ - examples/commands/inspect.rb
90
95
  - examples/components/authorization_button.rb
91
96
  - examples/components/select_menu.rb
92
97
  - examples/extension/main.rb
@@ -102,6 +107,7 @@ files:
102
107
  - lib/discorb/channel.rb
103
108
  - lib/discorb/client.rb
104
109
  - lib/discorb/color.rb
110
+ - lib/discorb/command.rb
105
111
  - lib/discorb/common.rb
106
112
  - lib/discorb/components.rb
107
113
  - lib/discorb/dictionary.rb