Finished writing the main function. Detects cheese keywords in messages and added user/server blacklist/whitelist.

This commit is contained in:
2026-01-12 16:05:48 -03:00
parent 903ccf91b2
commit c8e77e1cff
7 changed files with 368 additions and 7 deletions

View File

@@ -0,0 +1,38 @@
# cheeseBot
# Copyright (C) 2026 Eri (csxkdv/nxkdv) nxkdv@thenight.club
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
require_relative '../lists/cheese_words'
module CheeseChecker
WORDS_LIST = Lists::CHEESE_WORDS
PUNCTUATION = "!\"£$%^&*()[]{}'@#~;:,<.>/?-+\\|`¬"
NO_CHEESE = /[\s#{Regexp.escape(PUNCTUATION)}]*/
pattern_strings = WORDS_LIST.map do |word|
Regexp.escape(word).chars.map { |char| char + NO_CHEESE.source }.join
end
CHEESE_REGEX = /((?:#{pattern_strings.join('|')}))/im
def self.check_content(content)
changed_content = content.gsub(CHEESE_REGEX, "**\\1**")
return nil if changed_content == content
changed_content
end
end

118
src/modules/database.rb Normal file
View File

@@ -0,0 +1,118 @@
# cheeseBot
# Copyright (C) 2026 Eri (csxkdv/nxkdv) nxkdv@thenight.club
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
require 'pg'
module Database
DB_NAME = ENV['DB_NAME']
DB_USER = ENV['DB_USER']
DB_HOST = ENV['DB_HOST']
DB_PASSWORD = ENV['DB_PASSWORD']
def self.setup
create_database_if_not_exists
create_tables
end
def self.connect(db_name = DB_NAME)
PG.connect(
dbname: db_name,
user: DB_USER,
host: DB_HOST,
password: DB_PASSWORD
)
rescue PG::Error => e
puts "Database connection error: #{e.message}"
exit
end
def self.user_blacklisted?(user_id)
exists?("blacklist", user_id)
end
def self.server_blacklisted?(server_id)
exists?("server_blacklist", server_id)
end
def self.toggle_user_blacklist(user_id)
toggle("blacklist", user_id)
end
def self.toggle_server_blacklist(server_id)
toggle("server_blacklist", server_id)
end
private
def self.exists?(table, id)
conn = connect
result = conn.exec_params("SELECT 1 FROM #{table} WHERE id = $1", [id])
conn.close
!result.ntuples.zero?
end
def self.toggle(table, id)
conn = connect
if exists?(table, id)
conn.exec_params("DELETE FROM #{table} WHERE id = $1", [id])
conn.close
return false
else
conn.exec_params("INSERT INTO #{table} (id) VALUES ($1)", [id])
conn.close
return true
end
end
def self.create_database_if_not_exists
begin
conn = connect('postgres')
rescue PG::Error
puts "Could not connect to 'postgres' system database. Please create '#{DB_NAME}' manually."
return
end
result = conn.exec_params("SELECT 1 FROM pg_database WHERE datname = $1", [DB_NAME])
if result.ntuples.zero?
puts "Database '#{DB_NAME}' not found. Creating it..."
conn.exec("CREATE DATABASE \"#{DB_NAME}\"")
puts "Database '#{DB_NAME}' created successfully!"
end
conn.close
end
def self.create_tables
conn = connect
conn.exec(<<~SQL)
CREATE TABLE IF NOT EXISTS blacklist (
id BIGINT PRIMARY KEY
);
SQL
conn.exec(<<~SQL)
CREATE TABLE IF NOT EXISTS server_blacklist (
id BIGINT PRIMARY KEY
);
SQL
puts "Tables verified."
conn.close
end
end