Skip to content

Commit

Permalink
fix marker update
Browse files Browse the repository at this point in the history
  • Loading branch information
kipkaev55 committed Aug 17, 2018
1 parent 4429e58 commit f6e3eec
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ func NewBot(token, prefix string, durations ...float64) *Bot {
// SendMessage sending message to client or chat
func (b Bot) SendMessage(chatID string, text ...string) (bool, error) {
mutexMarkers.Lock()
var marker, message string
var (
marker, message string
m time.Time
ok bool
)
if len(text) > 1 {
marker = text[0]
text = append(text[:0], text[1:]...)
}
canSend := true
if m, ok := markers[marker]; ok {
if m, ok = markers[marker]; ok {
if b.Duration > time.Now().Sub(m).Seconds() {
canSend = false
} else {
markers[marker] = time.Now()
}
} else {
markers[marker] = time.Now()
Expand Down Expand Up @@ -90,5 +96,5 @@ func (b Bot) SendMessage(chatID string, text ...string) (bool, error) {
defer resp.Body.Close()
return success, err
}
return false, errors.New("Sending messages too often")
return false, errors.New(fmt.Sprintf("Sending messages too often. Last message was %.2f seconds ago. Period sending is %.2f", time.Now().Sub(m).Seconds(), b.Duration))
}

0 comments on commit f6e3eec

Please sign in to comment.