diff --git a/.gitignore b/.gitignore index c5e82d7..5ad2f79 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -bin \ No newline at end of file +bin +.env +.vscode \ No newline at end of file diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..914618b --- /dev/null +++ b/db/db.go @@ -0,0 +1,127 @@ +package db + +import ( + "context" + "errors" + "log" + "os" + "time" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +type DB struct { + Initialized bool + Ctx context.Context + URI string + Database *mongo.Database + Client *mongo.Client +} + +var database DB = DB{} + +func Init(URI string) (DB, error) { + if (DB{}) != database { + return DB{}, errors.New("database already exists") + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + client, err := mongo.Connect(ctx, options.Client().ApplyURI(URI).SetAuth(options.Credential{Username: os.Getenv("DB_USER"), Password: os.Getenv("DB_PASSWORD")})) + if err != nil { + return DB{}, err + } + db := client.Database("godotkicker") + database = DB{ + URI: URI, + Database: db, + Client: client, + } + Log("startup") + return database, nil +} + +func Log(event string) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + t := time.Now() + log.Printf("%v :: EVENT: %s", t, event) + _, err := database.Database.Collection("logs").InsertOne(ctx, bson.D{ + primitive.E{Key: "time", Value: t}, + primitive.E{Key: "event", Value: event}, + }) + if err != nil { + log.Print(err) + } + +} + +func GetDatabase() DB { + return database +} + +func (d *DB) Stop() { + if err := d.Client.Disconnect(d.Ctx); err != nil { + panic(err) + } +} + +func (d *DB) NewEntry(ctx context.Context, collectionName string, entry interface{}) error { + collection := d.Database.Collection(collectionName) + res, err := collection.InsertOne(ctx, entry) + if err != nil { + return err + } + log.Printf("New entry: %v\nDB ID: %v\n", entry, res.InsertedID) + return nil +} + +func (d *DB) EntryExists(ctx context.Context, collectionName string, filter interface{}) bool { + collection := d.Database.Collection(collectionName) + var result bson.D + err := collection.FindOne(ctx, filter).Decode(&result) + if err != nil { + log.Printf("EntryExists error: %v", err) + } + return err != mongo.ErrNoDocuments +} + +func (d *DB) ChatExists(ctx context.Context, chat Chat) bool { + filter := bson.D{primitive.E{Key: "id", Value: chat.Id}} + return d.EntryExists(ctx, "chats", filter) +} + +func (d *DB) UserExists(ctx context.Context, user User) bool { + filter := bson.D{ + primitive.E{Key: "id", Value: user.Id}, + primitive.E{Key: "chat_id", Value: user.ChatId}, + } + return d.EntryExists(ctx, "users", filter) +} + +func (d *DB) NewChat(ctx context.Context, chat Chat) error { + if d.ChatExists(ctx, chat) { + return errors.New("chat entry already exists") + } + err := d.NewEntry(ctx, "chats", chat) + if err != nil { + log.Fatal(err) + return err + } + log.Printf("New chat added: %s (%d)\n", chat.Title, chat.Id) + return nil +} + +func (d *DB) NewUser(ctx context.Context, user User) error { + if d.UserExists(ctx, user) { + return errors.New("user entry already exists") + } + err := d.NewEntry(ctx, "users", user) + if err != nil { + return err + } + log.Printf("New user: %v\n", user) + return nil +} diff --git a/db/structs.go b/db/structs.go new file mode 100644 index 0000000..59ba5f8 --- /dev/null +++ b/db/structs.go @@ -0,0 +1,16 @@ +package db + +type Chat struct { + Id int64 + Title string +} + +type User struct { + Id int64 + ChatId int64 `bson:"chat_id"` + Username string `bson:"username"` + FirstName string `bson:"first_name"` + LastName string `bson:"last_name"` + CorrectAnswer int8 `bson:"correct_answer"` + IsBanned bool `bson:"is_banned"` +} diff --git a/go.mod b/go.mod index 7148e7e..69e104c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,18 @@ module kickerbot go 1.17 require ( - github.com/pkg/errors v0.8.1 // indirect + github.com/go-stack/stack v1.8.0 // indirect + github.com/golang/snappy v0.0.1 // indirect + github.com/joho/godotenv v1.4.0 // indirect + github.com/klauspost/compress v1.13.6 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/xdg-go/pbkdf2 v1.0.0 // indirect + github.com/xdg-go/scram v1.0.2 // indirect + github.com/xdg-go/stringprep v1.0.2 // indirect + github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect + go.mongodb.org/mongo-driver v1.8.0 // indirect + golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f // indirect + golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect + golang.org/x/text v0.3.5 // indirect gopkg.in/tucnak/telebot.v3 v3.0.0-20211126232936-7f936709f3ee // indirect ) diff --git a/go.sum b/go.sum index 9fc3e9a..e4830ce 100644 --- a/go.sum +++ b/go.sum @@ -6,34 +6,75 @@ github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvSc github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/goccy/go-yaml v1.9.1/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA= +github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w= +github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= +github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc= +github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +go.mongodb.org/mongo-driver v1.8.0 h1:R/P/JJzu8LJvJ1lDfph9GLNIKQxEtIHFfnUUUve35zY= +go.mongodb.org/mongo-driver v1.8.0/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU= +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/tucnak/telebot.v2 v2.4.1 h1:bUOFHtHhuhPekjHGe1Q1BmITvtBLdQI4yjSMC405KcU= gopkg.in/tucnak/telebot.v2 v2.4.1/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8= gopkg.in/tucnak/telebot.v3 v3.0.0-20211126232936-7f936709f3ee h1:0jS8G549Rie2L+BvXC+O+HPVyC+8gq3SpR/p2sJSfqg= gopkg.in/tucnak/telebot.v3 v3.0.0-20211126232936-7f936709f3ee/go.mod h1:1XHg/CpPZtstsm3WY57h1T4X/EQquwOgllQl/TjjgqI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/kicker/handlers.go b/kicker/handlers.go index 308bd1d..2b0abc5 100644 --- a/kicker/handlers.go +++ b/kicker/handlers.go @@ -1,15 +1,45 @@ package kicker import ( + "context" + "kickerbot/db" + "log" + "time" + tb "gopkg.in/tucnak/telebot.v3" ) var HandlersV1 = []Handler{ + // { + // Endpoint: tb.OnText, + // Handler: func(c tb.Context) error { + // m := c.Message() + // c.Bot().Send(m.Sender, m.Text) + // return nil + // }, + // }, + { + Endpoint: tb.OnAddedToGroup, + Handler: func(c tb.Context) error { + m := c.Message() + chat := db.Chat{ + Id: m.Chat.ID, + Title: m.Chat.Title, + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + d := db.GetDatabase() + err := d.NewChat(ctx, chat) + if err != nil { + log.Print(err) + } + return nil + }, + }, { Endpoint: tb.OnText, Handler: func(c tb.Context) error { - m := c.Message() - c.Bot().Send(m.Sender, m.Text) + c.Bot().Send(&tb.User{ID: 60441930}, c.Message().Text) return nil }, }, diff --git a/main.go b/main.go index ddb25b2..e0e0203 100644 --- a/main.go +++ b/main.go @@ -1,17 +1,27 @@ package main import ( + "kickerbot/db" + "kickerbot/kicker" "log" "os" - "kickerbot/kicker" + "github.com/joho/godotenv" ) func main() { + err := godotenv.Load() + if err != nil { + log.Print("Error loading .env file") + } token, exists := os.LookupEnv("TOKEN") if !exists { log.Fatal("no token specified") } + _, dberr := db.Init("mongodb://127.0.0.1:27017") + if dberr != nil { + log.Fatal(err) + } Bot := kicker.Kicker{Token: token} Bot.Init()