diff --git a/captchagen/captchagen.go b/captchagen/captchagen.go index 08c8e5b..c6d0a87 100644 --- a/captchagen/captchagen.go +++ b/captchagen/captchagen.go @@ -91,6 +91,16 @@ func (captcha *Captcha) ToReader() *bytes.Reader { return reader } +func (captcha *Captcha) ToBytes() (*[]byte, error) { + buff := new(bytes.Buffer) + err := png.Encode(buff, captcha.Image) + if err != nil { + return nil, err + } + b := buff.Bytes() + return &b, nil +} + // Инициализация списка логотипов. // // Логотипы читаются из папки /assets рядом с исполняемым файлом. diff --git a/db/methods.go b/db/methods.go index 1250ec9..ba5fd55 100644 --- a/db/methods.go +++ b/db/methods.go @@ -14,15 +14,36 @@ func (d *DB) ChatExists(ctx context.Context, chat Chat) bool { return d.EntryExists(ctx, "chats", filter) } +func (d *DB) GetChat(ctx context.Context, chatId int64) (Chat, error) { + filter := bson.D{primitive.E{Key: "id", Value: chatId}} + var result Chat + err := d.Database.Collection("chats").FindOne(ctx, filter).Decode(&result) + if err != nil { + Log("get chat error", err) + return Chat{}, err + } + return result, nil +} + 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) + Log("new chat error", err) return err } log.Printf("New chat added: %s (%d)\n", chat.Title, chat.Id) return nil } + +func (d *DB) UpdateChat(ctx context.Context, chat Chat, updates bson.D) error { + filter := bson.D{primitive.E{Key: "id", Value: chat.Id}} + result, err := d.Database.Collection("chats").UpdateOne(ctx, filter, updates) + if err != nil { + Log("chat update error", err) + } + log.Printf("Chat updated: %v", result) + return err +} \ No newline at end of file diff --git a/db/structs.go b/db/structs.go index e7796b8..683728c 100644 --- a/db/structs.go +++ b/db/structs.go @@ -1,8 +1,9 @@ package db type Chat struct { - Id int64 - Title string + Id int64 + Title string + TopicId int64 `bson:"topic_id"` } type User struct { diff --git a/go.mod b/go.mod index dc76b58..adde6de 100644 --- a/go.mod +++ b/go.mod @@ -1,25 +1,29 @@ module kickerbot -go 1.17 +go 1.19 require ( + github.com/NicoNex/echotron/v3 v3.21.0 github.com/fogleman/gg v1.3.0 - github.com/go-co-op/gocron v1.11.0 - github.com/go-stack/stack v1.8.0 // indirect - github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect - github.com/golang/snappy v0.0.1 // indirect + github.com/go-co-op/gocron v1.18.0 github.com/joho/godotenv v1.4.0 - github.com/klauspost/compress v1.13.6 // indirect + go.mongodb.org/mongo-driver v1.11.0 +) + +require ( + github.com/go-stack/stack v1.8.1 // indirect + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect + github.com/golang/snappy v0.0.4 // indirect + github.com/klauspost/compress v1.15.12 // indirect + github.com/montanaflynn/stats v0.6.6 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/robfig/cron/v3 v3.0.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 - golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f // indirect - golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect - golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect - golang.org/x/text v0.3.6 // indirect - gopkg.in/tucnak/telebot.v3 v3.0.0-20211126232936-7f936709f3ee + github.com/xdg-go/scram v1.1.1 // indirect + github.com/xdg-go/stringprep v1.0.3 // indirect + github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect + golang.org/x/crypto v0.1.0 // indirect + golang.org/x/image v0.1.0 // indirect + golang.org/x/sync v0.1.0 // indirect + golang.org/x/text v0.4.0 // indirect ) diff --git a/go.sum b/go.sum index e8680a1..2dc05d3 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/NicoNex/echotron/v3 v3.21.0 h1:6YDSYA/AGlPxWoUdOIRrBvUZ0RhRa2W4taS4eSrbdXw= +github.com/NicoNex/echotron/v3 v3.21.0/go.mod h1:LpP5IyHw0y+DZUZMBgXEDAF9O8feXrQu7w7nlJzzoZI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= @@ -6,22 +8,30 @@ github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/go-co-op/gocron v1.11.0 h1:ujOMubCpGcTxnnR/9vJIPIEpgwuAjbueAYqJRNr+nHg= github.com/go-co-op/gocron v1.11.0/go.mod h1:qtlsoMpHlSdIZ3E/xuZzrrAbeX3u5JtPvWf2TcdutU0= +github.com/go-co-op/gocron v1.18.0 h1:SxTyJ5xnSN4byCq7b10LmmszFdxQlSQJod8s3gbnXxA= +github.com/go-co-op/gocron v1.18.0/go.mod h1:sD/a0Aadtw5CpflUJ/lpP9Vfdk979Wl1Sg33HPHg0FY= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 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/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= +github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= github.com/goccy/go-yaml v1.9.1/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= +github.com/golang/snappy v0.0.4/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/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM= +github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM= 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= @@ -30,6 +40,8 @@ github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope 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/montanaflynn/stats v0.6.6 h1:Duep6KMIDpY4Yo11iFsvyqJDyfzLF9+sndUKT+v64GQ= +github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= 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= @@ -48,40 +60,78 @@ 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/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= +github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= 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/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs= +github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= 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= +github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= +github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 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= +go.mongodb.org/mongo-driver v1.11.0 h1:FZKhBSTydeuffHj9CBjXlR8vQLee1cQyTWYPA6/tqiE= +go.mongodb.org/mongo-driver v1.11.0/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 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/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= +golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ= golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/image v0.1.0 h1:r8Oj8ZA2Xy12/b5KZYj3tuv7NG/fBz3TwQVvpJ9l8Rk= +golang.org/x/image v0.1.0/go.mod h1:iyPr49SD/G/TBxYVB/9RRtGUT5eNbo2u4NamWeQcD5c= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 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/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 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/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 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.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 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/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 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/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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= @@ -92,3 +142,4 @@ gopkg.in/tucnak/telebot.v3 v3.0.0-20211126232936-7f936709f3ee h1:0jS8G549Rie2L+B 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= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/kicker/handlers.go b/kicker/handlers.go index 6852e6e..fdde31a 100644 --- a/kicker/handlers.go +++ b/kicker/handlers.go @@ -9,19 +9,23 @@ import ( "strconv" "time" - tb "gopkg.in/tucnak/telebot.v3" + tb "github.com/NicoNex/echotron/v3" + "go.mongodb.org/mongo-driver/bson" ) -func userJoined(c tb.Context) error { - bot := c.Bot() +func userJoined(b *bot, update *tb.Update) error { captcha := captchagen.GenCaptcha() - reader := captcha.ToReader() - message := c.Message() + bytes, err := captcha.ToBytes() + if err != nil { + fmt.Printf("Error creating captcha bytes: %v", bytes) + b.SendMessage("Не могу создать капчу, @nefrace, проверь логи.", update.Message.From.ID, &tb.MessageOptions{MessageThreadID: update.Message.ThreadID}) + } + message := update.Message user := db.User{ - Id: message.Sender.ID, - Username: message.Sender.Username, - FirstName: message.Sender.FirstName, - LastName: message.Sender.LastName, + Id: message.From.ID, + Username: message.From.Username, + FirstName: message.From.FirstName, + LastName: message.From.LastName, IsBanned: false, ChatId: message.Chat.ID, JoinedMessage: message.ID, @@ -33,41 +37,55 @@ func userJoined(c tb.Context) error { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() log.Print(user) - msg := fmt.Sprintf("Приветствую тебя, %v!\nДля подтверждения, что ты человек, выбери логотип движка, которому посвящен данный чат, и отправь его номер сюда.\nЯ дам тебе две минуты на это.", user.FirstName) - photo := tb.Photo{File: tb.FromReader(reader), Caption: msg} - result, err := bot.Send(tb.ChatID(message.Chat.ID), &photo, &tb.SendOptions{ReplyTo: message}) + msg := fmt.Sprintf("Приветствую тебя, *[%s](tg://user?id=%d)*\\!\nДля подтверждения, что ты человек, выбери логотип движка, которому посвящен данный чат, и отправь его номер сюда\\.\n*_Я дам тебе две минуты на это\\._*", EscapeText(tb.MarkdownV2, user.FirstName), user.Id) + options := tb.PhotoOptions{ + Caption: msg, + ParseMode: tb.MarkdownV2, + } + if message.Chat.IsForum { + options.MessageThreadID = int(b.CaptchaTopic) + } + result, err := b.SendPhoto(tb.NewInputFileBytes("logos.png", *bytes), message.Chat.ID, &options) if err != nil { return err } - user.CaptchaMessage = result.ID + user.CaptchaMessage = result.Result.ID d.NewUser(ctx, user) return nil } -func userLeft(c tb.Context) error { - bot := c.Bot() - message := c.Message() - sender := c.Sender() +func userLeft(b *bot, update *tb.Update) error { + message := update.Message + sender := message.From d := db.GetDatabase() ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() if user, err := d.GetUser(ctx, db.User{Id: sender.ID, ChatId: message.Chat.ID}); err == nil { d.RemoveUser(ctx, user) - bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage}) - bot.Delete(&tb.Message{Chat: message.Chat, ID: user.JoinedMessage}) + b.DeleteMessage(message.Chat.ID, message.ID) + b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) } return nil } -func checkCaptcha(c tb.Context) error { - sender := c.Sender() - message := c.Message() - bot := c.Bot() +func checkCaptcha(b *bot, update *tb.Update) error { + message := update.Message + sender := message.ForwardFrom d := db.GetDatabase() ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() if user, err := d.GetUser(ctx, db.User{Id: sender.ID, ChatId: message.Chat.ID}); err == nil { + if message.Chat.IsForum { + chat, err := d.GetChat(ctx, message.Chat.ID) + if err != nil { + return err + } + if message.ThreadID != int(chat.TopicId) { + b.DeleteMessage(message.Chat.ID, message.ID) + return nil + } + } text_runes := []rune(message.Text) guess := string(text_runes[0]) solved := false @@ -75,28 +93,30 @@ func checkCaptcha(c tb.Context) error { if num == int(user.CorrectAnswer) { _ = d.RemoveUser(ctx, user) solved = true - bot.Delete(message) - bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage}) + b.DeleteMessage(message.Chat.ID, message.ID) + b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) } } else { - log.Print(err) + log.Println(err) + return err } if !solved { - bot.Delete(message) - bot.Delete(&tb.Message{Chat: message.Chat, ID: user.CaptchaMessage}) - bot.Delete(&tb.Message{Chat: message.Chat, ID: user.JoinedMessage}) - bot.Ban(message.Chat, &tb.ChatMember{User: sender}) + b.DeleteMessage(message.Chat.ID, message.ID) + b.DeleteMessage(message.Chat.ID, user.CaptchaMessage) + b.DeleteMessage(message.Chat.ID, user.JoinedMessage) + b.BanChatMember(message.Chat.ID, sender.ID, nil) _ = d.RemoveUser(ctx, user) } } return nil } -func botAdded(c tb.Context) error { - m := c.Message() +func botAdded(b *bot, update *tb.Update) error { + m := update.Message chat := db.Chat{ - Id: m.Chat.ID, - Title: m.Chat.Title, + Id: m.Chat.ID, + Title: m.Chat.Title, + TopicId: 0, } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -108,22 +128,22 @@ func botAdded(c tb.Context) error { return nil } -var HandlersV1 = []Handler{ - { - Endpoint: tb.OnText, - Handler: checkCaptcha, - }, - { - Endpoint: tb.OnAddedToGroup, - Handler: botAdded, - }, - - { - Endpoint: tb.OnUserJoined, - Handler: userJoined, - }, - { - Endpoint: tb.OnUserLeft, - Handler: userLeft, - }, +func setTopic(b *bot, update *tb.Update) error { + m := update.Message + d := db.GetDatabase() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + chat, err := d.GetChat(ctx, m.Chat.ID) + if err != nil { + return err + } + upd := bson.D{{Key: "$set", Value: bson.D{{Key: "topic_id", Value: m.ThreadID}}}} + b.CaptchaTopic = int64(m.ThreadID) + err = d.UpdateChat(ctx, chat, upd) + if err != nil { + return err + } + b.DeleteMessage(m.Chat.ID, m.ID) + b.SendMessage("Данный топик выбран в качестве проверочного для пользователей", m.Chat.ID, &tb.MessageOptions{MessageThreadID: m.ThreadID}) + return nil } diff --git a/kicker/kicker.go b/kicker/kicker.go index 466c551..0c554f0 100644 --- a/kicker/kicker.go +++ b/kicker/kicker.go @@ -1,45 +1,111 @@ package kicker import ( - "errors" + "context" + "kickerbot/db" "log" + "strings" "time" - tb "gopkg.in/tucnak/telebot.v3" + tb "github.com/NicoNex/echotron/v3" ) -type Handler struct { - Endpoint interface{} - Handler tb.HandlerFunc +type bot struct { + chatID int64 + CaptchaTopic int64 + Me *tb.User + tb.API +} + +func (b *bot) Update(update *tb.Update) { + if len(update.Message.NewChatMembers) != 0 { + for _, user := range update.Message.NewChatMembers { + if user.ID == b.Me.ID { + botAdded(b, update) + } + } + userJoined(b, update) + return + } + if update.Message.LeftChatMember != nil { + userLeft(b, update) + return + } + if update.Message.Text != "" { + if update.Message.Text == "/settopic" { + setTopic(b, update) + return + } + checkCaptcha(b, update) + + } } // Базовая структура для бота type Kicker struct { - Bot *tb.Bot - Token string + Token string + Dispatcher *tb.Dispatcher +} + +func (b *Kicker) NewBot(chatID int64) tb.Bot { + d := db.GetDatabase() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + chat := db.Chat{ + Id: chatID, + Title: "", + TopicId: 0, + } + if !d.ChatExists(ctx, chat) { + if err := d.NewChat(ctx, chat); err != nil { + return &bot{} + } + } + chat, _ = d.GetChat(ctx, chatID) + CaptchaTopic := chat.TopicId + result := &bot{ + chatID, + CaptchaTopic, + nil, + tb.NewAPI(b.Token), + } + me, err := result.GetMe() + if err != nil { + log.Println(err) + } + result.Me = me.Result + + return result } // Initialize bot with token func (b *Kicker) Init() error { - bot, err := tb.NewBot(tb.Settings{ - Token: b.Token, - Poller: &tb.LongPoller{Timeout: 10 * time.Second}, - }) - if err != nil { - log.Print(err) - return err - } - b.Bot = bot + dsp := tb.NewDispatcher(b.Token, b.NewBot) + b.Dispatcher = dsp return nil } -// Add handler methods to the bot -func (b *Kicker) AddHandlers(handlers []Handler) error { - if len(handlers) != 0 { - for i := range handlers { - b.Bot.Handle(handlers[i].Endpoint, handlers[i].Handler) - } - return nil - } - return errors.New("no handlers are declared") +func (b *Kicker) Start() error { + return b.Dispatcher.Poll() +} + +func EscapeText(parseMode tb.ParseMode, text string) string { + var replacer *strings.Replacer + + if parseMode == tb.HTML { + replacer = strings.NewReplacer("<", "<", ">", ">", "&", "&") + } else if parseMode == tb.Markdown { + replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[") + } else if parseMode == tb.MarkdownV2 { + replacer = strings.NewReplacer( + "_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", + "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", + "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", + "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!", + ) + } else { + return "" + } + + return replacer.Replace(text) } diff --git a/kicker/tasks.go b/kicker/tasks.go index db7926d..4cd19cc 100644 --- a/kicker/tasks.go +++ b/kicker/tasks.go @@ -6,12 +6,17 @@ import ( "log" "time" + tb "github.com/NicoNex/echotron/v3" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" - tb "gopkg.in/tucnak/telebot.v3" ) -func TaskKickOldUsers(b tb.Bot) { +type TaskBot struct { + Token string + tb.API +} + +func TaskKickOldUsers(b *tb.API) { d := db.GetDatabase() log.Print("STARTING KICKING TASK") ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) @@ -26,14 +31,9 @@ func TaskKickOldUsers(b tb.Bot) { log.Printf("Error in deleting task: %v", err) } for _, user := range users { - chat := tb.Chat{ID: user.ChatId} - tbUser := tb.User{ID: user.Id} - member := tb.ChatMember{User: &tbUser} - message := tb.Message{Chat: &chat, ID: user.CaptchaMessage} - joinMessage := tb.Message{Chat: &chat, ID: user.JoinedMessage} - b.Ban(&chat, &member) - b.Delete(&message) - b.Delete(&joinMessage) + b.BanChatMember(user.ChatId, user.Id, &tb.BanOptions{RevokeMessages: true}) + b.DeleteMessage(user.ChatId, user.CaptchaMessage) + b.DeleteMessage(user.ChatId, user.JoinedMessage) d.RemoveUser(ctx, user) } } diff --git a/main.go b/main.go index 197a45d..6e01275 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "os" "time" + "github.com/NicoNex/echotron/v3" "github.com/go-co-op/gocron" "github.com/joho/godotenv" ) @@ -29,9 +30,9 @@ func main() { Bot := kicker.Kicker{Token: token} Bot.Init() - Bot.AddHandlers(kicker.HandlersV1) scheduler := gocron.NewScheduler(time.UTC) - scheduler.Every(30).Seconds().Do(func() { kicker.TaskKickOldUsers(*Bot.Bot) }) + tasker := echotron.NewAPI(token) + scheduler.Every(30).Seconds().Do(func() { kicker.TaskKickOldUsers(&tasker) }) scheduler.StartAsync() - Bot.Bot.Start() + Bot.Start() }