Updated load_commands. Now it can load commands inside src/commands and subfolders inside commands folder.

This commit is contained in:
2026-01-06 15:07:48 -03:00
parent f5ec2f38fc
commit 98c854929d
4 changed files with 14 additions and 26 deletions

View File

@@ -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)