From 98c854929d6af0abf11f5e875ec5ae070b592d98 Mon Sep 17 00:00:00 2001 From: csxkdv Date: Tue, 6 Jan 2026 15:07:48 -0300 Subject: [PATCH] Updated load_commands. Now it can load commands inside src/commands and subfolders inside commands folder. --- src/bot.rb | 40 ++++++++++------------------- src/commands/{ => testing}/echo.rb | 0 src/commands/{ => testing}/embed.rb | 0 src/commands/{ => testing}/ping.rb | 0 4 files changed, 14 insertions(+), 26 deletions(-) rename src/commands/{ => testing}/echo.rb (100%) rename src/commands/{ => testing}/embed.rb (100%) rename src/commands/{ => testing}/ping.rb (100%) diff --git a/src/bot.rb b/src/bot.rb index 8654978..56b38dc 100644 --- a/src/bot.rb +++ b/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) diff --git a/src/commands/echo.rb b/src/commands/testing/echo.rb similarity index 100% rename from src/commands/echo.rb rename to src/commands/testing/echo.rb diff --git a/src/commands/embed.rb b/src/commands/testing/embed.rb similarity index 100% rename from src/commands/embed.rb rename to src/commands/testing/embed.rb diff --git a/src/commands/ping.rb b/src/commands/testing/ping.rb similarity index 100% rename from src/commands/ping.rb rename to src/commands/testing/ping.rb