Updated load_commands. Now it can load commands inside src/commands and subfolders inside commands folder.
This commit is contained in:
40
src/bot.rb
40
src/bot.rb
@@ -27,7 +27,7 @@ class FrugalityBot
|
||||
@db = Database.new
|
||||
|
||||
load_commands
|
||||
setup_events
|
||||
startup_bot
|
||||
end
|
||||
|
||||
def run
|
||||
@@ -37,34 +37,22 @@ class FrugalityBot
|
||||
private
|
||||
|
||||
def load_commands
|
||||
# 1. We look for all .rb files in "src/commands/..."
|
||||
comm_files = Dir[File.join(__dir__, 'commands', '*.rb')]
|
||||
|
||||
comm_files.each do |file|
|
||||
require file # We import the file
|
||||
|
||||
# We convert filename to module name
|
||||
# This mean that 'echo.rb' turns into 'Echo'
|
||||
# 'server_info' would turn into 'ServerInfo'
|
||||
filename = File.basename(file, '.rb')
|
||||
module_name = filename.split('_').map(&:capitalize).join
|
||||
|
||||
begin
|
||||
comm_module = Commands.const_get(module_name)
|
||||
|
||||
comm_module.register(@bot, @db)
|
||||
puts "Loaded command: #{module_name}"
|
||||
sleep(1.5)
|
||||
rescue NameError => e
|
||||
puts "Could not load #{filename}: Module 'Commands::#{module_name}' was not found."
|
||||
rescue StandardError => e
|
||||
puts "Error loading: #{filename}: #{e.message}"
|
||||
end
|
||||
Dir["#{File.dirname(__FILE__)}/commands/**/*.rb"].each do |file|
|
||||
require file
|
||||
end
|
||||
|
||||
Commands.constants.each do |const|
|
||||
cmd = Commands.const_get(const)
|
||||
|
||||
if cmd.is_a?(Module) && cmd.respond_to?(:register)
|
||||
cmd.register(@bot, @db)
|
||||
puts "Loaded command: #{const}"
|
||||
sleep(1.5)
|
||||
end
|
||||
end
|
||||
puts "Commands loaded."
|
||||
end
|
||||
|
||||
def setup_events
|
||||
def startup_bot
|
||||
@bot.ready do
|
||||
puts "#{@bot.profile.username} is online"
|
||||
@bot.update_status("online", "Checking the economy...", nil, 0, false, 0)
|
||||
|
||||
Reference in New Issue
Block a user