package main import ( "log" "github.com/jmoiron/sqlx" ) var schema = ` 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 ); 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 ) ` type Chat struct { Id int64 `json:"id" db:"id"` Name string `json:"name" db:"name"` Username string `json:"username" db:"username"` Topic int `json:"topic" db:"topic"` Active bool `json:"active" db:"active"` } type MessageToDelete struct { Id int64 `db:"id"` MessageId int `db:"message_id"` ChatId int64 `db:"chat_id"` DeleteDate int64 `db:"delete_date"` Tries int `db:"tries"` } type User struct { Id int64 `db:"id"` Name string `db:"name"` Username string `db:"username"` } type Captcha struct { Id int64 `db:"id"` UserID int64 `db:"user_id"` ChatID int64 `db:"chat_id"` MessageID int `db:"message_id"` CorrectAnswer string `db:"correct_answer"` BlockedUntil int64 `db:"blocked_until"` } var db *sqlx.DB func InitDb() error { newdb, err := sqlx.Connect("sqlite", "./bot.db?_time_format=sqlite") if err != nil { return err } db = newdb return nil } func NewChat(chat Chat) error { _, err := db.NamedExec(`insert into chats (id, name, username, topic, active) values (:id, :name, :username, :topic, :active)`, chat) return err } func IsChatExists(id int64) bool { var exists bool err := db.Get(&exists, `SELECT exists(SELECT 1 FROM chats WHERE id = $1);`, id) if err != nil { log.Println("Can't check existing of chat", id, err) return false } return exists } func IsChatActive(id int64) bool { var active bool err := db.Get(&active, `SELECT active from chats where id = $1`, id) if err != nil { return false } return active } func GetChatById(id int64) (Chat, error) { c := Chat{} err := db.Get(&c, `select * from chats where id = $1`, id) return c, err } func NewActivation(code string) error { _, err := db.Exec(`insert into activations (code) values ($1)`, code) return err } func UseActivation(code string) bool { exists := false err := db.Get(&exists, `SELECT exists(SELECT 1 FROM activations WHERE code = $1);`, code) if err != nil || !exists { return false } _, err = db.Exec(`delete from activations where code = $1`, code) return err == nil } func ActivateChat(id int64, thread int) error { _, err := db.Exec(`update chats set active = 1, topic = $2 where id = $1`, id, thread) return err } func AddMessageToDelete(msg MessageToDelete) error { _, err := db.NamedExec(`insert into messagesToDelete (message_id, chat_id, delete_date, tries) values (:message_id, :chat_id, :delete_date, 0)`, msg) return err } func NewUser(user User) error { _, err := db.NamedExec(`insert into users (id, name, username) values (:id, :name, :username)`, user) return err } func IsUserExists(id int64) bool { var exists bool err := db.Get(&exists, `SELECT exists(SELECT 1 FROM users WHERE id = $1);`, id) if err != nil { log.Println("Can't check existing of chat", id, err) return false } return exists } func GetUserById(id int64) (User, error) { c := User{} err := db.Get(&c, `select * from users where id = $1`, id) return c, err }