discorb 0.15.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/build_main.yml +2 -2
  4. data/.github/workflows/build_version.yml +1 -1
  5. data/.github/workflows/codeql-analysis.yml +1 -1
  6. data/.github/workflows/lint-push.yml +3 -5
  7. data/.github/workflows/lint.yml +1 -1
  8. data/.github/workflows/spec.yml +30 -0
  9. data/.lefthook/commit-msg/validator.rb +5 -0
  10. data/.rspec +2 -0
  11. data/.rspec_parallel +2 -0
  12. data/.rubocop.yml +49 -8
  13. data/Changelog.md +32 -1
  14. data/Gemfile +14 -8
  15. data/Rakefile +46 -25
  16. data/bin/console +3 -3
  17. data/docs/Examples.md +1 -1
  18. data/docs/application_command.md +138 -46
  19. data/docs/cli/irb.md +2 -2
  20. data/docs/cli/new.md +14 -9
  21. data/docs/cli/run.md +7 -11
  22. data/docs/cli.md +17 -10
  23. data/docs/events.md +257 -193
  24. data/docs/extension.md +1 -2
  25. data/docs/faq.md +0 -1
  26. data/docs/tutorial.md +12 -12
  27. data/docs/voice_events.md +106 -106
  28. data/examples/commands/message.rb +63 -0
  29. data/examples/commands/permission.rb +18 -0
  30. data/examples/commands/slash.rb +44 -0
  31. data/examples/commands/user.rb +51 -0
  32. data/examples/components/authorization_button.rb +2 -2
  33. data/examples/components/select_menu.rb +2 -2
  34. data/examples/extension/main.rb +1 -1
  35. data/examples/extension/message_expander.rb +5 -2
  36. data/examples/simple/eval.rb +2 -2
  37. data/examples/simple/ping_pong.rb +1 -1
  38. data/examples/simple/rolepanel.rb +2 -2
  39. data/examples/simple/shard.rb +17 -0
  40. data/examples/simple/wait_for_message.rb +1 -1
  41. data/exe/discorb +31 -16
  42. data/lefthook.yml +45 -0
  43. data/lib/discorb/allowed_mentions.rb +8 -0
  44. data/lib/discorb/app_command/command.rb +184 -60
  45. data/lib/discorb/app_command/common.rb +25 -0
  46. data/lib/discorb/app_command/handler.rb +116 -34
  47. data/lib/discorb/app_command.rb +2 -1
  48. data/lib/discorb/application.rb +17 -7
  49. data/lib/discorb/asset.rb +10 -2
  50. data/lib/discorb/attachment.rb +17 -2
  51. data/lib/discorb/audit_logs.rb +53 -12
  52. data/lib/discorb/channel/base.rb +108 -0
  53. data/lib/discorb/channel/category.rb +32 -0
  54. data/lib/discorb/channel/container.rb +44 -0
  55. data/lib/discorb/channel/dm.rb +28 -0
  56. data/lib/discorb/channel/guild.rb +245 -0
  57. data/lib/discorb/channel/stage.rb +140 -0
  58. data/lib/discorb/channel/text.rb +345 -0
  59. data/lib/discorb/channel/thread.rb +321 -0
  60. data/lib/discorb/channel/voice.rb +79 -0
  61. data/lib/discorb/channel.rb +2 -1126
  62. data/lib/discorb/client.rb +160 -64
  63. data/lib/discorb/common.rb +18 -3
  64. data/lib/discorb/components/button.rb +7 -7
  65. data/lib/discorb/components/select_menu.rb +6 -18
  66. data/lib/discorb/components/text_input.rb +12 -2
  67. data/lib/discorb/components.rb +1 -1
  68. data/lib/discorb/dictionary.rb +2 -0
  69. data/lib/discorb/embed.rb +55 -14
  70. data/lib/discorb/emoji.rb +59 -5
  71. data/lib/discorb/emoji_table.rb +4970 -4
  72. data/lib/discorb/error.rb +7 -1
  73. data/lib/discorb/event.rb +56 -21
  74. data/lib/discorb/exe/about.rb +1 -0
  75. data/lib/discorb/exe/irb.rb +2 -4
  76. data/lib/discorb/exe/new.rb +95 -28
  77. data/lib/discorb/exe/run.rb +9 -37
  78. data/lib/discorb/exe/setup.rb +25 -12
  79. data/lib/discorb/exe/show.rb +4 -3
  80. data/lib/discorb/extend.rb +1 -0
  81. data/lib/discorb/extension.rb +6 -3
  82. data/lib/discorb/flag.rb +11 -0
  83. data/lib/discorb/gateway.rb +312 -169
  84. data/lib/discorb/gateway_requests.rb +4 -7
  85. data/lib/discorb/guild.rb +255 -89
  86. data/lib/discorb/guild_template.rb +34 -7
  87. data/lib/discorb/http.rb +23 -11
  88. data/lib/discorb/integration.rb +27 -9
  89. data/lib/discorb/intents.rb +8 -8
  90. data/lib/discorb/interaction/autocomplete.rb +31 -19
  91. data/lib/discorb/interaction/command.rb +70 -17
  92. data/lib/discorb/interaction/components.rb +20 -4
  93. data/lib/discorb/interaction/modal.rb +0 -1
  94. data/lib/discorb/interaction/response.rb +73 -22
  95. data/lib/discorb/interaction/root.rb +29 -14
  96. data/lib/discorb/interaction.rb +1 -0
  97. data/lib/discorb/invite.rb +16 -9
  98. data/lib/discorb/member.rb +46 -5
  99. data/lib/discorb/message.rb +56 -15
  100. data/lib/discorb/message_meta.rb +39 -9
  101. data/lib/discorb/modules.rb +56 -14
  102. data/lib/discorb/permission.rb +14 -5
  103. data/lib/discorb/presence.rb +43 -10
  104. data/lib/discorb/rate_limit.rb +13 -3
  105. data/lib/discorb/reaction.rb +10 -4
  106. data/lib/discorb/role.rb +31 -4
  107. data/lib/discorb/shard.rb +74 -0
  108. data/lib/discorb/sticker.rb +30 -21
  109. data/lib/discorb/user.rb +13 -1
  110. data/lib/discorb/utils/colored_puts.rb +1 -0
  111. data/lib/discorb/voice_state.rb +30 -8
  112. data/lib/discorb/webhook.rb +88 -25
  113. data/lib/discorb.rb +10 -6
  114. data/po/yard.pot +9 -9
  115. data/sig/discorb.rbs +7232 -5837
  116. metadata +23 -6
  117. data/examples/commands/bookmarker.rb +0 -42
  118. data/examples/commands/hello.rb +0 -10
  119. data/examples/commands/inspect.rb +0 -25
  120. data/lib/discorb/log.rb +0 -81
data/lib/discorb/error.rb CHANGED
@@ -42,7 +42,10 @@ module Discorb
42
42
  # @return [Net::HTTPResponse] the HTTP response.
43
43
  attr_reader :response
44
44
 
45
+ #
46
+ # Initialize a new instance of the HTTPError class.
45
47
  # @private
48
+ #
46
49
  def initialize(resp, data)
47
50
  @code = data[:code]
48
51
  @response = resp
@@ -54,7 +57,10 @@ module Discorb
54
57
  # Represents a 400 error.
55
58
  #
56
59
  class BadRequestError < HTTPError
60
+ #
61
+ # Initialize a new instance of the BadRequestError class.
57
62
  # @private
63
+ #
58
64
  def initialize(resp, data)
59
65
  @code = data[:code]
60
66
  @response = resp
@@ -62,7 +68,7 @@ module Discorb
62
68
  [
63
69
  data[:message] + " (#{@code})", enumerate_errors(data[:errors])
64
70
  .map { |ek, ev| "#{ek}=>#{ev}" }
65
- .join("\n")
71
+ .join("\n"),
66
72
  ].join("\n")
67
73
  )
68
74
  end
data/lib/discorb/event.rb CHANGED
@@ -1,23 +1,30 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Discorb
3
4
  #
4
5
  # Represents an event in guild.
5
6
  #
6
7
  class ScheduledEvent < DiscordModel
7
- @privacy_level = {
8
+ # @private
9
+ # @return [{Integer => Symbol}] The mapping of privacy level.
10
+ PRIVACY_LEVEL = {
8
11
  2 => :guild_only,
9
- }
10
- @status = {
12
+ }.freeze
13
+ # @private
14
+ # @return [{Integer => Symbol}] The mapping of status.
15
+ STATUS = {
11
16
  1 => :scheduled,
12
17
  2 => :active,
13
18
  3 => :completed,
14
19
  4 => :canceled,
15
- }
16
- @entity_type = {
20
+ }.freeze
21
+ # @private
22
+ # @return [{Integer => Symbol}] The mapping of entity_type.
23
+ ENTITY_TYPE = {
17
24
  1 => :stage_instance,
18
25
  2 => :voice,
19
26
  3 => :external,
20
- }
27
+ }.freeze
21
28
 
22
29
  # @!visibility private
23
30
  def initialize(client, data)
@@ -32,10 +39,15 @@ module Discorb
32
39
  class Metadata
33
40
  # @return [String, nil] The location of the event. Only present if the event is a external event.
34
41
  attr_reader :location
42
+
35
43
  # @!visibility private
36
44
  def initialize(data)
37
45
  @location = data[:location]
38
46
  end
47
+
48
+ def inspect
49
+ "#<#{self.class.name} #{@name}>"
50
+ end
39
51
  end
40
52
 
41
53
  # @return [Discorb::Snowflake] The ID of the event.
@@ -127,48 +139,57 @@ module Discorb
127
139
  payload = case type == Discorb::Unset ? @entity_type : type
128
140
  when :stage_instance
129
141
  raise ArgumentError, "channel must be provided for stage_instance events" unless channel
142
+
130
143
  {
131
144
  name: name,
132
145
  description: description,
133
146
  scheduled_start_time: start_time.iso8601,
134
147
  scheduled_end_time: end_time&.iso8601,
135
- privacy_level: Discorb::ScheduledEvent.privacy_level.key(privacy_level) || Discorb::Unset,
148
+ privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
136
149
  channel_id: channel&.id,
137
- entity_type: Discorb::ScheduledEvent.entity_type.key(:stage_instance),
138
- status: Discorb::ScheduledEvent.status.key(status) || Discorb::Unset,
150
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:stage_instance),
151
+ status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
139
152
  }.reject { |_, v| v == Discorb::Unset }
140
153
  when :voice
141
154
  raise ArgumentError, "channel must be provided for voice events" unless channel
155
+
142
156
  {
143
157
  name: name,
144
158
  description: description,
145
159
  scheduled_start_time: start_time.iso8601,
146
160
  scheduled_end_time: end_time&.iso8601,
147
- privacy_level: Discorb::ScheduledEvent.privacy_level.key(privacy_level) || Discorb::Unset,
161
+ privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
148
162
  channel_id: channel&.id,
149
- entity_type: Discorb::ScheduledEvent.entity_type.key(:voice),
150
- status: Discorb::ScheduledEvent.status.key(status) || Discorb::Unset,
163
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:voice),
164
+ status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
151
165
  }.reject { |_, v| v == Discorb::Unset }
152
166
  when :external
153
167
  raise ArgumentError, "location must be provided for external events" unless location
154
168
  raise ArgumentError, "end_time must be provided for external events" unless end_time
169
+
155
170
  {
156
171
  name: name,
157
172
  description: description,
158
173
  channel_id: nil,
159
174
  scheduled_start_time: start_time.iso8601,
160
175
  scheduled_end_time: end_time.iso8601,
161
- privacy_level: Discorb::ScheduledEvent.privacy_level.key(privacy_level) || Discorb::Unset,
162
- entity_type: Discorb::ScheduledEvent.entity_type.key(:external),
176
+ privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
177
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:external),
163
178
  entity_metadata: {
164
179
  location: location,
165
180
  },
166
- status: Discorb::ScheduledEvent.status.key(status) || Discorb::Unset,
181
+ status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
167
182
  }.reject { |_, v| v == Discorb::Unset }
168
183
  else
169
184
  raise ArgumentError, "Invalid scheduled event type: #{type}"
170
185
  end
171
- @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}", "//guilds/:guild_id/scheduled-events/:scheduled_event_id", :patch), payload).wait
186
+ @client.http.request(
187
+ Route.new(
188
+ "/guilds/#{@guild_id}/scheduled-events/#{@id}",
189
+ "//guilds/:guild_id/scheduled-events/:scheduled_event_id",
190
+ :patch
191
+ ), payload
192
+ ).wait
172
193
  end
173
194
  end
174
195
 
@@ -205,7 +226,8 @@ module Discorb
205
226
  #
206
227
  def delete!
207
228
  Async do
208
- @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}", "//guilds/:guild_id/scheduled-events/:scheduled_event_id", :delete)).wait
229
+ @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}",
230
+ "//guilds/:guild_id/scheduled-events/:scheduled_event_id", :delete)).wait
209
231
  end
210
232
  end
211
233
 
@@ -231,8 +253,15 @@ module Discorb
231
253
  after = 0
232
254
  res = []
233
255
  loop do
234
- _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
256
+ _resp, users = @client.http.request(
257
+ Route.new(
258
+ "/guilds/#{@guild_id}/scheduled-events/#{@id}/users?limit=100&after=#{after}&with_member=true",
259
+ "//guilds/:guild_id/scheduled-events/:scheduled_event_id/users",
260
+ :get
261
+ )
262
+ ).wait
235
263
  break if users.empty?
264
+
236
265
  res += users.map { |u| Member.new(@client, @guild_id, u[:user], u[:member]) }
237
266
  after = users.last[:user][:id]
238
267
  end
@@ -244,7 +273,13 @@ module Discorb
244
273
  after: Discorb::Utils.try(after, :id),
245
274
  with_member: with_member,
246
275
  }.filter { |_k, v| !v.nil? }.to_h
247
- _resp, messages = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}", "//channels/:channel_id/messages", :get)).wait
276
+ _resp, messages = @client.http.request(
277
+ Route.new(
278
+ "/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}",
279
+ "//channels/:channel_id/messages",
280
+ :get
281
+ )
282
+ ).wait
248
283
  messages.map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) }
249
284
  end
250
285
  end
@@ -264,8 +299,8 @@ module Discorb
264
299
  @scheduled_start_time = Time.iso8601(data[:scheduled_start_time])
265
300
  @scheduled_end_time = data[:scheduled_end_time] && Time.iso8601(data[:scheduled_end_time])
266
301
  @privacy_level = :guild_only # data[:privacy_level]
267
- @status = self.class.status[data[:status]]
268
- @entity_type = self.class.entity_type[data[:entity_type]]
302
+ @status = STATUS[data[:status]]
303
+ @entity_type = ENTITY_TYPE[data[:entity_type]]
269
304
  @entity_id = data[:entity_id] && Snowflake.new(data[:entity_id])
270
305
  @entity_metadata = data[:entity_metadata] && Metadata.new(data[:entity_metadata])
271
306
  @creator = @client.users[@creator_id] || (data[:creator] && User.new(@client, data[:creator]))
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # description: Show information of discorb.
3
4
 
4
5
  puts " disco\e[31mrb\e[m - A new Discord API wrapper in Ruby. \n\n"
@@ -10,8 +10,6 @@ require "optparse"
10
10
  intents_value = Discorb::Intents.all.value
11
11
  token_file = "token"
12
12
 
13
- ARGV.delete_at 0
14
-
15
13
  opt = OptionParser.new <<~BANNER
16
14
  This command will start an interactive Ruby shell with connected client.
17
15
 
@@ -43,7 +41,7 @@ client.on :standby do
43
41
 
44
42
  puts <<~FIRST_MESSAGE
45
43
  Running on \e[31mRuby #{RUBY_VERSION}\e[m, disco\e[31mrb #{Discorb::VERSION}\e[m
46
- Type \e[90mdirb_help\e[m to help.
44
+ Type \e[90mdirb_help\e[m to help.
47
45
  FIRST_MESSAGE
48
46
 
49
47
  binding.irb
@@ -55,7 +53,7 @@ client.on :message do |message|
55
53
  $messages << message
56
54
  end
57
55
 
58
- token = ENV["DISCORD_BOT_TOKEN"] || ENV["DISCORD_TOKEN"]
56
+ token = ENV.fetch("DISCORD_BOT_TOKEN", nil) || ENV.fetch("DISCORD_TOKEN", nil)
59
57
  if token.nil?
60
58
  if File.exist?(token_file)
61
59
  token = File.read(token_file)
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # description: Make files for the discorb project.
3
4
 
4
5
  require "optparse"
5
6
  require "discorb"
7
+ require "pathname"
6
8
  require_relative "../utils/colored_puts"
7
9
 
8
10
  $path = Dir.pwd
@@ -10,10 +12,10 @@ $path = Dir.pwd
10
12
  # @private
11
13
  FILES = {
12
14
  "main.rb" => <<~'RUBY',
13
- require "discorb"
14
- require "dotenv"
15
+ # frozen_string_literal: true
15
16
 
16
- Dotenv.load # Loads .env file
17
+ require "discorb"
18
+ require "dotenv/load" # Load environment variables from .env file.
17
19
 
18
20
  client = Discorb::Client.new # Create client for connecting to Discord
19
21
 
@@ -23,8 +25,26 @@ FILES = {
23
25
 
24
26
  client.run ENV["%<token>s"] # Starts client
25
27
  RUBY
28
+ "main.rb_nc" => <<~'RUBY',
29
+ # frozen_string_literal: true
30
+
31
+ require "discorb"
32
+ require "dotenv/load"
33
+
34
+ client = Discorb::Client.new
35
+
36
+ client.once :standby do
37
+ puts "Logged in as #{client.user}"
38
+ end
39
+
40
+ client.run ENV["%<token>s"]
41
+ RUBY
26
42
  ".env" => <<~BASH,
27
- %<token>s=Y0urB0tT0k3nHer3.Th1sT0ken.W0ntWorkB3c4useItH4sM34n1ng
43
+ # Put your token after `%<token>s=`
44
+ %<token>s=
45
+ BASH
46
+ ".env_nc" => <<~BASH,
47
+ %<token>s=
28
48
  BASH
29
49
  ".gitignore" => <<~GITIGNORE,
30
50
  *.gem
@@ -38,13 +58,13 @@ FILES = {
38
58
  /test/tmp/
39
59
  /test/version_tmp/
40
60
  /tmp/
41
-
61
+
42
62
  # Used by dotenv library to load environment variables.
43
63
  .env
44
-
64
+
45
65
  # Ignore Byebug command history file.
46
66
  .byebug_history
47
-
67
+
48
68
  ## Specific to RubyMotion:
49
69
  .dat*
50
70
  .repl_history
@@ -52,7 +72,7 @@ FILES = {
52
72
  *.bridgesupport
53
73
  build-iPhoneOS/
54
74
  build-iPhoneSimulator/
55
-
75
+
56
76
  ## Specific to RubyMotion (use of CocoaPods):
57
77
  #
58
78
  # We recommend against adding the Pods directory to your .gitignore. However
@@ -60,33 +80,68 @@ FILES = {
60
80
  # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
61
81
  #
62
82
  # vendor/Pods/
63
-
83
+
64
84
  ## Documentation cache and generated files:
65
85
  /.yardoc/
66
86
  /_yardoc/
67
87
  /doc/
68
88
  /rdoc/
69
-
89
+
70
90
  ## Environment normalization:
71
91
  /.bundle/
72
92
  /vendor/bundle
73
93
  /lib/bundler/man/
74
-
94
+
75
95
  # for a library or gem, you might want to ignore these files since the code is
76
96
  # intended to run in multiple environments; otherwise, check them in:
77
97
  # Gemfile.lock
78
98
  # .ruby-version
79
99
  # .ruby-gemset
80
-
100
+
81
101
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
82
102
  .rvmrc
83
-
103
+
84
104
  # Used by RuboCop. Remote config files pulled in from inherit_from directive.
85
105
  # .rubocop-https?--*
86
106
 
87
107
  # This gitignore is from github/gitignore.
88
108
  # https://github.com/github/gitignore/blob/master/Ruby.gitignore
89
109
  GITIGNORE
110
+ ".gitignore_nc" => <<~GITIGNORE,
111
+ *.gem
112
+ *.rbc
113
+ /.config
114
+ /coverage/
115
+ /InstalledFiles
116
+ /pkg/
117
+ /spec/reports/
118
+ /spec/examples.txt
119
+ /test/tmp/
120
+ /test/version_tmp/
121
+ /tmp/
122
+
123
+ .env
124
+
125
+ .byebug_history
126
+
127
+ .dat*
128
+ .repl_history
129
+ build/
130
+ *.bridgesupport
131
+ build-iPhoneOS/
132
+ build-iPhoneSimulator/
133
+
134
+ /.yardoc/
135
+ /_yardoc/
136
+ /doc/
137
+ /rdoc/
138
+
139
+ /.bundle/
140
+ /vendor/bundle
141
+ /lib/bundler/man/
142
+
143
+ .rvmrc
144
+ GITIGNORE
90
145
  "Gemfile" => <<~RUBY,
91
146
  # frozen_string_literal: true
92
147
 
@@ -96,6 +151,8 @@ FILES = {
96
151
 
97
152
  gem "discorb", "~> #{Discorb::VERSION}"
98
153
  gem "dotenv", "~> 2.7"
154
+
155
+ ruby "~> #{RUBY_VERSION.split(".")[0]}.#{RUBY_VERSION.split(".")[1]}"
99
156
  RUBY
100
157
  ".env.sample" => <<~BASH,
101
158
  %<token>s=
@@ -129,14 +186,16 @@ FILES = {
129
186
 
130
187
  TODO: Write your bot's license here.
131
188
  See https://choosealicense.com/ for more information.
132
- https://rubygems.org/gems/license-cli may be useful.
133
189
 
134
190
  MARKDOWN
135
191
  }.freeze
136
192
 
137
193
  # @private
138
194
  def create_file(name)
139
- File.write($path + "/#{name}", format(FILES[name], token: $values[:token], name: $values[:name]), mode: "wb")
195
+ template_name = name
196
+ template_name += "_nc" if !$values[:comment] && FILES.key?(name + "_nc")
197
+ content = format(FILES[template_name], token: $values[:token], name: $values[:name])
198
+ File.write($path + "/#{name}", content, mode: "wb")
140
199
  end
141
200
 
142
201
  # @private
@@ -152,7 +211,7 @@ def bundle_init
152
211
  iputs "Initializing bundle..."
153
212
  create_file("Gemfile")
154
213
  iputs "Installing gems..."
155
- system "bundle install"
214
+ system({ "BUNDLE_GEMFILE" => nil }, "bundle install", chdir: $path)
156
215
  sputs "Installed gems.\n"
157
216
  end
158
217
 
@@ -160,12 +219,12 @@ end
160
219
  def git_init
161
220
  create_file(".gitignore")
162
221
  iputs "Initializing git repository..."
163
- system "git init"
164
- system "git add ."
165
- system "git commit -m \"Initial commit\""
222
+ system "git init", chdir: $path
223
+ system "git add .", chdir: $path
224
+ system "git commit -m \"Initial commit\"", chdir: $path
166
225
  sputs "Initialized repository, use " \
167
- "\e[32mgit commit --amend -m '...'\e[92m" \
168
- " to change commit message of initial commit.\n"
226
+ "\e[32mgit commit --amend -m '...'\e[92m" \
227
+ " to change commit message of initial commit.\n"
169
228
  end
170
229
 
171
230
  # @private
@@ -191,6 +250,7 @@ $values = {
191
250
  token: "TOKEN",
192
251
  descs: false,
193
252
  name: nil,
253
+ comment: true,
194
254
  }
195
255
 
196
256
  opt.on("--[no-]bundle", "Whether to use bundle. Default to true.") do |v|
@@ -205,6 +265,10 @@ opt.on("--[no-]descs", "Whether to put some file for description. Default to fal
205
265
  $values[:descs] = v
206
266
  end
207
267
 
268
+ opt.on("--[no-]comment", "Whether to write comment. Default to true.") do |v|
269
+ $values[:comment] = v
270
+ end
271
+
208
272
  opt.on("-t NAME", "--token NAME", "The name of token environment variable. Default to TOKEN.") do |v|
209
273
  $values[:token] = v
210
274
  end
@@ -213,30 +277,33 @@ opt.on("-n NAME", "--name NAME", "The name of your project. Default to the direc
213
277
  $values[:name] = v
214
278
  end
215
279
 
216
- opt.on("-f", "--force", "Whether to force use directory. Default to false.") do |v|
280
+ opt.on("--force", "-f", "Whether to force use directory. Default to false.") do |v|
217
281
  $values[:force] = v
218
282
  end
219
283
 
220
- ARGV.delete_at(0)
221
-
222
284
  opt.parse!(ARGV)
223
285
 
224
286
  if (dir = ARGV[0])
225
287
  $path += "/#{dir}"
288
+ $path = File.expand_path($path)
289
+ dir = File.basename($path)
226
290
  if Dir.exist?($path)
227
291
  if Dir.empty?($path)
228
292
  iputs "Found \e[30m#{dir}\e[90m and empty, using this directory."
229
293
  elsif $values[:force]
230
294
  iputs "Found \e[30m#{dir}\e[90m and not empty, but force is on, using this directory."
231
- else
232
- eputs "Directory \e[31m#{dir}\e[91m already exists and not empty. Use \e[31m-f\e[91m to force."
233
- exit
295
+ else
296
+ eputs "Directory \e[31m#{dir}\e[91m already exists and not empty. Use \e[31m-f\e[91m to force."
297
+ exit
234
298
  end
235
299
  else
236
300
  Dir.mkdir($path)
237
301
  iputs "Couldn't find \e[30m#{dir}\e[90m, created directory."
238
302
  end
239
303
  Dir.chdir($path)
304
+ else
305
+ puts opt
306
+ abort
240
307
  end
241
308
 
242
309
  $values[:name] ||= Dir.pwd.split("/").last
@@ -249,4 +316,4 @@ make_descs if $values[:descs]
249
316
 
250
317
  git_init if $values[:git]
251
318
 
252
- sputs "\nSuccessfully made a new project at \e[32m#{$path}\e[92m."
319
+ sputs "\nSuccessfully made a new project at \e[32m#{Pathname.new($path).cleanpath.split[-1]}\e[92m."
@@ -1,13 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # description: Run a client.
3
4
  require "optparse"
4
5
  require "json"
5
6
  require "discorb/utils/colored_puts"
6
7
  require "io/console"
7
-
8
- ARGV.delete_at 0
9
- # @private
10
- LOG_LEVELS = %w[none debug info warn error fatal].freeze
8
+ require "discorb"
11
9
 
12
10
  opt = OptionParser.new <<~BANNER
13
11
  This command will run a client.
@@ -18,27 +16,15 @@ opt = OptionParser.new <<~BANNER
18
16
  BANNER
19
17
  options = {
20
18
  title: nil,
21
- log_level: nil,
22
- log_file: nil,
23
- log_color: nil,
24
19
  setup: nil,
25
20
  token: false,
26
- bundler: :default,
27
21
  }
28
- opt.on("-l", "--log-level LEVEL", "Log level.") do |v|
29
- unless LOG_LEVELS.include? v.downcase
30
- eputs "Invalid log level: \e[31m#{v}\e[91m"
31
- eputs "Valid log levels: \e[31m#{LOG_LEVELS.join("\e[91m, \e[31m")}\e[91m"
32
- exit 1
33
- end
34
- options[:log_level] = v.downcase
35
- end
36
- opt.on("-f", "--log-file FILE", "File to write log to.") { |v| options[:log_file] = v }
37
- opt.on("-c", "--[no-]log-color", "Whether to colorize log output.") { |v| options[:log_color] = v }
38
22
  opt.on("-s", "--setup", "Whether to setup application commands.") { |v| options[:setup] = v }
39
- opt.on("-e", "--env [ENV]", "The name of the environment variable to use for token, or just `-e` or `--env` for intractive prompt.") { |v| options[:token] = v }
23
+ opt.on("-e", "--env [ENV]",
24
+ "The name of the environment variable to use for token, or just `-e` or `--env` for intractive prompt.") do |v|
25
+ options[:token] = v
26
+ end
40
27
  opt.on("-t", "--title TITLE", "The title of process.") { |v| options[:title] = v }
41
- opt.on("-b", "--[no-]bundler", "Whether to use bundler. Default to true if Gemfile exists, otherwise false.") { |v| options[:bundler] = v }
42
28
  opt.parse!(ARGV)
43
29
 
44
30
  script = ARGV[0]
@@ -52,6 +38,7 @@ if script.nil?
52
38
  break
53
39
  end
54
40
  break if dir == File.dirname(dir)
41
+
55
42
  dir = File.dirname(dir)
56
43
  end
57
44
  if File.dirname(script) != Dir.pwd
@@ -64,7 +51,7 @@ ENV["DISCORB_CLI_FLAG"] = "run"
64
51
  ENV["DISCORB_CLI_OPTIONS"] = JSON.generate(options)
65
52
 
66
53
  if options[:token]
67
- ENV["DISCORB_CLI_TOKEN"] = ENV[options[:token]]
54
+ ENV["DISCORB_CLI_TOKEN"] = ENV.fetch(options[:token], nil)
68
55
  raise "#{options[:token]} is not set." if ENV["DISCORB_CLI_TOKEN"].nil?
69
56
  elsif options[:token].nil? || options[:token] == "-"
70
57
  print "\e[90mPlease enter your token: \e[m"
@@ -72,25 +59,10 @@ elsif options[:token].nil? || options[:token] == "-"
72
59
  puts ""
73
60
  end
74
61
 
75
- if options[:bundler] == :default
76
- dir = Dir.pwd.split("/")
77
- options[:bundler] = false
78
- dir.length.times.reverse_each do |i|
79
- if File.exist? "#{dir[0..i].join("/")}/Gemfile"
80
- options[:bundler] = true
81
- break
82
- end
83
- end
84
- end
85
-
86
62
  ENV["DISCORB_CLI_TITLE"] = options[:title]
87
63
 
88
64
  if File.exist? script
89
- if options[:bundler]
90
- exec "bundle exec ruby #{script}"
91
- else
92
- exec "ruby #{script}"
93
- end
65
+ exec "ruby #{script}"
94
66
  else
95
67
  eputs "Could not load script: \e[31m#{script}\e[91m"
96
68
  end
@@ -1,10 +1,9 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # description: Setup application commands.
3
4
  require "optparse"
4
5
  require "discorb/utils/colored_puts"
5
6
 
6
- ARGV.delete_at 0
7
-
8
7
  options = {
9
8
  guilds: nil,
10
9
  script: true,
@@ -17,8 +16,21 @@ opt = OptionParser.new <<~BANNER
17
16
 
18
17
  script The script to setup.
19
18
  BANNER
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
+ opt.on("-g", "--guild ID", Array,
20
+ "The guild ID to setup, use comma for setup commands in multiple guilds, " \
21
+ "or `global` for setup global commands.") do |v|
22
+ options[:guilds] = v
23
+ end
24
+ opt.on("-c", "--clear-guild ID", Array,
25
+ "The guild ID to clear command, use comma for clear commands in multiple guilds, " \
26
+ "or `global` for clear global commands.") do |v|
27
+ options[:clear_guilds] = v
28
+ end
29
+ opt.on("-s", "--[no-]script",
30
+ "Whether to run `:setup` event. " \
31
+ "This may be useful if setup script includes operation that shouldn't run twice. Default to true.") do |v|
32
+ options[:script] = v
33
+ end
22
34
  opt.parse!(ARGV)
23
35
 
24
36
  script = ARGV[0]
@@ -26,17 +38,18 @@ script ||= "main.rb"
26
38
  ENV["DISCORB_CLI_FLAG"] = "setup"
27
39
 
28
40
  ENV["DISCORB_SETUP_GUILDS"] = if options[:guilds] == ["global"]
29
- "global"
30
- elsif options[:guilds]
31
- options[:guilds].join(",")
32
- end
41
+ "global"
42
+ elsif options[:guilds]
43
+ options[:guilds].join(",")
44
+ end
45
+
46
+ ENV["DISCORB_SETUP_CLEAR_GUILDS"] = options[:clear_guilds]&.join(",")
33
47
 
34
48
  ENV["DISCORB_SETUP_SCRIPT"] = options[:script].to_s if options[:script]
35
49
 
36
- begin
50
+ if File.exist? script
37
51
  load script
38
- rescue LoadError
39
- eputs "Could not load script: \e[31m#{script}\e[m"
40
- else
41
52
  sputs "Successfully set up commands for \e[32m#{script}\e[m."
53
+ else
54
+ eputs "Could not load script: \e[31m#{script}\e[m"
42
55
  end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # description: Show information of your environment.
3
4
 
4
5
  require "etc"
5
6
  require "discorb"
6
7
 
7
- puts "\e[90mRuby:\e[m #{RUBY_VERSION}"
8
- puts "\e[90mdiscorb:\e[m #{Discorb::VERSION}"
8
+ puts "\e[90m Ruby:\e[m #{RUBY_VERSION}"
9
+ puts "\e[90m discorb:\e[m #{Discorb::VERSION}"
9
10
  uname = Etc.uname
10
- puts "\e[90mSystem:\e[m #{uname[:sysname]} #{uname[:release]}"
11
+ puts "\e[90m System:\e[m #{uname[:sysname]} #{uname[:release]}"
11
12
  puts "\e[90mPlatform:\e[m #{RUBY_PLATFORM}"
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # rubocop: disable Style/Documentation
3
4
 
4
5
  class Time