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
|
||||
20
src/commands/currency.rb
Normal file
20
src/commands/currency.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module Commands
|
||||
module Currency
|
||||
extend self
|
||||
|
||||
def register(bot, db)
|
||||
bot.register_application_command(:currency, 'Get your currency', server_id: ENV['TEST_SERVER_ID'])
|
||||
|
||||
bot.application_command(:currency) do |event|
|
||||
# 1. Get the User ID from the event
|
||||
user_id = event.user.id
|
||||
|
||||
# 2. Ask the DB for the balance
|
||||
balance = db.get_currency(user_id)
|
||||
|
||||
# 3. Respond to Discord
|
||||
event.respond(content: "You have #{balance} coins.")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
14
src/commands/echo.rb
Normal file
14
src/commands/echo.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module Commands
|
||||
module Echo
|
||||
extend self
|
||||
def register(bot, _db)
|
||||
bot.register_application_command(:echo, 'Repeats what you say', server_id: ENV['TEST_SERVER_ID']) do |cmd|
|
||||
cmd.string('message', 'The text you want the bot to repeat', required: true)
|
||||
end
|
||||
|
||||
bot.application_command(:echo) do |event|
|
||||
event.respond(content: "You said: #{event.options['message']}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
13
src/commands/ping.rb
Normal file
13
src/commands/ping.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module Commands
|
||||
module Ping
|
||||
extend self
|
||||
|
||||
def register(bot, _db)
|
||||
bot.register_application_command(:ping, 'Check if the bot is alive')
|
||||
|
||||
bot.application_command(:ping) do |event|
|
||||
event.respond(content: 'Pong!')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
36
src/database.rb
Normal file
36
src/database.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
require 'pg'
|
||||
|
||||
class Database
|
||||
def initialize
|
||||
# Connect once when the bot starts
|
||||
@conn = PG.connect(dbname: 'frugality_database')
|
||||
|
||||
init_tables
|
||||
end
|
||||
|
||||
def init_tables
|
||||
sql = <<~SQL
|
||||
CREATE TABLE IF NOT EXISTS total_money (
|
||||
user_id BIGINT PRIMARY KEY,
|
||||
amount BIGINT DEFAULT 0
|
||||
);
|
||||
SQL
|
||||
|
||||
@conn.exec(sql)
|
||||
puts "Database tables have been initialized."
|
||||
end
|
||||
|
||||
# We pass the user_id
|
||||
def get_currency(user_id)
|
||||
# 1. Run the query using parameters ($1) to prevent SQL injection
|
||||
result = @conn.exec_params("SELECT amount FROM total_money WHERE user_id = $1", [user_id])
|
||||
|
||||
# 2. Check if the user exists
|
||||
if result.num_tuples.zero?
|
||||
return 0 # User has no money/row yet
|
||||
else
|
||||
# 3. Return the value (don't print it)
|
||||
return result[0]['amount'].to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user