discorb 0.15.0 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +1 -0
- data/.github/workflows/build_main.yml +2 -2
- data/.github/workflows/build_version.yml +1 -1
- data/.github/workflows/codeql-analysis.yml +1 -1
- data/.github/workflows/lint-push.yml +3 -5
- data/.github/workflows/lint.yml +1 -1
- data/.github/workflows/spec.yml +30 -0
- data/.lefthook/commit-msg/validator.rb +5 -0
- data/.rspec +2 -0
- data/.rspec_parallel +2 -0
- data/.rubocop.yml +49 -8
- data/Changelog.md +32 -1
- data/Gemfile +14 -8
- data/Rakefile +46 -25
- data/bin/console +3 -3
- data/docs/Examples.md +1 -1
- data/docs/application_command.md +138 -46
- data/docs/cli/irb.md +2 -2
- data/docs/cli/new.md +14 -9
- data/docs/cli/run.md +7 -11
- data/docs/cli.md +17 -10
- data/docs/events.md +257 -193
- data/docs/extension.md +1 -2
- data/docs/faq.md +0 -1
- data/docs/tutorial.md +12 -12
- data/docs/voice_events.md +106 -106
- data/examples/commands/message.rb +63 -0
- data/examples/commands/permission.rb +18 -0
- data/examples/commands/slash.rb +44 -0
- data/examples/commands/user.rb +51 -0
- data/examples/components/authorization_button.rb +2 -2
- data/examples/components/select_menu.rb +2 -2
- data/examples/extension/main.rb +1 -1
- data/examples/extension/message_expander.rb +5 -2
- data/examples/simple/eval.rb +2 -2
- data/examples/simple/ping_pong.rb +1 -1
- data/examples/simple/rolepanel.rb +2 -2
- data/examples/simple/shard.rb +17 -0
- data/examples/simple/wait_for_message.rb +1 -1
- data/exe/discorb +31 -16
- data/lefthook.yml +45 -0
- data/lib/discorb/allowed_mentions.rb +8 -0
- data/lib/discorb/app_command/command.rb +184 -60
- data/lib/discorb/app_command/common.rb +25 -0
- data/lib/discorb/app_command/handler.rb +116 -34
- data/lib/discorb/app_command.rb +2 -1
- data/lib/discorb/application.rb +17 -7
- data/lib/discorb/asset.rb +10 -2
- data/lib/discorb/attachment.rb +17 -2
- data/lib/discorb/audit_logs.rb +53 -12
- data/lib/discorb/channel/base.rb +108 -0
- data/lib/discorb/channel/category.rb +32 -0
- data/lib/discorb/channel/container.rb +44 -0
- data/lib/discorb/channel/dm.rb +28 -0
- data/lib/discorb/channel/guild.rb +245 -0
- data/lib/discorb/channel/stage.rb +140 -0
- data/lib/discorb/channel/text.rb +345 -0
- data/lib/discorb/channel/thread.rb +321 -0
- data/lib/discorb/channel/voice.rb +79 -0
- data/lib/discorb/channel.rb +2 -1126
- data/lib/discorb/client.rb +160 -64
- data/lib/discorb/common.rb +18 -3
- data/lib/discorb/components/button.rb +7 -7
- data/lib/discorb/components/select_menu.rb +6 -18
- data/lib/discorb/components/text_input.rb +12 -2
- data/lib/discorb/components.rb +1 -1
- data/lib/discorb/dictionary.rb +2 -0
- data/lib/discorb/embed.rb +55 -14
- data/lib/discorb/emoji.rb +59 -5
- data/lib/discorb/emoji_table.rb +4970 -4
- data/lib/discorb/error.rb +7 -1
- data/lib/discorb/event.rb +56 -21
- data/lib/discorb/exe/about.rb +1 -0
- data/lib/discorb/exe/irb.rb +2 -4
- data/lib/discorb/exe/new.rb +95 -28
- data/lib/discorb/exe/run.rb +9 -37
- data/lib/discorb/exe/setup.rb +25 -12
- data/lib/discorb/exe/show.rb +4 -3
- data/lib/discorb/extend.rb +1 -0
- data/lib/discorb/extension.rb +6 -3
- data/lib/discorb/flag.rb +11 -0
- data/lib/discorb/gateway.rb +312 -169
- data/lib/discorb/gateway_requests.rb +4 -7
- data/lib/discorb/guild.rb +255 -89
- data/lib/discorb/guild_template.rb +34 -7
- data/lib/discorb/http.rb +23 -11
- data/lib/discorb/integration.rb +27 -9
- data/lib/discorb/intents.rb +8 -8
- data/lib/discorb/interaction/autocomplete.rb +31 -19
- data/lib/discorb/interaction/command.rb +70 -17
- data/lib/discorb/interaction/components.rb +20 -4
- data/lib/discorb/interaction/modal.rb +0 -1
- data/lib/discorb/interaction/response.rb +73 -22
- data/lib/discorb/interaction/root.rb +29 -14
- data/lib/discorb/interaction.rb +1 -0
- data/lib/discorb/invite.rb +16 -9
- data/lib/discorb/member.rb +46 -5
- data/lib/discorb/message.rb +56 -15
- data/lib/discorb/message_meta.rb +39 -9
- data/lib/discorb/modules.rb +56 -14
- data/lib/discorb/permission.rb +14 -5
- data/lib/discorb/presence.rb +43 -10
- data/lib/discorb/rate_limit.rb +13 -3
- data/lib/discorb/reaction.rb +10 -4
- data/lib/discorb/role.rb +31 -4
- data/lib/discorb/shard.rb +74 -0
- data/lib/discorb/sticker.rb +30 -21
- data/lib/discorb/user.rb +13 -1
- data/lib/discorb/utils/colored_puts.rb +1 -0
- data/lib/discorb/voice_state.rb +30 -8
- data/lib/discorb/webhook.rb +88 -25
- data/lib/discorb.rb +10 -6
- data/po/yard.pot +9 -9
- data/sig/discorb.rbs +7232 -5837
- metadata +23 -6
- data/examples/commands/bookmarker.rb +0 -42
- data/examples/commands/hello.rb +0 -10
- data/examples/commands/inspect.rb +0 -25
- data/lib/discorb/log.rb +0 -81
data/lib/discorb/client.rb
CHANGED
@@ -40,8 +40,6 @@ module Discorb
|
|
40
40
|
attr_reader :emojis
|
41
41
|
# @return [Discorb::Dictionary{Discorb::Snowflake => Discorb::Message}] A dictionary of messages.
|
42
42
|
attr_reader :messages
|
43
|
-
# @return [Discorb::Logger] The logger.
|
44
|
-
attr_reader :log
|
45
43
|
# @return [Array<Discorb::ApplicationCommand::Command>] The commands that the client is using.
|
46
44
|
attr_reader :commands
|
47
45
|
# @return [Float] The ping of the client.
|
@@ -50,12 +48,28 @@ module Discorb
|
|
50
48
|
attr_reader :ping
|
51
49
|
# @return [:initialized, :running, :closed] The status of the client.
|
52
50
|
attr_reader :status
|
53
|
-
# @return [Integer] The session ID of connection.
|
54
|
-
attr_reader :session_id
|
55
51
|
# @return [Hash{String => Discorb::Extension}] The loaded extensions.
|
56
52
|
attr_reader :extensions
|
53
|
+
# @return [Hash{Integer => Discorb::Shard}] The shards of the client.
|
54
|
+
attr_reader :shards
|
57
55
|
# @private
|
58
|
-
|
56
|
+
# @return [Hash{Discorb::Snowflake => Discorb::ApplicationCommand::Command}] The commands on the top level.
|
57
|
+
attr_reader :callable_commands
|
58
|
+
# @private
|
59
|
+
# @return [{String => Thread::Mutex}] A hash of mutexes.
|
60
|
+
attr_reader :mutex
|
61
|
+
|
62
|
+
# @!attribute [r] session_id
|
63
|
+
# @return [String] The session ID of the client or current shard.
|
64
|
+
# @return [nil] If not connected to the gateway.
|
65
|
+
# @!attribute [r] shard
|
66
|
+
# @return [Discorb::Shard] The current shard. This is implemented with Thread variables.
|
67
|
+
# @return [nil] If client has no shard.
|
68
|
+
# @!attribute [r] shard_id
|
69
|
+
# @return [Discorb::Shard] The current shard ID. This is implemented with Thread variables.
|
70
|
+
# @return [nil] If client has no shard.
|
71
|
+
# @!attribute [r] logger
|
72
|
+
# @return [Logger] The logger.
|
59
73
|
|
60
74
|
#
|
61
75
|
# Initializes a new client.
|
@@ -63,16 +77,16 @@ module Discorb
|
|
63
77
|
# @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions that the client is using.
|
64
78
|
# @param [Discorb::Intents] intents The intents that the client is currently using.
|
65
79
|
# @param [Integer] message_caches The number of messages to cache.
|
66
|
-
# @param [
|
67
|
-
# @param [Boolean] colorize_log Whether to colorize the log.
|
80
|
+
# @param [Logger] logger The IO object to use for logging.
|
68
81
|
# @param [:debug, :info, :warn, :error, :critical] log_level The log level.
|
69
82
|
# @param [Boolean] wait_until_ready Whether to delay event dispatch until ready.
|
70
83
|
# @param [Boolean] fetch_member Whether to fetch member on ready. This may slow down the client. Default to `false`.
|
71
|
-
# @param [String] title
|
84
|
+
# @param [String] title
|
85
|
+
# The title of the process. `false` to default of ruby, `nil` to `discorb: User#0000`. Default to `nil`.
|
72
86
|
#
|
73
87
|
def initialize(
|
74
88
|
allowed_mentions: nil, intents: nil, message_caches: 1000,
|
75
|
-
|
89
|
+
logger: nil,
|
76
90
|
wait_until_ready: true, fetch_member: false,
|
77
91
|
title: nil
|
78
92
|
)
|
@@ -80,7 +94,11 @@ module Discorb
|
|
80
94
|
@intents = (intents or Intents.default)
|
81
95
|
@events = {}
|
82
96
|
@api_version = nil
|
83
|
-
@
|
97
|
+
@logger = logger || Logger.new(
|
98
|
+
$stdout,
|
99
|
+
progname: "discorb",
|
100
|
+
level: Logger::ERROR,
|
101
|
+
)
|
84
102
|
@user = nil
|
85
103
|
@users = Discorb::Dictionary.new
|
86
104
|
@channels = Discorb::Dictionary.new
|
@@ -95,11 +113,13 @@ module Discorb
|
|
95
113
|
@tasks = []
|
96
114
|
@conditions = {}
|
97
115
|
@commands = []
|
98
|
-
@
|
116
|
+
@callable_commands = []
|
99
117
|
@status = :initialized
|
100
118
|
@fetch_member = fetch_member
|
101
119
|
@title = title
|
102
120
|
@extensions = {}
|
121
|
+
@mutex = {}
|
122
|
+
@shards = {}
|
103
123
|
set_default_events
|
104
124
|
end
|
105
125
|
|
@@ -180,18 +200,22 @@ module Discorb
|
|
180
200
|
events << event_method
|
181
201
|
end
|
182
202
|
if events.nil?
|
183
|
-
|
203
|
+
logger.debug "Event #{event_name} doesn't have any proc, skipping"
|
184
204
|
next
|
185
205
|
end
|
186
|
-
|
206
|
+
logger.debug "Dispatching event #{event_name}"
|
187
207
|
events.each do |block|
|
188
208
|
Async do
|
189
209
|
Async(annotation: "Discorb event: #{event_name}") do |_task|
|
190
210
|
@events[event_name].delete(block) if block.is_a?(Discorb::EventHandler) && block.metadata[:once]
|
191
211
|
block.call(*args)
|
192
|
-
|
212
|
+
logger.debug "Dispatched proc with ID #{block.id.inspect}"
|
193
213
|
rescue StandardError, ScriptError => e
|
194
|
-
|
214
|
+
if event_name == :error
|
215
|
+
raise e
|
216
|
+
else
|
217
|
+
dispatch(:error, event_name, args, e)
|
218
|
+
end
|
195
219
|
end
|
196
220
|
end
|
197
221
|
end
|
@@ -261,7 +285,13 @@ module Discorb
|
|
261
285
|
#
|
262
286
|
def fetch_invite(code, with_count: true, with_expiration: true)
|
263
287
|
Async do
|
264
|
-
_resp, data = @http.request(
|
288
|
+
_resp, data = @http.request(
|
289
|
+
Route.new(
|
290
|
+
"/invites/#{code}?with_count=#{with_count}&with_expiration=#{with_expiration}",
|
291
|
+
"//invites/:code",
|
292
|
+
:get
|
293
|
+
)
|
294
|
+
).wait
|
265
295
|
Invite.new(self, data, false)
|
266
296
|
end
|
267
297
|
end
|
@@ -309,10 +339,11 @@ module Discorb
|
|
309
339
|
activities: [],
|
310
340
|
status: status,
|
311
341
|
since: nil,
|
342
|
+
afk: nil,
|
312
343
|
}
|
313
344
|
payload[:activities] = [activity.to_hash] unless activity.nil?
|
314
345
|
payload[:status] = status unless status.nil?
|
315
|
-
if
|
346
|
+
if connection
|
316
347
|
Async do
|
317
348
|
send_gateway(3, **payload)
|
318
349
|
end
|
@@ -371,6 +402,7 @@ module Discorb
|
|
371
402
|
case ext
|
372
403
|
when Class
|
373
404
|
raise ArgumentError, "#{ext} is not a extension" unless ext < Discorb::Extension
|
405
|
+
|
374
406
|
ins = ext.new(self, ...)
|
375
407
|
when Discorb::Extension
|
376
408
|
ins = ext
|
@@ -399,13 +431,13 @@ module Discorb
|
|
399
431
|
|
400
432
|
cls = ins.class
|
401
433
|
cls.loaded(self, ...) if cls.respond_to? :loaded
|
402
|
-
ins.class.
|
434
|
+
ins.class.callable_commands.each do |cmd|
|
403
435
|
unless cmd.respond_to? :self_replaced
|
404
436
|
cmd.define_singleton_method(:extension) { ins.class.name }
|
405
437
|
cmd.replace_block(ins)
|
406
438
|
cmd.block.define_singleton_method(:self_replaced) { true }
|
407
439
|
end
|
408
|
-
@
|
440
|
+
@callable_commands << cmd
|
409
441
|
end
|
410
442
|
@extensions[ins.class.name] = ins
|
411
443
|
ins
|
@@ -423,15 +455,16 @@ module Discorb
|
|
423
455
|
#
|
424
456
|
# @note If the token is nil, you should use `discorb run` with the `-e` or `--env` option.
|
425
457
|
#
|
426
|
-
def run(token = nil)
|
427
|
-
token ||= ENV
|
458
|
+
def run(token = nil, shards: nil, shard_count: nil)
|
459
|
+
token ||= ENV.fetch("DISCORB_CLI_TOKEN", nil)
|
428
460
|
raise ArgumentError, "Token is not specified, and -e/--env is not specified" if token.nil?
|
429
|
-
|
461
|
+
|
462
|
+
case ENV.fetch("DISCORB_CLI_FLAG", nil)
|
430
463
|
when nil
|
431
|
-
start_client(token)
|
464
|
+
start_client(token, shards: shards, shard_count: shard_count)
|
432
465
|
when "run"
|
433
466
|
before_run(token)
|
434
|
-
start_client(token)
|
467
|
+
start_client(token, shards: shards, shard_count: shard_count)
|
435
468
|
when "setup"
|
436
469
|
run_setup(token)
|
437
470
|
end
|
@@ -441,44 +474,52 @@ module Discorb
|
|
441
474
|
# Stops the client.
|
442
475
|
#
|
443
476
|
def close!
|
444
|
-
@
|
477
|
+
if @shards.any?
|
478
|
+
@shards.each(&:close!)
|
479
|
+
else
|
480
|
+
@connection.send_close
|
481
|
+
end
|
445
482
|
@tasks.each(&:stop)
|
446
483
|
@status = :closed
|
447
|
-
|
484
|
+
end
|
485
|
+
|
486
|
+
def session_id
|
487
|
+
if shard
|
488
|
+
shard.session_id
|
489
|
+
else
|
490
|
+
@session_id
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
def logger
|
495
|
+
shard&.logger || @logger
|
496
|
+
end
|
497
|
+
|
498
|
+
def shard
|
499
|
+
Thread.current.thread_variable_get("shard")
|
500
|
+
end
|
501
|
+
|
502
|
+
def shard_id
|
503
|
+
Thread.current.thread_variable_get("shard_id")
|
448
504
|
end
|
449
505
|
|
450
506
|
private
|
451
507
|
|
452
508
|
def before_run(token)
|
453
509
|
require "json"
|
454
|
-
options = JSON.parse(ENV
|
510
|
+
options = JSON.parse(ENV.fetch("DISCORB_CLI_OPTIONS", nil), symbolize_names: true)
|
455
511
|
setup_commands(token) if options[:setup]
|
456
|
-
if options[:log_level]
|
457
|
-
if options[:log_level] == "none"
|
458
|
-
@log.out = nil
|
459
|
-
else
|
460
|
-
@log.out = case options[:log_file]
|
461
|
-
when nil, "stderr"
|
462
|
-
$stderr
|
463
|
-
when "stdout"
|
464
|
-
$stdout
|
465
|
-
else
|
466
|
-
::File.open(options[:log_file], "a")
|
467
|
-
end
|
468
|
-
@log.level = options[:log_level].to_sym
|
469
|
-
@log.colorize_log = options[:log_color].nil? ? @log.out.isatty : options[:log_color]
|
470
|
-
end
|
471
|
-
end
|
472
512
|
end
|
473
513
|
|
474
514
|
def run_setup(token)
|
475
515
|
guild_ids = "global"
|
476
|
-
if guilds = ENV
|
516
|
+
if guilds = ENV.fetch("DISCORB_SETUP_GUILDS", nil)
|
477
517
|
guild_ids = guilds.split(",")
|
478
518
|
end
|
479
519
|
guild_ids = false if guild_ids == ["global"]
|
480
520
|
setup_commands(token, guild_ids: guild_ids).wait
|
481
|
-
|
521
|
+
clear_commands(token, ENV.fetch("DISCORB_SETUP_CLEAR_GUILDS", "").split(","))
|
522
|
+
if ENV.fetch("DISCORB_SETUP_SCRIPT", nil) == "true"
|
482
523
|
@events[:setup]&.each do |event|
|
483
524
|
event.call
|
484
525
|
end
|
@@ -486,38 +527,93 @@ module Discorb
|
|
486
527
|
end
|
487
528
|
end
|
488
529
|
|
489
|
-
def
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
530
|
+
def set_status(status, shard)
|
531
|
+
if shard.nil?
|
532
|
+
@status = status
|
533
|
+
else
|
534
|
+
@shards[shard].status = status
|
535
|
+
end
|
536
|
+
end
|
537
|
+
|
538
|
+
def connection
|
539
|
+
if shard_id
|
540
|
+
@shards[shard_id].connection
|
541
|
+
else
|
542
|
+
@connection
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
def connection=(value)
|
547
|
+
if shard_id
|
548
|
+
@shards[shard_id].connection = value
|
549
|
+
else
|
550
|
+
@connection = value
|
551
|
+
end
|
552
|
+
end
|
553
|
+
|
554
|
+
def session_id=(value)
|
555
|
+
if shard_id
|
556
|
+
@shards[shard_id].session_id = value
|
557
|
+
else
|
558
|
+
@session_id = value
|
559
|
+
end
|
560
|
+
end
|
561
|
+
|
562
|
+
def start_client(token, shards: nil, shard_count: nil)
|
563
|
+
@token = token.to_s
|
564
|
+
@shard_count = shard_count
|
565
|
+
Signal.trap(:SIGINT) do
|
566
|
+
logger.info "SIGINT received, closing..."
|
567
|
+
Signal.trap(:SIGINT, "DEFAULT")
|
568
|
+
close!
|
569
|
+
end
|
570
|
+
if shards.nil?
|
571
|
+
main_loop(nil)
|
572
|
+
else
|
573
|
+
@shards = shards.map.with_index do |shard, i|
|
574
|
+
Shard.new(self, shard, shard_count, i)
|
495
575
|
end
|
496
|
-
@
|
497
|
-
|
498
|
-
@main_task = Async do
|
499
|
-
@status = :running
|
500
|
-
connect_gateway(false).wait
|
501
|
-
rescue StandardError
|
502
|
-
@status = :stopped
|
503
|
-
@close_condition.signal
|
504
|
-
raise
|
576
|
+
@shards[..-1].each_with_index do |shard, i|
|
577
|
+
shard.next_shard = @shards[i + 1]
|
505
578
|
end
|
506
|
-
@
|
507
|
-
|
579
|
+
@shards.each { |s| s.thread.join }
|
580
|
+
end
|
581
|
+
end
|
582
|
+
|
583
|
+
def main_loop(shard)
|
584
|
+
set_status(:running, shard)
|
585
|
+
connect_gateway(false).wait
|
586
|
+
rescue StandardError
|
587
|
+
set_status(:closed, shard)
|
588
|
+
raise
|
589
|
+
end
|
590
|
+
|
591
|
+
def main_task
|
592
|
+
if shard_id
|
593
|
+
shard.main_task
|
594
|
+
else
|
595
|
+
@main_task
|
596
|
+
end
|
597
|
+
end
|
598
|
+
|
599
|
+
def main_task=(value)
|
600
|
+
if shard_id
|
601
|
+
shard.main_task = value
|
602
|
+
else
|
603
|
+
@main_task = value
|
508
604
|
end
|
509
605
|
end
|
510
606
|
|
511
607
|
def set_default_events
|
512
608
|
on :error, override: true do |event_name, _args, e|
|
513
609
|
message = "An error occurred while dispatching #{event_name}:\n#{e.full_message}"
|
514
|
-
|
610
|
+
logger.error message
|
515
611
|
end
|
516
612
|
|
517
613
|
once :standby do
|
518
614
|
next if @title == false
|
519
615
|
|
520
|
-
title = @title || ENV
|
616
|
+
title = @title || ENV.fetch("DISCORB_CLI_TITLE", nil) || "discorb: #{@user}"
|
521
617
|
Process.setproctitle title
|
522
618
|
end
|
523
619
|
end
|
data/lib/discorb/common.rb
CHANGED
@@ -4,7 +4,9 @@ module Discorb
|
|
4
4
|
# @return [String] The API base URL.
|
5
5
|
API_BASE_URL = "https://discord.com/api/v10"
|
6
6
|
# @return [String] The version of discorb.
|
7
|
-
VERSION = "0.
|
7
|
+
VERSION = "0.17.0"
|
8
|
+
# @return [Array<Integer>] The version array of discorb.
|
9
|
+
VERSION_ARRAY = VERSION.split(".").map(&:to_i).freeze
|
8
10
|
# @return [String] The user agent for the bot.
|
9
11
|
USER_AGENT = "DiscordBot (https://discorb-lib.github.io #{VERSION}) Ruby/#{RUBY_VERSION}".freeze
|
10
12
|
|
@@ -25,8 +27,11 @@ module Discorb
|
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
|
-
|
30
|
+
def inspect
|
31
|
+
"#<#{self.class}: #{@id}>"
|
32
|
+
end
|
29
33
|
|
34
|
+
# @private
|
30
35
|
def hash
|
31
36
|
@id.hash
|
32
37
|
end
|
@@ -37,7 +42,12 @@ module Discorb
|
|
37
42
|
#
|
38
43
|
# @see https://discord.com/developers/docs/reference#snowflakes Official Discord API docs
|
39
44
|
class Snowflake < String
|
45
|
+
#
|
46
|
+
# Initialize new snowflake.
|
40
47
|
# @private
|
48
|
+
#
|
49
|
+
# @param [#to_s] value The value of the snowflake.
|
50
|
+
#
|
41
51
|
def initialize(value)
|
42
52
|
@value = value.to_i
|
43
53
|
super(@value.to_s)
|
@@ -92,7 +102,7 @@ module Discorb
|
|
92
102
|
end
|
93
103
|
|
94
104
|
def timestamp
|
95
|
-
Time.at(((@value >> 22) + 1_420_070_400_000) / 1000)
|
105
|
+
Time.at(((@value >> 22) + 1_420_070_400_000) / 1000.0)
|
96
106
|
end
|
97
107
|
|
98
108
|
def worker_id
|
@@ -127,6 +137,10 @@ module Discorb
|
|
127
137
|
@method = method
|
128
138
|
end
|
129
139
|
|
140
|
+
def inspect
|
141
|
+
"#<#{self.class} #{self.identifier}>"
|
142
|
+
end
|
143
|
+
|
130
144
|
def hash
|
131
145
|
@url.hash
|
132
146
|
end
|
@@ -138,6 +152,7 @@ module Discorb
|
|
138
152
|
def major_param
|
139
153
|
param_type = @key.split("/").find { |k| k.start_with?(":") }
|
140
154
|
return "" unless param_type
|
155
|
+
|
141
156
|
param = url.gsub(API_BASE_URL, "").split("/")[@key.split("/").index(param_type) - 1]
|
142
157
|
%w[:channel_id :guild_id :webhook_id].include?(param_type) ? param : ""
|
143
158
|
end
|
@@ -21,7 +21,9 @@ module Discorb
|
|
21
21
|
attr_accessor :disabled
|
22
22
|
alias disabled? disabled
|
23
23
|
|
24
|
-
@
|
24
|
+
# @private
|
25
|
+
# @return [{Symbol => Integer}] The mapping of button styles.
|
26
|
+
STYLES = {
|
25
27
|
primary: 1,
|
26
28
|
secondary: 2,
|
27
29
|
success: 3,
|
@@ -51,7 +53,8 @@ module Discorb
|
|
51
53
|
#
|
52
54
|
# Converts the button to a hash.
|
53
55
|
#
|
54
|
-
# @see https://discord.com/developers/docs/interactions/message-components#button-object-button-structure
|
56
|
+
# @see https://discord.com/developers/docs/interactions/message-components#button-object-button-structure
|
57
|
+
# Official Discord API docs
|
55
58
|
# @return [Hash] A hash representation of the button.
|
56
59
|
#
|
57
60
|
def to_hash
|
@@ -59,7 +62,7 @@ module Discorb
|
|
59
62
|
{
|
60
63
|
type: 2,
|
61
64
|
label: @label,
|
62
|
-
style:
|
65
|
+
style: STYLES[@style],
|
63
66
|
url: @url,
|
64
67
|
emoji: @emoji&.to_hash,
|
65
68
|
disabled: @disabled,
|
@@ -68,7 +71,7 @@ module Discorb
|
|
68
71
|
{
|
69
72
|
type: 2,
|
70
73
|
label: @label,
|
71
|
-
style:
|
74
|
+
style: STYLES[@style],
|
72
75
|
custom_id: @custom_id,
|
73
76
|
emoji: @emoji&.to_hash,
|
74
77
|
disabled: @disabled,
|
@@ -81,9 +84,6 @@ module Discorb
|
|
81
84
|
end
|
82
85
|
|
83
86
|
class << self
|
84
|
-
# @private
|
85
|
-
attr_reader :styles
|
86
|
-
|
87
87
|
#
|
88
88
|
# Creates a new button from a hash.
|
89
89
|
#
|
@@ -37,7 +37,8 @@ module Discorb
|
|
37
37
|
#
|
38
38
|
# Converts the select menu to a hash.
|
39
39
|
#
|
40
|
-
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure
|
40
|
+
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure
|
41
|
+
# Official Discord API docs
|
41
42
|
# @return [Hash] A hash representation of the select menu.
|
42
43
|
#
|
43
44
|
def to_hash
|
@@ -109,7 +110,8 @@ module Discorb
|
|
109
110
|
#
|
110
111
|
# Converts the option to a hash.
|
111
112
|
#
|
112
|
-
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure
|
113
|
+
# @see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure
|
114
|
+
# Official Discord API docs
|
113
115
|
# @return [Hash] Hash representation of the option.
|
114
116
|
#
|
115
117
|
def to_hash
|
@@ -122,22 +124,8 @@ module Discorb
|
|
122
124
|
}
|
123
125
|
end
|
124
126
|
|
125
|
-
|
126
|
-
|
127
|
-
case emoji
|
128
|
-
when UnicodeEmoji
|
129
|
-
{
|
130
|
-
id: nil,
|
131
|
-
name: emoji.to_s,
|
132
|
-
animated: false,
|
133
|
-
}
|
134
|
-
when CustomEmoji
|
135
|
-
{
|
136
|
-
id: emoji.id,
|
137
|
-
name: emoji.name,
|
138
|
-
animated: emoji.animated?,
|
139
|
-
}
|
140
|
-
end
|
127
|
+
def inspect
|
128
|
+
"#<#{self.class} #{@label}: #{@value}>"
|
141
129
|
end
|
142
130
|
|
143
131
|
class << self
|
@@ -40,7 +40,16 @@ module Discorb
|
|
40
40
|
# @param [String, nil] value The prefilled value of the text input.
|
41
41
|
# @param [String, nil] placeholder The placeholder of the text input.
|
42
42
|
#
|
43
|
-
def initialize(
|
43
|
+
def initialize(
|
44
|
+
label,
|
45
|
+
custom_id,
|
46
|
+
style,
|
47
|
+
min_length: nil,
|
48
|
+
max_length: nil,
|
49
|
+
required: false,
|
50
|
+
value: nil,
|
51
|
+
placeholder: nil
|
52
|
+
)
|
44
53
|
@label = label
|
45
54
|
@custom_id = custom_id
|
46
55
|
@style = style
|
@@ -54,7 +63,8 @@ module Discorb
|
|
54
63
|
#
|
55
64
|
# Converts the select menu to a hash.
|
56
65
|
#
|
57
|
-
# @see https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure
|
66
|
+
# @see https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure
|
67
|
+
# Official Discord API docs
|
58
68
|
# @return [Hash] A hash representation of the text input.
|
59
69
|
#
|
60
70
|
def to_hash
|
data/lib/discorb/components.rb
CHANGED