discorb 0.5.6 → 0.6.0

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.
data/lib/discorb/emoji.rb CHANGED
@@ -86,7 +86,7 @@ module Discorb
86
86
  # @param [Array<Discorb::Role>] roles The new roles that can use this emoji.
87
87
  # @param [String] reason The reason for editing the emoji.
88
88
  #
89
- # @return [self] The edited emoji.
89
+ # @return [Async::Task<self>] The edited emoji.
90
90
  #
91
91
  def edit(name: :unset, roles: :unset, reason: nil)
92
92
  Async do
@@ -107,7 +107,7 @@ module Discorb
107
107
  #
108
108
  # @param [String] reason The reason for deleting the emoji.
109
109
  #
110
- # @return [self] The deleted emoji.
110
+ # @return [Async::Task<self>] The deleted emoji.
111
111
  #
112
112
  def delete!(reason: nil)
113
113
  Async do
data/lib/discorb/error.rb CHANGED
@@ -67,6 +67,12 @@ module Discorb
67
67
  end
68
68
  end
69
69
 
70
+ #
71
+ # Represents a 401 error.
72
+ #
73
+ class UnauthorizedError < HTTPError
74
+ end
75
+
70
76
  #
71
77
  # Represents a 403 error.
72
78
  #
@@ -2,6 +2,7 @@
2
2
  require "optparse"
3
3
  require "json"
4
4
  require "discorb/utils/colored_puts"
5
+ require "io/console"
5
6
 
6
7
  ARGV.delete_at 0
7
8
  # @!visibility private
@@ -20,6 +21,7 @@ options = {
20
21
  log_file: nil,
21
22
  log_color: nil,
22
23
  setup: nil,
24
+ token: false,
23
25
  }
24
26
  opt.on("-d", "--deamon", "Run as a daemon.") { |v| options[:daemon] = v }
25
27
  opt.on("-l", "--log-level LEVEL", "Log level.") do |v|
@@ -33,6 +35,7 @@ end
33
35
  opt.on("-f", "--log-file FILE", "File to write log to.") { |v| options[:log_file] = v }
34
36
  opt.on("-c", "--[no-]log-color", "Whether to colorize log output.") { |v| options[:log_color] = v }
35
37
  opt.on("-s", "--setup", "Whether to setup application commands.") { |v| options[:setup] = v }
38
+ opt.on("-t", "--token [ENV]", "The name of the environment variable to use for token, or just `-t` or `--token` for intractive prompt.") { |v| options[:token] = v }
36
39
  opt.parse!(ARGV)
37
40
 
38
41
  script = ARGV[0]
@@ -42,6 +45,15 @@ script ||= "main.rb"
42
45
  ENV["DISCORB_CLI_FLAG"] = "run"
43
46
  ENV["DISCORB_CLI_OPTIONS"] = JSON.generate(options)
44
47
 
48
+ if options[:token]
49
+ ENV["DISCORB_CLI_TOKEN"] = ENV[options[:token]]
50
+ raise "#{options[:token]} is not set." if ENV["DISCORB_CLI_TOKEN"].nil?
51
+ elsif options[:token].nil? || options[:token] == "-"
52
+ print "\e[90mPlease enter your token: \e[m"
53
+ ENV["DISCORB_CLI_TOKEN"] = $stdin.noecho(&:gets).chomp
54
+ puts ""
55
+ end
56
+
45
57
  begin
46
58
  load script
47
59
  rescue LoadError
@@ -4,19 +4,32 @@ require "discorb/utils/colored_puts"
4
4
 
5
5
  ARGV.delete_at 0
6
6
 
7
+ options = {
8
+ guilds: nil,
9
+ }
10
+
7
11
  opt = OptionParser.new <<~BANNER
8
12
  This command will setup application commands.
9
13
 
10
- Usage: discorb setup [script]
14
+ Usage: discorb setup [options] [script]
11
15
 
12
16
  script The script to setup.
13
17
  BANNER
18
+ 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 }
14
19
  opt.parse!(ARGV)
15
20
 
16
21
  script = ARGV[0]
17
22
  script ||= "main.rb"
18
23
  ENV["DISCORB_CLI_FLAG"] = "setup"
19
24
 
25
+ if options[:guilds] == ["global"]
26
+ ENV["DISCORB_SETUP_GUILDS"] = "global"
27
+ elsif options[:guilds]
28
+ ENV["DISCORB_SETUP_GUILDS"] = options[:guilds].join(",")
29
+ else
30
+ ENV["DISCORB_SETUP_GUILDS"] = nil
31
+ end
32
+
20
33
  begin
21
34
  load script
22
35
  rescue LoadError
@@ -49,7 +49,7 @@ module Discorb
49
49
 
50
50
  # @return [Hash{Symbol => Array<Discorb::Event>}] The events of the extension.
51
51
  attr_reader :events
52
- # @return [Array<Discorb::Command::Command] The commands of the extension.
52
+ # @return [Array<Discorb::Command::Command>] The commands of the extension.
53
53
  attr_reader :commands
54
54
  # @private
55
55
  attr_reader :bottom_commands
@@ -90,7 +90,7 @@ module Discorb
90
90
  #
91
91
  # @param [Boolean] force Whether to force fetching the message.
92
92
  #
93
- # @return [Discorb::Message] The message.
93
+ # @return [Async::Task<Discorb::Message>] The message.
94
94
  def fetch_message(force: false)
95
95
  Async do
96
96
  next @message if !force && @message
@@ -139,7 +139,7 @@ module Discorb
139
139
  #
140
140
  # @param [Boolean] force Whether to force fetching the message.
141
141
  #
142
- # @return [Discorb::Message] The message.
142
+ # @return [Async::Task<Discorb::Message>] The message.
143
143
  def fetch_message(force: false)
144
144
  Async do
145
145
  next @message if !force && @message
@@ -191,7 +191,7 @@ module Discorb
191
191
  #
192
192
  # @param [Boolean] force Whether to force fetching the message.
193
193
  #
194
- # @return [Discorb::Message] The message.
194
+ # @return [Async::Task<Discorb::Message>] The message.
195
195
  def fetch_message(force: false)
196
196
  Async do
197
197
  next @message if !force && @message
@@ -265,7 +265,7 @@ module Discorb
265
265
  # @macro async
266
266
  # @macro http
267
267
  #
268
- # @return [Discorb::Message] The message.
268
+ # @return [Async::Task<Discorb::Message>] The message.
269
269
  def fetch_message
270
270
  Async do
271
271
  channel.fetch_message(@id).wait
@@ -584,6 +584,11 @@ module Discorb
584
584
  @session_id = data[:session_id]
585
585
  @user = ClientUser.new(self, data[:user])
586
586
  @uncached_guilds = data[:guilds].map { |g| g[:id] }
587
+ if @uncached_guilds == []
588
+ @ready = true
589
+ dispatch(:ready)
590
+ @log.info("Guilds were cached")
591
+ end
587
592
  @tasks << handle_heartbeat(@heartbeat_interval)
588
593
  when "GUILD_CREATE"
589
594
  if @uncached_guilds.include?(data[:id])
data/lib/discorb/guild.rb CHANGED
@@ -175,7 +175,7 @@ module Discorb
175
175
  # @macro http
176
176
  # @note This querys the API every time. We recommend using {#emojis} instead.
177
177
  #
178
- # @return [Discorb::Dictionary{Discorb::Snowflake => Discorb::CustomEmoji}] A dictionary of emoji in the guild.
178
+ # @return [Async::Task<Discorb::Dictionary{Discorb::Snowflake => Discorb::CustomEmoji}>] A dictionary of emoji in the guild.
179
179
  #
180
180
  def fetch_emoji_list
181
181
  Async do
@@ -201,7 +201,7 @@ module Discorb
201
201
  #
202
202
  # @param [#to_s] id The emoji id.
203
203
  #
204
- # @return [Discorb::CustomEmoji] The emoji with the given id.
204
+ # @return [Async::Task<Discorb::CustomEmoji>] The emoji with the given id.
205
205
  #
206
206
  def fetch_emoji(id)
207
207
  _resp, data = @client.http.get("/guilds/#{@id}/emojis/#{id}").wait
@@ -217,7 +217,7 @@ module Discorb
217
217
  # @param [Discorb::Image] image The image of the emoji.
218
218
  # @param [Array<Discorb::Role>] roles A list of roles to give the emoji.
219
219
  #
220
- # @return [Discorb::CustomEmoji] The created emoji.
220
+ # @return [Async::Task<Discorb::CustomEmoji>] The created emoji.
221
221
  #
222
222
  def create_emoji(name, image, roles: [])
223
223
  _resp, data = @client.http.post(
@@ -236,7 +236,7 @@ module Discorb
236
236
  # @macro async
237
237
  # @macro http
238
238
  #
239
- # @return [Array<Discorb::Webhook>] A list of webhooks in the guild.
239
+ # @return [Async::Task<Array<Discorb::Webhook>>] A list of webhooks in the guild.
240
240
  #
241
241
  def fetch_webhooks
242
242
  Async do
@@ -250,7 +250,7 @@ module Discorb
250
250
  # @macro async
251
251
  # @macro http
252
252
  #
253
- # @return [Discorb::AuditLog] The audit log of the guild.
253
+ # @return [Async::Task<Discorb::AuditLog>] The audit log of the guild.
254
254
  #
255
255
  def fetch_audit_log
256
256
  Async do
@@ -264,7 +264,7 @@ module Discorb
264
264
  # @macro async
265
265
  # @macro http
266
266
  #
267
- # @return [Array<Discorb::Channel>] A list of channels in the guild.
267
+ # @return [Async::Task<Array<Discorb::Channel>>] A list of channels in the guild.
268
268
  #
269
269
  def fetch_channels
270
270
  Async do
@@ -288,7 +288,7 @@ module Discorb
288
288
  # @param [Discorb::CategoryChannel] parent The parent of the channel.
289
289
  # @param [String] reason The reason for creating the channel.
290
290
  #
291
- # @return [Discorb::TextChannel] The created text channel.
291
+ # @return [Async::Task<Discorb::TextChannel>] The created text channel.
292
292
  #
293
293
  def create_text_channel(
294
294
  name, topic: nil, rate_limit_per_user: nil, slowmode: nil, position: nil, nsfw: nil, permission_overwrites: nil, parent: nil, reason: nil
@@ -333,7 +333,7 @@ module Discorb
333
333
  # @param [Discorb::CategoryChannel] parent The parent of the channel.
334
334
  # @param [String] reason The reason for creating the channel.
335
335
  #
336
- # @return [Discorb::VoiceChannel] The created voice channel.
336
+ # @return [Async::Task<Discorb::VoiceChannel>] The created voice channel.
337
337
  #
338
338
  def create_voice_channel(
339
339
  name, bitrate: 64, user_limit: nil, position: nil, permission_overwrites: nil, parent: nil, reason: nil
@@ -373,7 +373,7 @@ module Discorb
373
373
  # @param [Discorb::CategoryChannel] parent The parent of the channel.
374
374
  # @param [String] reason The reason for creating the channel.
375
375
  #
376
- # @return [Discorb::CategoryChannel] The created category channel.
376
+ # @return [Async::Task<Discorb::CategoryChannel>] The created category channel.
377
377
  #
378
378
  def create_category_channel(name, position: nil, permission_overwrites: nil, parent: nil, reason: nil)
379
379
  Async do
@@ -412,7 +412,7 @@ module Discorb
412
412
  # @param [Discorb::CategoryChannel] parent The parent of the channel.
413
413
  # @param [String] reason The reason for creating the channel.
414
414
  #
415
- # @return [Discorb::StageChannel] The created stage channel.
415
+ # @return [Async::Task<Discorb::StageChannel>] The created stage channel.
416
416
  #
417
417
  def create_stage_channel(name, bitrate: 64, position: nil, permission_overwrites: nil, parent: nil, reason: nil)
418
418
  Async do
@@ -453,7 +453,7 @@ module Discorb
453
453
  # @param [Discorb::CategoryChannel] parent The parent of the channel.
454
454
  # @param [String] reason The reason for creating the channel.
455
455
  #
456
- # @return [Discorb::NewsChannel] The created news channel.
456
+ # @return [Async::Task<Discorb::NewsChannel>] The created news channel.
457
457
  #
458
458
  def create_news_channel(
459
459
  name, topic: nil, rate_limit_per_user: nil, slowmode: nil, position: nil, nsfw: nil, permission_overwrites: nil, parent: nil, reason: nil
@@ -489,7 +489,7 @@ module Discorb
489
489
  # @macro async
490
490
  # @macro http
491
491
  #
492
- # @return [Array<Discorb::ThreadChannel>] The list of threads.
492
+ # @return [Async::Task<Array<Discorb::ThreadChannel>>] The list of threads.
493
493
  #
494
494
  def fetch_active_threads
495
495
  Async do
@@ -506,8 +506,8 @@ module Discorb
506
506
  #
507
507
  # @param [#to_s] id The ID of the member to fetch.
508
508
  #
509
- # @return [Discorb::Member] The member.
510
- # @return [nil] If the member is not found.
509
+ # @return [Async::Task<Discorb::Member>] The member.
510
+ # @return [Async::Task<nil>] If the member is not found.
511
511
  #
512
512
  def fetch_member(id)
513
513
  Async do
@@ -527,7 +527,7 @@ module Discorb
527
527
  # @param [String] name The name of the member to search for.
528
528
  # @param [Integer] limit The maximum number of members to return.
529
529
  #
530
- # @return [Array<Discorb::Member>] The list of members.
530
+ # @return [Async::Task<Array<Discorb::Member>>] The list of members.
531
531
  #
532
532
  def fetch_members_named(name, limit: 1)
533
533
  Async do
@@ -541,8 +541,8 @@ module Discorb
541
541
  # @macro async
542
542
  # @macro http
543
543
  #
544
- # @return [Discorb::Member] The member.
545
- # @return [nil] If the member is not found.
544
+ # @return [Async::Task<Discorb::Member>] The member.
545
+ # @return [Async::Task<nil>] If the member is not found.
546
546
  #
547
547
  def fetch_member_named(...)
548
548
  Async do
@@ -585,7 +585,7 @@ module Discorb
585
585
  # @macro async
586
586
  # @macro http
587
587
  #
588
- # @return [Array<Discorb::Guild::Ban>] The list of bans.
588
+ # @return [Async::Task<Array<Discorb::Guild::Ban>>] The list of bans.
589
589
  #
590
590
  def fetch_bans
591
591
  Async do
@@ -601,8 +601,8 @@ module Discorb
601
601
  #
602
602
  # @param [Discorb::User] user The user to fetch.
603
603
  #
604
- # @return [Discorb::Guild::Ban] The ban.
605
- # @return [nil] If the ban is not found.
604
+ # @return [Async::Task<Discorb::Guild::Ban>] The ban.
605
+ # @return [Async::Task<nil>] If the ban is not found.
606
606
  #
607
607
  def fetch_ban(user)
608
608
  Async do
@@ -621,7 +621,7 @@ module Discorb
621
621
  #
622
622
  # @param [Discorb::User] user The user to check.
623
623
  #
624
- # @return [Boolean] Whether the user was banned.
624
+ # @return [Async::Task<Boolean>] Whether the user was banned.
625
625
  #
626
626
  def banned?(user)
627
627
  Async do
@@ -638,7 +638,7 @@ module Discorb
638
638
  # @param [Integer] delete_message_days The number of days to delete messages.
639
639
  # @param [String] reason The reason for banning the member.
640
640
  #
641
- # @return [Discorb::Guild::Ban] The ban.
641
+ # @return [Async::Task<Discorb::Guild::Ban>] The ban.
642
642
  #
643
643
  def ban_member(member, delete_message_days: 0, reason: nil)
644
644
  Async do
@@ -668,7 +668,7 @@ module Discorb
668
668
  # @macro async
669
669
  # @macro http
670
670
  #
671
- # @return [Array<Discorb::Role>] The list of roles.
671
+ # @return [Async::Task<Array<Discorb::Role>>] The list of roles.
672
672
  #
673
673
  def fetch_roles
674
674
  Async do
@@ -688,7 +688,7 @@ module Discorb
688
688
  # @param [Boolean] mentionable Whether the role should be mentionable.
689
689
  # @param [String] reason The reason for creating the role.
690
690
  #
691
- # @return [Discorb::Role] The role.
691
+ # @return [Async::Task<Discorb::Role>] The role.
692
692
  #
693
693
  def create_role(name = nil, color: nil, hoist: nil, mentionable: nil, reason: nil)
694
694
  Async do
@@ -712,7 +712,7 @@ module Discorb
712
712
  # @param [Integer] days The number of days to prune.
713
713
  # @param [Array<Discorb::Role>] roles The roles that include for pruning.
714
714
  #
715
- # @return [Integer] The number of members that will be pruned.
715
+ # @return [Async::Task<Integer>] The number of members that will be pruned.
716
716
  #
717
717
  def fetch_prune(days = 7, roles: [])
718
718
  Async do
@@ -735,7 +735,7 @@ module Discorb
735
735
  # @param [Array<Discorb::Role>] roles The roles that include for pruning.
736
736
  # @param [String] reason The reason for pruning.
737
737
  #
738
- # @return [Integer] The number of members that were pruned.
738
+ # @return [Async::Task<Integer>] The number of members that were pruned.
739
739
  #
740
740
  def prune(days = 7, roles: [], reason: nil)
741
741
  Async do
@@ -751,7 +751,7 @@ module Discorb
751
751
  # @macro async
752
752
  # @macro http
753
753
  #
754
- # @return [Array<Discorb::VoiceRegion>] The available voice regions.
754
+ # @return [Async::Task<Array<Discorb::VoiceRegion>>] The available voice regions.
755
755
  #
756
756
  def fetch_voice_regions
757
757
  Async do
@@ -765,7 +765,7 @@ module Discorb
765
765
  # @macro async
766
766
  # @macro http
767
767
  #
768
- # @return [Array<Invite>] The invites.
768
+ # @return [Async::Task<Array<Invite>>] The invites.
769
769
  #
770
770
  def fetch_invites
771
771
  Async do
@@ -779,7 +779,7 @@ module Discorb
779
779
  # @macro async
780
780
  # @macro http
781
781
  #
782
- # @return [Array<Discorb::Integration>] The integrations.
782
+ # @return [Async::Task<Array<Discorb::Integration>>] The integrations.
783
783
  #
784
784
  def fetch_integrations
785
785
  Async do
@@ -793,7 +793,7 @@ module Discorb
793
793
  # @macro async
794
794
  # @macro http
795
795
  #
796
- # @return [Discorb::Guild::Widget] The widget.
796
+ # @return [Async::Task<Discorb::Guild::Widget>] The widget.
797
797
  #
798
798
  def fetch_widget
799
799
  Async do
@@ -807,7 +807,7 @@ module Discorb
807
807
  # @macro async
808
808
  # @macro http
809
809
  #
810
- # @return [Discorb::Guild::VanityInvite] The vanity URL.
810
+ # @return [Async::Task<Discorb::Guild::VanityInvite>] The vanity URL.
811
811
  #
812
812
  def fetch_vanity_invite
813
813
  Async do
@@ -821,7 +821,7 @@ module Discorb
821
821
  # @macro async
822
822
  # @macro http
823
823
  #
824
- # @return [Discorb::Guild::WelcomeScreen] The welcome screen.
824
+ # @return [Async::Task<Discorb::Guild::WelcomeScreen>] The welcome screen.
825
825
  #
826
826
  def fetch_welcome_screen
827
827
  Async do
@@ -835,7 +835,7 @@ module Discorb
835
835
  # @macro async
836
836
  # @macro http
837
837
  #
838
- # @return [Array<Discorb::Sticker::GuildSticker>] The stickers.
838
+ # @return [Async::Task<Array<Discorb::Sticker::GuildSticker>>] The stickers.
839
839
  #
840
840
  def fetch_stickers
841
841
  Async do
@@ -851,8 +851,8 @@ module Discorb
851
851
  #
852
852
  # @param [#to_s] id The ID of the sticker.
853
853
  #
854
- # @return [Discorb::Sticker::GuildSticker] The sticker.
855
- # @return [nil] If the sticker does not exist.
854
+ # @return [Async::Task<Discorb::Sticker::GuildSticker>] The sticker.
855
+ # @return [Async::Task<nil>] If the sticker does not exist.
856
856
  #
857
857
  def fetch_sticker(id)
858
858
  Async do
@@ -869,7 +869,7 @@ module Discorb
869
869
  # @macro async
870
870
  # @macro http
871
871
  #
872
- # @return [Discorb::GuildTemplate] The templates.
872
+ # @return [Async::Task<Discorb::GuildTemplate>] The templates.
873
873
  #
874
874
  def fetch_templates
875
875
  Async do
@@ -882,7 +882,7 @@ module Discorb
882
882
  # Almost the same as {#fetch_templates}, but returns a single template.
883
883
  #
884
884
  # @return [Discorb::GuildTemplate] The template.
885
- # @return [nil] If the template does not exist.
885
+ # @return [Async::Task<nil>] If the template does not exist.
886
886
  #
887
887
  def fetch_template
888
888
  Async do
@@ -897,7 +897,7 @@ module Discorb
897
897
  # @param [String] description The description of the template.
898
898
  # @param [String] reason The reason for creating the template.
899
899
  #
900
- # @return [Discorb::GuildTemplate] The template.
900
+ # @return [Async::Task<Discorb::GuildTemplate>] The template.
901
901
  #
902
902
  def create_template(name, description = nil, reason: nil)
903
903
  Async do