Migrations, new chat_member table

This commit is contained in:
Nefrace 2024-10-27 23:19:51 +03:00
parent c5490d64a3
commit 2e5c9ad57c
5 changed files with 128 additions and 10 deletions

82
20241027151548_init.sql Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -104,6 +104,7 @@ type Captcha struct {
MessageID int `db:"message_id"` MessageID int `db:"message_id"`
CorrectAnswer string `db:"correct_answer"` CorrectAnswer string `db:"correct_answer"`
BlockedUntil int64 `db:"blocked_until"` BlockedUntil int64 `db:"blocked_until"`
Created int64 `db:"created"`
} }
var db *sqlx.DB var db *sqlx.DB

View File

@ -19,9 +19,11 @@ var BOT_NAME string
func main() { func main() {
log.SetFlags(log.Lshortfile + log.Ltime + log.Ldate) log.SetFlags(log.Lshortfile + log.Ltime + log.Ldate)
InitResources() InitResources()
err := godotenv.Load()
if err != nil { error := godotenv.Load()
log.Println("No .env loaded. Relying on existing variables")
if error != nil {
log.Println("no .env loaded. relying on existing variables")
} }
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
@ -32,20 +34,20 @@ func main() {
} }
defer db.Close() defer db.Close()
db.MustExec(schema) // db.MustExec(schema)
opts := []bot.Option{ opts := []bot.Option{
bot.WithMiddlewares(logChats, logUsers, checkRegistered), bot.WithMiddlewares(logChats, logUsers, checkRegistered),
bot.WithDefaultHandler(defaultHandler), bot.WithDefaultHandler(defaultHandler),
} }
b, err := bot.New(os.Getenv("TG_TOKEN"), opts...) b, error := bot.New(os.Getenv("TG_TOKEN"), opts...)
if err != nil { if error != nil {
panic(err) panic(error)
} }
me, err := b.GetMe(ctx) me, error := b.GetMe(ctx)
if err != nil { if error != nil {
log.Fatalf("Can't get me: %v", err) log.Fatalf("Can't get me: %v", error)
} }
BOT_NAME = me.Username BOT_NAME = me.Username
log.Println("Using name", BOT_NAME) log.Println("Using name", BOT_NAME)