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 @db = Database.new
load_commands load_commands
setup_events startup_bot
end end
def run def run
@@ -37,34 +37,22 @@ class FrugalityBot
private private
def load_commands def load_commands
# 1. We look for all .rb files in "src/commands/..." Dir["#{File.dirname(__FILE__)}/commands/**/*.rb"].each do |file|
comm_files = Dir[File.join(__dir__, 'commands', '*.rb')] require file
end
comm_files.each do |file|
require file # We import the file Commands.constants.each do |const|
cmd = Commands.const_get(const)
# We convert filename to module name
# This mean that 'echo.rb' turns into 'Echo' if cmd.is_a?(Module) && cmd.respond_to?(:register)
# 'server_info' would turn into 'ServerInfo' cmd.register(@bot, @db)
filename = File.basename(file, '.rb') puts "Loaded command: #{const}"
module_name = filename.split('_').map(&:capitalize).join sleep(1.5)
end
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
end end
puts "Commands loaded."
end end
def setup_events def startup_bot
@bot.ready do @bot.ready do
puts "#{@bot.profile.username} is online" puts "#{@bot.profile.username} is online"
@bot.update_status("online", "Checking the economy...", nil, 0, false, 0) @bot.update_status("online", "Checking the economy...", nil, 0, false, 0)