Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
######################################
############# IMPORTANT ##############
# When upgrading the RuboCop gem,
# ensure that new cops do not heavily
# modify the existing style.
# Disable any of these new cops.
######################################
AllCops:
NewCops: disable
NewCops: enable

Metrics/BlockLength:
Enabled: false
Expand All @@ -10,25 +17,15 @@ Metrics/MethodLength:
Metrics/ModuleLength:
Enabled: false

Metrics/ClassLength:
Enabled: false

# TEMPORARY
Metrics/BlockNesting:
Enabled: false

Style/Documentation:
Enabled: false

Lint/Debugger:
Exclude:
- "run.rb"

Layout/EndOfLine:
EnforcedStyle: lf

# TODO: Please revert to 100!
Layout/LineLength:
Max: 100
Max: 130

# Keep to syntactic linting, instead of complexity.
Metrics/CyclomaticComplexity:
Expand All @@ -40,19 +37,12 @@ Metrics/AbcSize:
Metrics/PerceivedComplexity:
Enabled: false

# Pending cops for RuboCop 0.80
Style/HashEachMethods:
Enabled: true

Style/HashTransformKeys:
Enabled: true

Style/HashTransformValues:
Enabled: true
# TODO: Please correct and remove!
Metrics/BlockNesting:
Enabled: false

# Pendng cops for RuboCop 0.81
Lint/RaiseException:
Enabled: true
Metrics/ClassLength:
Enabled: false

Lint/StructNewOverride:
Enabled: true
Lint/SelfAssignment:
Enabled: false
6 changes: 4 additions & 2 deletions commandrb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
Gem::Specification.new do |s|
s.name = 'commandrb'
s.version = '0.4.8'
s.date = '2020-10-03'
s.summary = 'Commandrb'
s.description = 'A customisable and easy to use Commands System for Discordrb.'
s.authors = ['Erisa A']
s.email = '[email protected]'
s.files = ['lib/commandrb.rb', 'lib/helper.rb']
s.required_ruby_version = '>= 2.4'
s.required_ruby_version = '>= 2.5'
s.homepage =
'https://github.com/Yuuki-Discord/commandrb'
s.license = 'MIT'
s.add_runtime_dependency 'discordrb', '~> 3.1', '>= 3.1.0'
s.metadata = {
'rubygems_mfa_required' => 'true'
}
end
114 changes: 51 additions & 63 deletions lib/commandrb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ def initialize(init_hash)
raise 'No token supplied in init hash!' if @config[:token].nil? || (init_hash[:token] == '')

init_parse_self = begin
init_hash[:parse_self]
rescue StandardError
nil
end
init_hash[:parse_self]
rescue StandardError
nil
end
init_type = @config[:type]

if init_type == :bot
raise 'No client ID or invalid client ID supplied in init hash!' if init_hash[:client_id].nil?
end
raise 'No client ID or invalid client ID supplied in init hash!' if init_type == :bot && init_hash[:client_id].nil?

@config[:owners] = init_hash[:owners]
@config[:owners] = [] if @config[:owners].nil?
Expand Down Expand Up @@ -145,37 +143,33 @@ def initialize(init_hash)
command[:min_args] = 0 if command[:min_args].nil?
command[:server_only] = false if command[:server_only].nil?
command[:typing] = @config[:typing_default] if command[:typing_default].nil?
if command[:delete_activator].nil?
command[:delete_activator] = @config[:delete_activators]
end
command[:delete_activator] = @config[:delete_activators] if command[:delete_activator].nil?
command[:owner_override] = false if command[:owner_override].nil?

# If the settings are to delete activating messages, then do that.
# I'm *hoping* this doesn't cause issues with argument extraction.
event.message.delete if command[:delete_activator]

# If the command is only for use in servers, display error and abort.
unless failed
if command[:server_only] && event.channel.private?
# For selfbots, a fancy embed will be used. WIP.
if @config[:selfbot]
event.channel.send_embed do |embed|
embed.colour = 0x221461
embed.title = '❌ An error has occured!'
embed.description = 'This command can only be used in servers!'
embed.footer = Discordrb::Webhooks::EmbedFooter.new(
text: "Command:'#{event.message.content}'"
)
end
else
# If its not a selfbot, an ordinary message will be shown,
# may be changed to embed later.
event.respond('❌ This command will only work in servers!')
if !failed && (command[:server_only] && event.channel.private?)
# For selfbots, a fancy embed will be used. WIP.
if @config[:selfbot]
event.channel.send_embed do |embed|
embed.colour = 0x221461
embed.title = '❌ An error has occured!'
embed.description = 'This command can only be used in servers!'
embed.footer = Discordrb::Webhooks::EmbedFooter.new(
text: "Command:'#{event.message.content}'"
)
end
# Abort!
finished = true
next
else
# If its not a selfbot, an ordinary message will be shown,
# may be changed to embed later.
event.respond('❌ This command will only work in servers!')
end
# Abort!
finished = true
next
end

# If the user is a bot and the command is set to not pass bots
Expand All @@ -193,7 +187,7 @@ def initialize(init_hash)
# If the config is setup to show typing messages, then do so.
event.channel.start_typing if command[:typing]

args = event.message.content.split(' ')
args = event.message.content.split
# Parse args if args exist !
begin
spaces = 1
Expand All @@ -217,29 +211,25 @@ def initialize(init_hash)
end

# Check the number of args for the command.
unless command[:max_args].nil? || failed
if (command[:max_args]).positive? && args.length > command[:max_args]
send_error = Helper.error_embed(
error: "Too many arguments! \nMax arguments: `#{command[:max_args]}`",
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
failed = true
end
if !(command[:max_args].nil? || failed) && ((command[:max_args]).positive? && args.length > command[:max_args])
send_error = Helper.error_embed(
error: "Too many arguments! \nMax arguments: `#{command[:max_args]}`",
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
failed = true
end

# Check the number of args for the command.
unless command[:min_args].nil? || failed
if (command[:min_args]).positive? && args.length < command[:min_args]
send_error = Helper.error_embed(
error: "Too few arguments! \nMin arguments: `#{command[:min_args]}`",
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
failed = true
end
if !(command[:min_args].nil? || failed) && ((command[:min_args]).positive? && args.length < command[:min_args])
send_error = Helper.error_embed(
error: "Too few arguments! \nMin arguments: `#{command[:min_args]}`",
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
failed = true
end

unless command[:required_permissions].nil? || failed
Expand All @@ -263,19 +253,17 @@ def initialize(init_hash)
# show error and abort.
puts "[DEBUG] Command being processed: '#{command}'" if @debug_mode == true
puts "[DEBUG] Owners only? #{command[:owners_only]}" if @debug_mode == true
if command[:owners_only]
unless owner?(event.user.id)

send_error = Helper.error_embed(
error: "You don't have permission for that!\n"\
'Only owners are allowed to access this command.',
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
failed = true
# next
end
if command[:owners_only] && !owner?(event.user.id)

send_error = Helper.error_embed(
error: "You don't have permission for that!\n"\
'Only owners are allowed to access this command.',
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
failed = true
# next
end

unless finished
Expand Down
2 changes: 1 addition & 1 deletion lib/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.avatar_embed(
raise 'Invalid arguments for Helper.avatar_embed!' if url.nil?

colour = 0x22ef1f if color.nil? && colour.nil?
username = username.nil? ? 'Unknown User' : username
username = 'Unknown User' if username.nil?
Discordrb::Webhooks::Embed.new(
colour: colour || color,
image: Discordrb::Webhooks::EmbedImage.new(url: url),
Expand Down