This commit is contained in:
cdle 2021-08-19 21:39:56 +08:00
parent 83af478508
commit 23ad661fc2
3 changed files with 29 additions and 21 deletions

View File

@ -58,7 +58,7 @@ var sendMessagee = func(msg string, msgs ...interface{}) {
tp := msgs[1].(string)
uid := msgs[2].(int)
gid := 0
if len(msgs) == 4 {
if len(msgs) >= 4 {
gid = msgs[3].(int)
}
switch tp {
@ -100,17 +100,15 @@ var handleMessage = func(msgs ...interface{}) interface{} {
if len(msgs) == 4 {
gid = msgs[3].(int)
}
if new, rt := NewActiveUser(tp, uid); new {
sendMessagee(rt, msgs...)
}
go NewActiveUser(tp, uid, msgs...)
switch msg {
case "status", "状态":
if !isAdmin(msgs...) {
return "你没有权限操作"
}
return Count()
case "sign", "打卡":
return "打卡成功,许愿币+1"
// case "sign", "打卡":
// return "打卡成功,许愿币+1"
case "qrcode", "扫码", "二维码", "scan":
url := fmt.Sprintf("http://127.0.0.1:%d/api/login/qrcode.png?tp=%s&uid=%d&gid=%d", web.BConfig.Listen.HTTPPort, tp, uid, gid)
rsp, err := httplib.Get(url).Response()

View File

@ -36,7 +36,7 @@ func initTgBot() {
b.SendAlbum(m.Sender, tb.Album{&tb.Photo{File: tb.FromReader(rt.(*http.Response).Body)}})
}
} else {
rt := handleMessage(m.Text, "tgg", m.Sender.ID, int(m.Chat.ID))
rt := handleMessage(m.Text, "tgg", m.Sender.ID, int(m.Chat.ID), m.Sender)
// fmt.Println(rt)
switch rt.(type) {
case string:

View File

@ -4,19 +4,32 @@ import (
"fmt"
"time"
tb "gopkg.in/tucnak/telebot.v2"
"gorm.io/gorm"
)
type User struct {
ID int
Number int
Number int `gorm:"unique"`
Class string
ActiveAt time.Time
Coin int
}
func NewActiveUser(class string, uid int) (bool, string) {
func NewActiveUser(class string, uid int, msgs ...interface{}) {
msg := ""
if class == "tgg" {
sender := msgs[4].(*tb.User)
last := ""
if sender.LastName != "" {
last = " " + sender.LastName
}
if sender.Username == "" {
msg = fmt.Sprintf(`@%s%s `, sender.FirstName, last)
} else {
msg = fmt.Sprintf(`@%s `, sender.Username)
}
class = "tg"
}
if class == "qqg" {
@ -26,10 +39,8 @@ func NewActiveUser(class string, uid int) (bool, string) {
var u User
var ntime = time.Now()
var first = false
msg := ""
total := []int{}
tx := db.Begin()
err := tx.Where("class = ? and number = ?", class, uid).First(&u).Error
err := db.Where("class = ? and number = ?", class, uid).First(&u).Error
if err != nil {
first = true
u = User{
@ -38,14 +49,13 @@ func NewActiveUser(class string, uid int) (bool, string) {
Coin: 1,
ActiveAt: ntime,
}
if err := tx.Create(&u).Error; err != nil {
tx.Rollback()
return true, err.Error()
if err := db.Create(&u).Error; err != nil {
return
}
} else {
if zero.After(u.ActiveAt) {
if zero.Unix() > u.ActiveAt.Unix() {
first = true
tx.Updates(map[string]interface{}{
db.Updates(map[string]interface{}{
"active_at": ntime,
"coin": gorm.Expr("coin+1"),
})
@ -53,9 +63,9 @@ func NewActiveUser(class string, uid int) (bool, string) {
}
}
if first {
tx.Model(User{}).Select("count(id) as total").Where("active_at > ?", zero).Pluck("total", &total)
msg = fmt.Sprintf("你是今天第%d个发言的用户奖励%d个心愿币心愿币余额%d。", total[0]+1, 1, u.Coin)
db.Model(User{}).Select("count(id) as total").Where("active_at > ?", zero).Pluck("total", &total)
msg += fmt.Sprintf("你是今天第%d个发言的用户奖励%d个许愿币许愿币余额%d。", total[0]+1, 1, u.Coin)
// fmt.Println(msg)
sendMessagee(msg, msgs...)
}
tx.Commit()
return first, msg
}