mirror of
https://github.com/pischule/go-mention-all-bot.git
synced 2025-12-19 06:56:43 +00:00
Add automatic opt-in
This commit is contained in:
9
go.mod
9
go.mod
@@ -2,11 +2,14 @@ module pischule/mention-all-bot
|
|||||||
|
|
||||||
go 1.20
|
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 (
|
require (
|
||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.14.17 // 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
|
|
||||||
)
|
)
|
||||||
|
|||||||
21
main.go
21
main.go
@@ -135,9 +135,23 @@ func handleStats(c tele.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleUserLeft(c tele.Context) error {
|
func handleUserLeft(c tele.Context) error {
|
||||||
u := ChatUser{UserID: c.Message().UserLeft.ID, ChatID: c.Chat().ID}
|
cu := ChatUser{UserID: c.Message().UserLeft.ID, ChatID: c.Chat().ID}
|
||||||
log.Printf("user %d left chat %d", u.UserID, u.ChatID)
|
log.Printf("user %d left chat %d", cu.UserID, cu.ChatID)
|
||||||
DB.Where(&u).Delete(&ChatUser{})
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,5 +165,6 @@ func main() {
|
|||||||
b.Handle("/all", handleAll)
|
b.Handle("/all", handleAll)
|
||||||
b.Handle("/stats", handleStats)
|
b.Handle("/stats", handleStats)
|
||||||
b.Handle(tele.OnUserLeft, handleUserLeft)
|
b.Handle(tele.OnUserLeft, handleUserLeft)
|
||||||
|
b.Handle(tele.OnUserJoined, handleUserJoined)
|
||||||
b.Start()
|
b.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user