Skip to content

Commit a266a99

Browse files
committed
优化每日限额机制
若转换/下载不成功,则不消耗次数
1 parent 8e54427 commit a266a99

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

db/usageLimit.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package db
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/go-redis/redis/v8"
67
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@@ -9,7 +10,7 @@ import (
910
"time"
1011
)
1112

12-
// CheckLimit 用户是否已达到今日限额
13+
// CheckLimit Determines if the user has reached today's limit
1314
func CheckLimit(update *tgbotapi.Update) bool {
1415
UID := int64(0)
1516
if update.Message != nil {
@@ -32,10 +33,31 @@ func CheckLimit(update *tgbotapi.Update) bool {
3233
if limitTimes > config.Get().General.UserDailyLimit {
3334
return true
3435
}
35-
rdb.Set(ctx, fmt.Sprintf("%s:UserLimit:%d", ServicePrefix, UID), limitTimes+1, redis.KeepTTL)
3636
return false
3737
}
3838

39+
// ConsumeLimit Consume the current user's daily limit
40+
func ConsumeLimit(update *tgbotapi.Update) error {
41+
UID := int64(0)
42+
if update.Message != nil {
43+
UID = update.Message.Chat.ID
44+
} else if update.CallbackQuery != nil {
45+
UID = update.CallbackQuery.Message.Chat.ID
46+
} else {
47+
return errors.New("failed to get uid")
48+
}
49+
50+
limit := rdb.Get(ctx, fmt.Sprintf("%s:UserLimit:%d", ServicePrefix, UID)).Val()
51+
if limit == "" {
52+
rdb.Set(ctx, fmt.Sprintf("%s:UserLimit:%d", ServicePrefix, UID), 1, 24*time.Hour)
53+
return nil
54+
}
55+
56+
limitTimes, _ := strconv.Atoi(limit)
57+
rdb.Set(ctx, fmt.Sprintf("%s:UserLimit:%d", ServicePrefix, UID), limitTimes+1, redis.KeepTTL)
58+
return nil
59+
}
60+
3961
// 获取该用户已使用的次数
4062
func getUsed(UID int64) int {
4163
limit := rdb.Get(ctx, fmt.Sprintf("%s:UserLimit:%d", ServicePrefix, UID)).Val()

handler/AnimationMessage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
77
"github.com/rroy233/StickerDownloader/config"
8+
"github.com/rroy233/StickerDownloader/db"
89
"github.com/rroy233/StickerDownloader/languages"
910
"github.com/rroy233/StickerDownloader/utils"
1011
"gopkg.in/rroy233/logger.v2"
@@ -89,6 +90,11 @@ func AnimationMessage(update tgbotapi.Update) {
8990
return
9091
}
9192

93+
//Consume the current user's daily limit
94+
if err = db.ConsumeLimit(&update); err != nil {
95+
logger.Error.Println(userInfo + err.Error())
96+
}
97+
9298
utils.EditMsgText(update.Message.Chat.ID, msg.MessageID, languages.Get(&update).BotMsg.ConvertCompleted)
9399
if err != nil {
94100
logger.Error.Println(userInfo+"failed to delete msg:", err)

handler/DownloadStickerSetQuery.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ func DownloadStickerSetQuery(update tgbotapi.Update) {
214214
logger.Info.Println(userInfo + "DownloadStickerSetQuery-upload(Telegram) successfully!!!")
215215
}
216216

217+
//Consume the current user's daily limit
218+
if err = db.ConsumeLimit(&update); err != nil {
219+
logger.Error.Println(userInfo + "DownloadStickerSetQuery - " + err.Error())
220+
}
221+
217222
return
218223
}
219224

handler/StickersMessage.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ func StickerMessage(update tgbotapi.Update) {
125125
utils.RemoveFile(outPath)
126126
}
127127

128+
//Consume the current user's daily limit
129+
if err = db.ConsumeLimit(&update); err != nil {
130+
logger.Error.Println(userInfo + err.Error())
131+
}
132+
128133
err = utils.BotRequest(tgbotapi.NewEditMessageTextAndMarkup(update.Message.Chat.ID, msg.MessageID, languages.Get(&update).BotMsg.ConvertCompleted, tgbotapi.NewInlineKeyboardMarkup(
129134
tgbotapi.NewInlineKeyboardRow(tgbotapi.NewInlineKeyboardButtonData(languages.Get(&update).BotMsg.DownloadStickerSet, DownloadStickerSetCallbackQuery)),
130135
)))

0 commit comments

Comments
 (0)