From efe02a69349115e2bf356a9934c05edae1475201 Mon Sep 17 00:00:00 2001 From: Maksim Pischulenok Date: Wed, 14 Jun 2023 00:01:16 +0300 Subject: [PATCH] Add automatic opt-in --- go.mod | 9 ++++++--- main.go | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 050bb03..dca41e2 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,14 @@ module pischule/mention-all-bot go 1.20 +require ( + gopkg.in/telebot.v3 v3.1.3 + gorm.io/driver/sqlite v1.5.2 + gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 +) + require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-sqlite3 v1.14.17 // indirect - gopkg.in/telebot.v3 v3.1.3 // indirect - gorm.io/driver/sqlite v1.5.2 // indirect - gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 // indirect ) diff --git a/main.go b/main.go index e0d6b57..9c2c36c 100644 --- a/main.go +++ b/main.go @@ -135,9 +135,23 @@ func handleStats(c tele.Context) error { } func handleUserLeft(c tele.Context) error { - u := ChatUser{UserID: c.Message().UserLeft.ID, ChatID: c.Chat().ID} - log.Printf("user %d left chat %d", u.UserID, u.ChatID) - DB.Where(&u).Delete(&ChatUser{}) + cu := ChatUser{UserID: c.Message().UserLeft.ID, ChatID: c.Chat().ID} + log.Printf("user %d left chat %d", cu.UserID, cu.ChatID) + DB.Where(&cu).Delete(&ChatUser{}) + return nil +} + +func handleUserJoined(c tele.Context) error { + u := c.Message().UserJoined + if u.IsBot { + return nil + } + cu := ChatUser{UserID: u.ID, ChatID: c.Chat().ID, Username: extractUsername(u)} + log.Printf("user %d joined chat %d", cu.UserID, cu.ChatID) + DB.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "chat_id"}, {Name: "user_id"}}, + DoUpdates: clause.AssignmentColumns([]string{"username"}), + }).Create(&cu) return nil } @@ -151,5 +165,6 @@ func main() { b.Handle("/all", handleAll) b.Handle("/stats", handleStats) b.Handle(tele.OnUserLeft, handleUserLeft) + b.Handle(tele.OnUserJoined, handleUserJoined) b.Start() }