commandrb 0.4.3 → 0.5.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.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/commandrb.rb +162 -213
  3. data/lib/helper.rb +46 -35
  4. metadata +12 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6822eef019c15386f6d8915e2f4652ed22ce977f
4
- data.tar.gz: d99a5734a08845743616e3bd3921e68ea83456e1
2
+ SHA256:
3
+ metadata.gz: a71ccd55615cb29fa0a400e3fd29b543d3ce8d52d264646be9e50d2b824790bd
4
+ data.tar.gz: cc5f60c22142653ac2b9b3a2504c083329ea382738c07c9bc6f9c6f4966e519c
5
5
  SHA512:
6
- metadata.gz: 66ad55bd73df84f41c3f9cd102ea10f0e4f8aa346400bc552ffcca999217585245502bade76c52f0d5f11fe5e60498780ff006d28d297dcc31814638a7257e32
7
- data.tar.gz: 63369fdb6ab5ed5b4e1d737701a8f13e14c4c9b70b1e87b9ada5b1e2642ee1721e4bdccb4f8b0fa39980431533e45e5e9e2fc6a82b595d62d476320cb99845c2
6
+ metadata.gz: 23cc6f6e1cf7f67c05d555794cd700c9a8ab8d0a5e329543e1c58e8219c61ef3e64a02b9de45b020cd22b7bed21639124ecf60f196e154c2f49398c3dc1ee364
7
+ data.tar.gz: 1fdd79b3754dc7e156ac365723efdb2589b56f4a04adda1309b04ce62e69cfa23641ae1dd48631cc461bf3495367e80e5a765df48443dd9d52b9f4f6c3f93ff0
data/lib/commandrb.rb CHANGED
@@ -1,9 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'helper'
2
4
 
3
5
  class CommandrbBot
4
-
5
- ENV['COMMANDRB_MODE'] == 'debug' ? @debug_mode = true : @debug_mode = false
6
-
7
6
  # Be able to adjust the config on the fly.
8
7
  attr_accessor :config
9
8
 
@@ -16,7 +15,6 @@ class CommandrbBot
16
15
  # Lets you change global prefixes while the bot is running (Not recommended!)
17
16
  attr_accessor :prefixes
18
17
 
19
-
20
18
  def add_command(name, attributes = {})
21
19
  @commands[name.to_sym] = attributes
22
20
  end
@@ -24,13 +22,22 @@ class CommandrbBot
24
22
  def remove_command(name)
25
23
  begin
26
24
  @commands.delete(name)
27
- rescue
25
+ rescue StandardError
28
26
  return false
29
27
  end
30
28
  true
31
29
  end
32
30
 
31
+ # By defining this separately, we allow you to overwrite it and use your own owner list.
32
+ # Your checks will instead be run by commandrb and allow you to use :owner_only as normal.
33
+ def owner?(id)
34
+ @config[:owners].include?(id)
35
+ end
36
+
37
+ alias is_owner? owner?
38
+
33
39
  def initialize(init_hash)
40
+ @debug_mode = ENV['COMMANDRB_MODE'] == 'debug'
34
41
 
35
42
  # Setup the variables for first use.
36
43
  @commands = {}
@@ -39,24 +46,20 @@ class CommandrbBot
39
46
 
40
47
  # Load sane defaults for options that aren't specified.
41
48
 
42
- # @config[:prefix_type] = 'rescue' if @config[:prefix_type].nil?
43
49
  @config[:typing_default] = false if @config[:typing_default].nil?
44
- @config[:selfbot] = false if @config[:selfbot].nil?
45
50
  @config[:delete_activators] = false if @config[:delete_activators].nil?
46
51
 
47
- if @config[:token].nil? or init_hash[:token] == ''
48
- raise 'No token supplied in init hash!'
49
- return false
50
- end
52
+ raise 'No token supplied in init hash!' if @config[:token].nil? || (init_hash[:token] == '')
51
53
 
52
- init_parse_self = init_hash[:parse_self] rescue nil
54
+ init_parse_self = begin
55
+ init_hash[:parse_self]
56
+ rescue StandardError
57
+ nil
58
+ end
53
59
  init_type = @config[:type]
54
60
 
55
- if init_type == :bot
56
- if init_hash[:client_id].nil?
57
- raise 'No client ID or invalid client ID supplied in init hash!'
58
- return false
59
- end
61
+ if init_type == :bot && init_hash[:client_id].nil?
62
+ raise 'No client ID or invalid client ID supplied in init hash!'
60
63
  end
61
64
 
62
65
  @config[:owners] = init_hash[:owners]
@@ -65,10 +68,10 @@ class CommandrbBot
65
68
  @prefixes = init_hash[:prefixes]
66
69
 
67
70
  @bot = Discordrb::Bot.new(
68
- token: @config[:token],
69
- client_id: @config[:client_id],
70
- parse_self: init_parse_self,
71
- type: @config[:type]
71
+ token: @config[:token],
72
+ client_id: @config[:client_id],
73
+ parse_self: init_parse_self,
74
+ type: @config[:type]
72
75
  )
73
76
 
74
77
  unless init_hash[:ready].nil?
@@ -78,201 +81,147 @@ class CommandrbBot
78
81
  end
79
82
  end
80
83
 
81
-
82
84
  # Command processing
83
85
  @bot.message do |event|
84
- @finished = false
85
- @command = nil
86
- @event = nil
87
- @chosen = nil
88
- @args = nil
89
- @rawargs = nil
90
- @continue = false
91
- @prefixes.each { |prefix|
92
- break if @finished
93
- if event.message.content.start_with?(prefix)
94
-
95
- @commands.each { | key, command |
96
- break if @finished
97
- puts ":: Considering #{key.to_s}" if @debug_mode
98
- triggers = command[:triggers].nil? ? [key.to_s] : command[:triggers]
99
-
100
- triggers.each { |trigger|
101
- @activator = prefix + trigger.to_s
102
- puts @activator if @debug_mode
103
- @activator = @activator.downcase
104
- if event.message.content.downcase.start_with?(@activator)
105
- puts "Prefix matched! #{@activator}" if @debug_mode
106
-
107
- # Continue only if you've already chosen a choice.
108
- unless @chosen.nil?
109
- # If the new activator begins with the chosen one, then override it.
110
- # Example: sh is chosen, shell is the new one.
111
- # In this example, shell would override sh, preventing ugly bugs.
112
- if @activator.start_with?(@chosen)
113
- puts "#{@activator} just overrode #{@chosen}" if @debug_mode
114
- @chosen = @activator
115
- # Otherwhise, just give up.
116
- else
117
- puts "Match failed..." if @debug_mode
118
- next
119
- end
120
- # If you haven't chosen yet, get choosing!
121
- else
122
- puts "First match obtained!" if @debug_mode
123
- @continue = true
124
- @chosen = @activator
125
- end
126
- end
127
- }
128
-
129
- puts "Result: #{@chosen}" if @debug_mode
130
-
131
- next if !@continue
132
- puts "Final esult: #{@chosen}" if @debug_mode
133
-
134
- break if @config[:selfbot] && event.user.id != @bot.profile.id
135
-
136
- # Command flag defaults
137
- command[:catch_errors] = @config[:catch_errors] if command[:catch_errors].nil?
138
- command[:owners_only] = false if command[:owners_only].nil?
139
- command[:max_args] = 2000 if command[:max_args].nil?
140
- command[:server_only] = false if command[:server_only].nil?
141
- command[:typing] = @config[:typing_default] if command[:typing_default].nil?
142
- command[:delete_activator] = @config[:delete_activators] if command[:delete_activator].nil?
143
-
144
- # If the command is set to owners only and the user is not the owner, show error and abort.
145
- puts "[DEBUG] Command being processed: '#{command}'" if @debug_mode
146
- puts "[DEBUG] Owners only? #{command[:owners_only]}" if @debug_mode
147
- if command[:owners_only]
148
- if !@config[:owners].include?(event.user.id)
149
- event.channel.send_message('', false,
150
- Helper.error_embed(
151
- error: "You don't have permission for that!\nOnly owners are allowed to access this command.",
152
- footer: "Command: `#{event.message.content}`",
153
- colour: 0xFA0E30,
154
- code_error: false
155
- )
156
- )
157
- puts 'Were returning!'
158
- @finished = true
159
- next
160
- end
161
- end
162
-
163
- # If the settings are to delete activating messages, then do that.
164
- # I'm *hoping* this doesn't cause issues with argument extraction.
165
- event.message.delete if command[:delete_activator]
166
-
167
- # If the command is only for use in servers, display error and abort.
168
- if command[:server_only] && event.channel.private?
169
- # For selfbots, a fancy embed will be used. WIP.
170
- if @config[:selfbot]
171
- event.channel.send_embed do |embed|
172
- embed.colour = 0x221461
173
- embed.title = '❌ An error has occured!'
174
- embed.description = 'This command can only be used in servers!'
175
- embed.footer = Discordrb::Webhooks::EmbedFooter.new(text: "Command: '#{event.message.content}'")
176
- end
177
- else
178
- # If its not a selfbot, an ordinary message will be shown, may be changed to embed later.
179
- event.respond('❌ This command will only work in servers!')
180
- end
181
- # Abort!
182
- @finished = true
183
- next
184
- end
185
-
186
- # If the user is a bot and the command is set to not pass bots OR the user is a bot and the global config is to not parse bots...
187
- # ...then abort :3
188
- if (event.user.bot_account? && command[:parse_bots] == false) || (event.user.bot_account? && @config[:parse_bots] == false)
189
- # Abort!
190
- @finished = true
191
- next
192
- end
193
-
194
- # If the config is setup to show typing messages, then do so.
195
- event.channel.start_typing if command[:typing]
196
-
197
- args = event.message.content.split(' ')
198
- # Parse args if args exist !
199
- begin
200
- spaces = 1
201
- # Prefixes with spaces are special and need to be parsed differently : )
202
- if prefix.include? " "
203
- spaces += prefix.count(' ')
204
- args = event.message.content.slice!(args[0].size + args[1].size + spaces, event.message.content.size)
205
- else
206
- args = event.message.content.slice!(args[0].size + spaces, event.message.content.size)
207
- end
208
- # Split the argmuents into an array for easy usage but keep the raw args !!
209
- rawargs = args
210
- args = args.split(/ /)
211
- rescue NoMethodError # Not the most elegant solution but it'll do. TODO: Make a more elegant solution.
212
- args = []
213
- end
214
-
215
-
216
- # Check the number of args for the command.
217
- unless command[:max_args].nil?
218
- if command[:max_args] > 0 && args.length > command[:max_args]
219
- # May be replaced with an embed.
220
- event.channel.send_message('', false,
221
- Helper.error_embed(
222
- error: "Too many arguments! \nMax arguments: `#{command[:max_args]}`",
223
- footer: "Command: `#{event.message.content}`",
224
- colour: 0xFA0E30,
225
- code_error: false
226
- )
227
- )
228
- @finished = true
229
- break
230
- end
231
- end
232
-
233
- unless command[:required_permissions].nil?
234
- command[:required_permissions].each { |x|
235
- unless event.user.on(event.server).permission?(x,event.channel)
236
- event.channel.send_message('', false,
237
- Helper.error_embed(
238
- error: "You don't have permission for that!\nPermission required: `#{x.to_s}`",
239
- footer: "Command: `#{event.message.content}`",
240
- colour: 0xFA0E30,
241
- code_error: false
242
- )
243
- )
244
- @finished = true
245
- break
246
- end
247
- }
248
- end
249
-
250
- unless @finished
251
- # If the command is configured to catch all errors, thy shall be done.
252
- if !command[:catch_errors] || @config['catch_errors']
253
- # Run the command code!
254
- command[:code].call(event, args, rawargs)
255
- else
256
- # Run the command code, but catch all errors and output accordingly.
257
- begin
258
- command[:code].call(event, args, rawargs)
259
- rescue Exception => e
260
- event.respond("❌ An error has occured!! ```ruby\n#{e}```Please contact the bot owner with the above message for assistance.")
261
- end
262
- end
263
- end
264
-
265
- # All done here.
266
- puts "Finished!! Executed command: #{@chosen}" if @debug_mode
267
- @command = command
268
- @event = event
269
- @args = args
270
- @rawargs = rawargs
271
- @finished = true
272
- break
273
- }
86
+ chosen_activator = nil
87
+ message_content = nil
88
+ chosen_command = nil
89
+ used_prefix = ''
90
+
91
+ # If we have a usable prefix, get the raw arguments for this command.
92
+ @prefixes.each do |prefix|
93
+ next unless event.message.content.start_with?(prefix)
94
+
95
+ # Store the message's content, sans its prefix.
96
+ # Strip leading spaces in the event a prefix ends with a space.
97
+ message_content = event.message.content
98
+ used_prefix = message_content.slice! prefix
99
+ break
100
+ end
101
+
102
+ # Otherwise, do not continue processing.
103
+ next if message_content.nil?
104
+
105
+ @commands.each do |key, command|
106
+ puts ":: Considering #{key}" if @debug_mode == true
107
+ triggers = command[:triggers].nil? ? [key.to_s] : command[:triggers]
108
+
109
+ triggers.each do |trigger|
110
+ activator = trigger.to_s.downcase
111
+ puts activator if @debug_mode == true
112
+ next unless event.message.content.downcase.start_with?(activator)
113
+
114
+ puts "Prefix matched! #{activator}" if @debug_mode == true
115
+
116
+ # Continue only if you've already chosen a choice.
117
+ if chosen_activator.nil?
118
+ puts 'First match obtained!' if @debug_mode == true
119
+ chosen_activator = activator
120
+ chosen_command = command
121
+
122
+ # If the new activator begins with the chosen one, then override it.
123
+ # Example: sh is chosen, shell is the new one.
124
+ # In this example, shell would override sh, preventing ugly bugs.
125
+ elsif activator.start_with?(chosen_activator)
126
+ puts "#{activator} just overrode #{chosen_activator}" if @debug_mode == true
127
+ chosen_activator = activator
128
+ chosen_command = command
129
+ # Otherwise, just give up.
130
+ elsif @debug_mode == true
131
+ puts 'Match failed...'
132
+ end
133
+ # If you haven't chosen yet, get choosing!
274
134
  end
275
- }
135
+
136
+ puts "Result: #{chosen_activator}" if @debug_mode == true
137
+ end
138
+
139
+ # If we have no chosen activator, it is likely the command does not exist
140
+ # or the prefix itself was run.
141
+ next if chosen_activator.nil?
142
+
143
+ command_run = used_prefix + chosen_activator
144
+ puts "Final result: #{command_run}" if @debug_mode == true
145
+
146
+ # Command flag defaults
147
+ chosen_command[:owners_only] = false if chosen_command[:owners_only].nil?
148
+ chosen_command[:server_only] = false if chosen_command[:server_only].nil?
149
+ chosen_command[:typing] = @config[:typing_default] if chosen_command[:typing_default].nil?
150
+ if chosen_command[:delete_activator].nil?
151
+ chosen_command[:delete_activator] =
152
+ @config[:delete_activators]
153
+ end
154
+ chosen_command[:owner_override] = false if chosen_command[:owner_override].nil?
155
+
156
+ # If the settings are to delete activating messages, then do that.
157
+ # I'm *hoping* this doesn't cause issues with argument extraction.
158
+ event.message.delete if chosen_command[:delete_activator]
159
+
160
+ # If the command is only for use in servers, display error and abort.
161
+ if chosen_command[:server_only] && event.channel.private?
162
+ event.channel.send_embed error_embed(
163
+ error: 'This command can only be used in servers!',
164
+ footer: "Command: `#{command_run}`"
165
+ )
166
+ break
167
+ end
168
+
169
+ # If the user is a bot and the command is set to not pass bots
170
+ # OR the user is a bot and the global config is to not parse bots...
171
+ # ...then abort :3
172
+ if event.user.bot_account? && \
173
+ (chosen_command[:parse_bots] == false || @config[:parse_bots] == false)
174
+ # Abort!
175
+ break
176
+ end
177
+
178
+ # If the config is setup to show typing messages, then do so.
179
+ event.channel.start_typing if chosen_command[:typing]
180
+
181
+ # Our arguments are the message's contents, minus the activator.
182
+ args = message_content
183
+ args.slice! chosen_activator
184
+ args = args.split
185
+
186
+ no_permission = false
187
+
188
+ chosen_command[:required_permissions]&.each do |x|
189
+ if event.user.on(event.server).permission?(x, event.channel) \
190
+ || (chosen_command[:owner_override] && @config[:owners].include?(event.user.id))
191
+ next
192
+ end
193
+
194
+ event.channel.send_embed '', error_embed(
195
+ error: "You don't have permission for that!\nPermission required: `#{x}`",
196
+ footer: "Command: `#{command_run}`"
197
+ )
198
+ no_permission = true
199
+ break
200
+ end
201
+
202
+ next if no_permission
203
+
204
+ # If the command is set to owners only and the user is not the owner,
205
+ # show an error and abort.
206
+ puts "[DEBUG] Command being processed: '#{chosen_command}'" if @debug_mode == true
207
+ puts "[DEBUG] Owners only? #{chosen_command[:owners_only]}" if @debug_mode == true
208
+ if chosen_command[:owners_only] && !owner?(event.user.id)
209
+ event.channel.send_embed '', error_embed(
210
+ error: "You don't have permission for that!\n"\
211
+ 'Only owners are allowed to access this command.',
212
+ footer: "Command: `#{command_run}`"
213
+ )
214
+ next
215
+ end
216
+
217
+ # Run the command code!
218
+ # TODO: determine a good method to log other errors as made via the command.
219
+ # Without, we will simply log to console.
220
+ chosen_command[:code].call(event, args, message_content)
221
+
222
+ # All done here.
223
+ puts "Finished!! Executed command: #{chosen_activator}" if @debug_mode == true
224
+ next
276
225
  end
277
226
  end
278
227
  end
data/lib/helper.rb CHANGED
@@ -1,37 +1,48 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CommandrbBot
2
- module Helper
3
- def self.error_embed(error: nil, footer: nil, colour: nil, color: nil, code_error: true)
4
- if error.nil? or footer.nil?
5
- raise 'Invalid arguments for Helper.error_embed!'
6
- else
7
- if color.nil? and colour.nil?
8
- colour = 0x22ef1f
9
- end
10
- Discordrb::Webhooks::Embed.new(
11
- title: '❌ An error has occured!',
12
- description: code_error ? "```ruby\n#{error}```" : error,
13
- colour: colour || color,
14
- footer: Discordrb::Webhooks::EmbedFooter.new(text: footer)
15
- )
16
- end
17
- end
18
-
19
- def self.avatar_embed(color: nil, colour: nil, url: nil, username: nil, time: Time.now.getutc.asctime)
20
- if url.nil?
21
- raise 'Invalid arguments for Helper.avatar_embed!'
22
- else
23
- if color.nil? and colour.nil?
24
- colour = 0x22ef1f
25
- end
26
- username = username.nil? ? 'Unknown User' : username
27
- Discordrb::Webhooks::Embed.new(
28
- colour: colour || color,
29
- image: Discordrb::Webhooks::EmbedImage.new(url: url),
30
- author: Discordrb::Webhooks::EmbedAuthor.new(name: "Avatar for #{username}", url: url),
31
- footer: Discordrb::Webhooks::EmbedFooter.new(text: "Avatar correct as of #{time}")
32
- )
33
- end
34
- end
35
-
36
- end
4
+ # Utilizes several methods to attempt to determine a user.
5
+ def user_parse(context)
6
+ # Can't do anything if there's nothing to begin with.
7
+ return nil if context.nil?
8
+
9
+ # Catches cases such as "0": obviously invalid, attempted nonetheless.
10
+ context = context.to_s
11
+
12
+ # If it's an ID.
13
+ id_check = bot.user(context)
14
+ return id_check unless id_check.nil?
15
+
16
+ # If it's a mention!
17
+ matches = /<@!?(\d+)>/.match(context)
18
+ return bot.user(matches[1]) unless matches.nil?
19
+
20
+ # Might be a username...
21
+ return bot.find_user(context)[0] unless bot.find_user(context).nil?
22
+
23
+ nil
24
+ end
25
+
26
+ # Generates a usable error embed with defaults.
27
+ def error_embed(error: nil, footer: nil, colour: nil, color: nil)
28
+ raise 'Invalid arguments for Helper.error_embed!' if error.nil? || footer.nil?
29
+
30
+ colour = 0xFA0E30 if color.nil? && colour.nil?
31
+ Discordrb::Webhooks::Embed.new(
32
+ title: '❌ An error has occurred!',
33
+ description: error,
34
+ colour: colour || color,
35
+ footer: Discordrb::Webhooks::EmbedFooter.new(text: footer)
36
+ )
37
+ end
38
+
39
+ # Generates a usable error embed with defaults, and a formatted error.
40
+ def code_embed(error: nil, footer: nil, colour: nil, color: nil)
41
+ raise 'Invalid arguments for Helper.code_embed!' if error.nil? || footer.nil?
42
+
43
+ # Format to have a code block with formatting.
44
+ error = "```ruby\n#{error}```"
45
+
46
+ error_embed(error: error, footer: footer, colour: colour, color: color)
47
+ end
37
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commandrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
- - Erisa Komuro (Seriel)
8
- autorequire:
7
+ - Erisa A
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-15 00:00:00.000000000 Z
11
+ date: 2022-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb
@@ -31,18 +31,19 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.1.0
33
33
  description: A customisable and easy to use Commands System for Discordrb.
34
- email: seriel@fl0.co
34
+ email: erisa@erisa.uk
35
35
  executables: []
36
36
  extensions: []
37
37
  extra_rdoc_files: []
38
38
  files:
39
39
  - lib/commandrb.rb
40
40
  - lib/helper.rb
41
- homepage: https://github.com/Seriell/commandrb
41
+ homepage: https://github.com/Yuuki-Discord/commandrb
42
42
  licenses:
43
43
  - MIT
44
- metadata: {}
45
- post_install_message:
44
+ metadata:
45
+ rubygems_mfa_required: 'true'
46
+ post_install_message:
46
47
  rdoc_options: []
47
48
  require_paths:
48
49
  - lib
@@ -50,16 +51,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
51
  requirements:
51
52
  - - ">="
52
53
  - !ruby/object:Gem::Version
53
- version: '2.1'
54
+ version: '2.5'
54
55
  required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  requirements:
56
57
  - - ">="
57
58
  - !ruby/object:Gem::Version
58
59
  version: '0'
59
60
  requirements: []
60
- rubyforge_project:
61
- rubygems_version: 2.6.12
62
- signing_key:
61
+ rubygems_version: 3.3.7
62
+ signing_key:
63
63
  specification_version: 4
64
64
  summary: Commandrb
65
65
  test_files: []