mirror of
https://github.com/ngoduykhanh/wireguard-ui
synced 2024-06-10 18:02:26 +02:00
Fixed Bot becoming nil; send flood notification message only once
This commit is contained in:
parent
c05eaf1744
commit
4c5cb94729
|
@ -26,16 +26,19 @@ var (
|
||||||
Bot *echotron.API
|
Bot *echotron.API
|
||||||
BotMutex sync.RWMutex
|
BotMutex sync.RWMutex
|
||||||
|
|
||||||
floodWait = make(map[int64]int64, 0)
|
floodWait = make(map[int64]int64, 0)
|
||||||
|
floodMessageSent = make(map[int64]struct{}, 0)
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(initDeps TgBotInitDependencies) (err error) {
|
func Start(initDeps TgBotInitDependencies) (err error) {
|
||||||
ticker := time.NewTicker(time.Minute)
|
ticker := time.NewTicker(time.Minute)
|
||||||
defer func() {
|
defer func() {
|
||||||
BotMutex.Lock()
|
if err != nil {
|
||||||
Bot = nil
|
BotMutex.Lock()
|
||||||
BotMutex.Unlock()
|
Bot = nil
|
||||||
ticker.Stop()
|
BotMutex.Unlock()
|
||||||
|
ticker.Stop()
|
||||||
|
}
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
err = fmt.Errorf("[PANIC] recovered from panic: %v", r)
|
err = fmt.Errorf("[PANIC] recovered from panic: %v", r)
|
||||||
}
|
}
|
||||||
|
@ -77,6 +80,10 @@ func Start(initDeps TgBotInitDependencies) (err error) {
|
||||||
if update.Message != nil {
|
if update.Message != nil {
|
||||||
userid := update.Message.Chat.ID
|
userid := update.Message.Chat.ID
|
||||||
if _, wait := floodWait[userid]; wait {
|
if _, wait := floodWait[userid]; wait {
|
||||||
|
if _, notified := floodMessageSent[userid]; notified {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
floodMessageSent[userid] = struct{}{}
|
||||||
bot.SendMessage(
|
bot.SendMessage(
|
||||||
fmt.Sprintf("You can only request your configs once per %d minutes", FloodWait),
|
fmt.Sprintf("You can only request your configs once per %d minutes", FloodWait),
|
||||||
userid,
|
userid,
|
||||||
|
@ -142,6 +149,7 @@ func updateFloodWait() {
|
||||||
for userid, ts := range floodWait {
|
for userid, ts := range floodWait {
|
||||||
if ts < thresholdTS {
|
if ts < thresholdTS {
|
||||||
delete(floodWait, userid)
|
delete(floodWait, userid)
|
||||||
|
delete(floodMessageSent, userid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue