diff --git a/20241027151548_init.sql b/20241027151548_init.sql new file mode 100644 index 0000000..e436961 --- /dev/null +++ b/20241027151548_init.sql @@ -0,0 +1,82 @@ +-- +goose Up +-- +goose StatementBegin +SELECT 'up SQL query'; + +create table if not exists chats +( +id INTEGER PRIMARY KEY, +name TEXT NOT NULL, +username TEXT DEFAULT '', +topic INTEGER DEFAULT 0, +active INTEGER DEFAULT 0, +rules_msg TEXT DEFAULT '' +); + +create table if not exists activations +( +id INTEGER PRIMARY KEY AUTOINCREMENT, +code TEXT NOT NULL +); + +create table if not exists messagesToDelete +( +id INTEGER PRIMARY KEY AUTOINCREMENT, +message_id INTEGER NOT NULL, +chat_id INTEGER NOT NULL, +delete_date INTEGER NOT NULL, +tries INTEGER DEFAULT 0 +); + +create table if not exists users +( +id INTEGER PRIMARY KEY, +name TEXT NOT NULL, +username TEXT DEFAULT '' +); + +create table if not exists admins +( +id INTEGER PRIMARY KEY AUTOINCREMENT, +user_id INTEGER NOT NULL, +chat_id INTEGER NOT NULL, +FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, +FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE +); + +create table if not exists bans +( +id INTEGER PRIMARY KEY AUTOINCREMENT, +chat_id INTEGER NOT NULL, +user_id INTEGER NOT NULL, +text TEXT DEFAULT '', +reason TEXT DEFAULT '', +ban_date INTEGER NOT NULL, +unban_date INTEGER DEFAULT 0, +FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, +FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE +); + +create table if not exists captchas +( +id INTEGER PRIMARY KEY AUTOINCREMENT, +user_id INTEGER, +chat_id INTEGER, +message_id INTEGER, +correct_answer TEXT DEFAULT '', +blocked_until INTEGER DEFAULT 0, +FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, +FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE +) +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +SELECT 'down SQL query'; +drop table chats; +drop table activations; +drop table messagesToDelete; +drop table users; +drop table admins; +drop table bans; +drop table captchas; +-- +goose StatementEnd diff --git a/20241027153235_add_captcha_create_date.sql b/20241027153235_add_captcha_create_date.sql new file mode 100644 index 0000000..c68d8be --- /dev/null +++ b/20241027153235_add_captcha_create_date.sql @@ -0,0 +1,13 @@ +-- +goose Up +-- +goose StatementBegin +SELECT 'up SQL query'; +alter table captchas +add column created INTEGER DEFAULT 0; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +SELECT 'down SQL query'; +alter table captchas +drop column created; +-- +goose StatementEnd diff --git a/20241027153926_add_chatmembers.sql b/20241027153926_add_chatmembers.sql new file mode 100644 index 0000000..b6145d0 --- /dev/null +++ b/20241027153926_add_chatmembers.sql @@ -0,0 +1,20 @@ +-- +goose Up +-- +goose StatementBegin +SELECT 'up SQL query'; +create table if not exists chat_members +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + is_admin INTEGER DEFAULT 0, + chat_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + created INTEGER NOT NULL, + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE +); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +SELECT 'down SQL query'; +drop table chat_members; +-- +goose StatementEnd diff --git a/src/db.go b/src/db.go index 8f23492..9dfb11a 100644 --- a/src/db.go +++ b/src/db.go @@ -104,6 +104,7 @@ type Captcha struct { MessageID int `db:"message_id"` CorrectAnswer string `db:"correct_answer"` BlockedUntil int64 `db:"blocked_until"` + Created int64 `db:"created"` } var db *sqlx.DB diff --git a/src/main.go b/src/main.go index 16b9047..3b3ea26 100644 --- a/src/main.go +++ b/src/main.go @@ -19,9 +19,11 @@ var BOT_NAME string func main() { log.SetFlags(log.Lshortfile + log.Ltime + log.Ldate) InitResources() - err := godotenv.Load() - if err != nil { - log.Println("No .env loaded. Relying on existing variables") + + error := godotenv.Load() + + if error != nil { + log.Println("no .env loaded. relying on existing variables") } ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) @@ -32,20 +34,20 @@ func main() { } defer db.Close() - db.MustExec(schema) + // db.MustExec(schema) opts := []bot.Option{ bot.WithMiddlewares(logChats, logUsers, checkRegistered), bot.WithDefaultHandler(defaultHandler), } - b, err := bot.New(os.Getenv("TG_TOKEN"), opts...) - if err != nil { - panic(err) + b, error := bot.New(os.Getenv("TG_TOKEN"), opts...) + if error != nil { + panic(error) } - me, err := b.GetMe(ctx) - if err != nil { - log.Fatalf("Can't get me: %v", err) + me, error := b.GetMe(ctx) + if error != nil { + log.Fatalf("Can't get me: %v", error) } BOT_NAME = me.Username log.Println("Using name", BOT_NAME)