Main commit
This commit is contained in:
58
src/bot.rb
Normal file
58
src/bot.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
require 'discordrb'
|
||||
require_relative 'database'
|
||||
|
||||
class FrugalityBot
|
||||
def initialize
|
||||
@bot = Discordrb::Bot.new(
|
||||
token: ENV['BOT_TOKEN'],
|
||||
intents: [:servers, :server_messages]
|
||||
)
|
||||
|
||||
@db = Database.new
|
||||
|
||||
load_commands
|
||||
setup_events
|
||||
end
|
||||
|
||||
def run
|
||||
@bot.run
|
||||
end
|
||||
|
||||
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
|
||||
# We find the module inside 'Commands' namespace
|
||||
comm_module = Commands.const_get(module_name)
|
||||
|
||||
# Register the command
|
||||
comm_module.register(@bot, @db)
|
||||
puts "Loaded command: #{module_name}"
|
||||
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
|
||||
puts "Commands loaded."
|
||||
end
|
||||
|
||||
def setup_events
|
||||
@bot.ready do
|
||||
puts "#{@bot.profile.username} is online"
|
||||
@bot.update_status("online", "Checking the economy...", nil, 0, false, 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user