Skip to content

Commit

Permalink
Add custom ignore feature for users
Browse files Browse the repository at this point in the history
Signed-off-by: aliwoto <aminnimaj@gmail.com>
  • Loading branch information
ALiwoto committed Nov 9, 2021
1 parent 4723b4e commit febf518
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
9 changes: 8 additions & 1 deletion ratelimiter/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ func (l *Limiter) limiterHandler(b *gotgbot.Bot, ctx *ext.Context) error {
return ext.EndGroups
}

status.Last = time.Now()
l.mutex.Unlock()
status.Last = time.Now()

if status.IsCustomLimited() {
if !status.custom.ignoreException && l.isException(ctx.Message) {
return ext.ContinueGroups
}
return ext.EndGroups
}

return ext.ContinueGroups
}
47 changes: 46 additions & 1 deletion ratelimiter/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (l *Limiter) SetPunishmentDuration(d time.Duration) {
}

// SetMaxMessageCount sets the possible messages count in the
// antifloodwait amout of time (which is `l.timeout`).
// antifloodwait amount of time (which is `l.timeout`).
// in that period of time, chat (or user) needs to send less than
// this much message, otherwise they will be limited by this limiter
// and so as a result of that their messages will be ignored by the bot.
Expand All @@ -281,6 +281,38 @@ func (l *Limiter) SetMaxCacheDuration(d time.Duration) {
}
}

func (l *Limiter) AddCustomIgnore(id int64, d time.Duration, ignoreExceptions bool) {
l.mutex.Lock()
status := l.userMap[id]
if status == nil {
status = new(UserStatus)
status.custom = &customIgnore{
startTime: time.Now(),
duration: d,
ignoreException: ignoreExceptions,
}
l.userMap[id] = status
l.mutex.Unlock()
return
}
l.mutex.Unlock()
status.custom = &customIgnore{
startTime: time.Now(),
duration: d,
ignoreException: ignoreExceptions,
}
}

func (l *Limiter) RemoveCustomIgnore(id int64) {
l.mutex.Lock()
status := l.userMap[id]
l.mutex.Unlock()
if status == nil {
return
}
status.custom = nil
}

// hasTextCondition will check if the message meets the message condition
// or not.
// basically if l.TextOnly is set to true, this method will check if
Expand Down Expand Up @@ -363,4 +395,17 @@ func (s *UserStatus) IsLimited() bool {
return s.limited
}

func (s *UserStatus) IsCustomLimited() bool {
if s.custom == nil {
return false
}

if time.Since(s.custom.startTime) > s.custom.duration {
s.custom = nil
return false
}

return true
}

//---------------------------------------------------------
16 changes: 12 additions & 4 deletions ratelimiter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ type UserStatus struct {
// count is the counts of the messages of the user received
// by limiter.
count int

custom *customIgnore
}

type customIgnore struct {
startTime time.Time
duration time.Duration
ignoreException bool
}

// Limiter is the main struct of this library.
type Limiter struct {
mutex *sync.Mutex
// IsEnable will be true if and only if the limiter is enabled
// and should check for the incomming messages.
// and should check for the incoming messages.
isEnabled bool

// IsStopped will be false when the limiter is stopped.
Expand Down Expand Up @@ -64,7 +72,7 @@ type Limiter struct {
// send `maxCount` messages per `timeout`.
timeout time.Duration

// maxTimeout is the maxmimum time out of clearing user status
// maxTimeout is the maximum time out of clearing user status
// cache in the memory.
maxTimeout time.Duration

Expand All @@ -74,7 +82,7 @@ type Limiter struct {
punishment time.Duration

// maxCount is the maximum number of messages we can accept from the
// user in `timeout` amout of time; if the user sends more than
// user in `timeout` amount of time; if the user sends more than
// this much message, it will be limited and so the bot will ignore
// their messages.
maxCount int
Expand All @@ -97,7 +105,7 @@ type Limiter struct {
// any messages to the bot until it's limit time is completely over.
// otherwise the limitation will remain on the user until it stops
// sending any messages to the bot.
// (A truely bad way of handling antofloodwait... we recommend not to
// (A truly bad way of handling antifloodwait... we recommend not to
// set this value to `true`, unless it's very very necessary).
IsStrict bool

Expand Down

0 comments on commit febf518

Please sign in to comment.