Skip to content

Commit

Permalink
v0.4.0 - A lot more stability.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa committed Oct 31, 2017
1 parent 27f5c8f commit 49ffd83
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
2 changes: 1 addition & 1 deletion commandrb.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'commandrb'
s.version = '0.3.2'
s.version = '0.4.0'
s.date = '2017-10-02'
s.summary = 'Commandrb'
s.description = 'A customisable and easy to use Commands System for Discordrb.'
Expand Down
66 changes: 54 additions & 12 deletions lib/commandrb.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require_relative 'helper'

class CommandrbBot

ENV['COMMANDRB_MODE'] == 'debug' ? @debug_mode = true : @debug_mode = false

# Be able to adjust the config on the fly.
attr_accessor :config
Expand Down Expand Up @@ -41,7 +45,7 @@ def initialize(init_hash)
@config[:delete_activators] = false if @config[:delete_activators].nil?

if @config[:token].nil? or init_hash[:token] == ''
puts 'No token supplied in init hash!'
raise 'No token supplied in init hash!'
return false
end

Expand All @@ -50,12 +54,13 @@ def initialize(init_hash)

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

@config[:owners] = init_hash[:owners]
@config[:owners] = [] if @config[:owners].nil?

@prefixes = init_hash[:prefixes]

Expand All @@ -76,43 +81,55 @@ def initialize(init_hash)

# Command processing
@bot.message do |event|
@finished = false
@command = nil
@event = nil
@chosen = nil
@args = nil
@rawargs = nil
@continue = false
@prefixes.each { |prefix|
break if @finished
if event.message.content.start_with?(prefix)

@commands.each { | key, command |
break if @finished
puts ":: Considering #{key.to_s}" if @debug_mode
triggers = command[:triggers].nil? ? [key.to_s] : command[:triggers]

triggers.each { |trigger|
@activator = prefix + trigger.to_s
puts @activator if @debug_mode
@activator = @activator.downcase
if event.message.content.downcase.start_with?(@activator)
puts "Prefix matched! #{@activator}" if @debug_mode

# Continue only if you've already chosen a choice.
unless @chosen.nil?
# If the new activator begins with the chosen one, then override it.
# Example: sh is chosen, shell is the new one.
# In this example, shell would override sh, preventing ugly bugs.
if @activator.start_with?(@chosen)
puts "#{@activator} just overrode #{@chosen}" if @debug_mode
@chosen = @activator
# Otherwhise, just give up.
else
puts "Match failed..." if @debug_mode
next
end
# If you haven't chosen yet, get choosing!
else
puts "First match obtained!" if @debug_mode
@continue = true
@chosen = @activator
end
end
}

puts "Result: #{@chosen}" if @debug_mode

next unless @continue
next if !@continue
puts "Final esult: #{@chosen}" if @debug_mode

break if @config[:selfbot] && event.user.id != @bot.profile.id

Expand All @@ -125,9 +142,20 @@ def initialize(init_hash)
command[:delete_activator] = @config[:delete_activators] if command[:delete_activator].nil?

# If the command is set to owners only and the user is not the owner, show error and abort.
puts "[DEBUG] Command being processed: '#{command}'" if @debug_mode
puts "[DEBUG] Owners only? #{command[:owners_only]}" if @debug_mode
if command[:owners_only]
unless @config[:owners].include?(event.user.id)
event.respond('❌ You don\'t have permission for that!')
if !@config[:owners].include?(event.user.id)
event.channel.send_message('', false,
Helper.error_embed(
error: "You don't have permission for that!\nOnly owners are allowed to access this command.",
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
)
puts 'Were returning!'
@finished = true
next
end
end
Expand All @@ -151,13 +179,15 @@ def initialize(init_hash)
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 OR the user is a bot and the global config is to not parse bots...
# ...then abort :3
if (event.user.bot_account? && command[:parse_bots] == false) || (event.user.bot_account? && @config[:parse_bots] == false)
# Abort!
@finished = true
next
end

Expand All @@ -182,12 +212,21 @@ def initialize(init_hash)


# Check the number of args for the command.
if args.length > command[:max_args]
# May be replaced with an embed.
event.respond("❌ Too many arguments! \nMax arguments: `#{command[:max_args]}`")
next
unless command[:max_args].nil?
if command[:max_args] > 0 && args.length > command[:max_args]
# May be replaced with an embed.
event.channel.send_message('', false,
Helper.error_embed(
error: "Too many arguments! \nMax arguments: `#{command[:max_args]}`",
footer: "Command: `#{event.message.content}`",
colour: 0xFA0E30,
code_error: false
)
)
break
end
end

# If the command is configured to catch all errors, thy shall be done.
if !command[:catch_errors] || @config['catch_errors']
# Run the command code!
Expand All @@ -203,18 +242,21 @@ def initialize(init_hash)

unless command[:required_permissions].nil?
command[:required_permissions].each { |x|
if event.user.on(event.server).permission?(:ban_members,event.channel)
if event.user.on(event.server).permission?(x,event.channel)
event.respond('❌ You don\'t have permission for that!')
break
@finished = true
next
end
}
end

# All done here.
puts "Finished!! Executed command: #{@chosen}" if @debug_mode
@command = command
@event = event
@args = args
@rawargs = rawargs
@finished = true
break
}
end
Expand Down
37 changes: 37 additions & 0 deletions lib/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class CommandrbBot
module Helper
def self.error_embed(error: nil, footer: nil, colour: nil, color: nil, code_error: true)
if error.nil? or footer.nil?
raise 'Invalid arguments for Helper.error_embed!'
else
if color.nil? and colour.nil?
colour = 0x22ef1f
end
Discordrb::Webhooks::Embed.new(
title: '❌ An error has occured!',
description: code_error ? "```ruby\n#{error}```" : error,
colour: colour || color,
footer: Discordrb::Webhooks::EmbedFooter.new(text: footer)
)
end
end

def self.avatar_embed(color: nil, colour: nil, url: nil, username: nil, time: Time.now.getutc.asctime)
if url.nil?
raise 'Invalid arguments for Helper.avatar_embed!'
else
if color.nil? and colour.nil?
colour = 0x22ef1f
end
username = username.nil? ? 'Unknown User' : username
Discordrb::Webhooks::Embed.new(
colour: colour || color,
image: Discordrb::Webhooks::EmbedImage.new(url: url),
author: Discordrb::Webhooks::EmbedAuthor.new(name: "Avatar for #{username}", url: url),
footer: Discordrb::Webhooks::EmbedFooter.new(text: "Avatar correct as of #{time}")
)
end
end

end
end

0 comments on commit 49ffd83

Please sign in to comment.