@@ -48,13 +48,8 @@ export class PersistentTimer {
48
48
tournamentId ?: string
49
49
) : Promise < PersistentTimer > {
50
50
// TODO: check for end <= now
51
- const endMilli = end . getTime ( ) ;
52
- const nowMilli = Date . now ( ) ;
53
- const left = this . formatTime ( endMilli - nowMilli ) ;
54
- const messageId = await discord . sendMessage (
55
- channelId ,
56
- `Time left in the round: \`${ left } \`. Ends ${ time ( end ) } (${ time ( end , "R" ) } ).`
57
- ) ;
51
+ const message = PersistentTimer . timerMessage ( end ) ;
52
+ const messageId = await discord . sendMessage ( channelId , message ) ;
58
53
59
54
const entity = new Countdown ( ) ;
60
55
entity . end = end ;
@@ -141,15 +136,13 @@ export class PersistentTimer {
141
136
}
142
137
const secondsRemaining = Math . ceil ( ( now . getTime ( ) - end . getTime ( ) ) / 1000 ) ;
143
138
logger . verbose ( `tick: ${ this . entity . id } now(${ iso } ) secondsRemaining(${ secondsRemaining } )` ) ;
144
- if ( secondsRemaining % this . entity . cronIntervalSeconds === 0 ) {
145
- const left = PersistentTimer . formatTime ( end . getTime ( ) - Date . now ( ) ) ;
146
- logger . verbose ( `tick: ${ this . entity . id } now(${ iso } ) left(${ left } )` ) ;
139
+ // Tick every minute if more than five minutes remain. Within five minutes, tick every five seconds.
140
+ // This is due to Discord rate limits.
141
+ if ( secondsRemaining % ( secondsRemaining > 300 ? 60 : this . entity . cronIntervalSeconds ) === 0 ) {
142
+ const message = PersistentTimer . timerMessage ( end ) ;
143
+ logger . verbose ( `tick: ${ this . entity . id } now(${ iso } ) left(${ message } )` ) ;
147
144
try {
148
- await this . discord . editMessage (
149
- this . entity . channelId ,
150
- this . entity . messageId ,
151
- `Time left in the round: \`${ left } \`. Ends ${ time ( end ) } (${ time ( end , "R" ) } ).`
152
- ) ;
145
+ await this . discord . editMessage ( this . entity . channelId , this . entity . messageId , message ) ;
153
146
logger . verbose ( `tick: ${ this . entity . id } now(${ iso } ) edited` ) ;
154
147
} catch ( error ) {
155
148
logger . warn ( `tick: could not edit ${ this . entity . channelId } ${ this . entity . messageId } ` ) ;
@@ -175,4 +168,11 @@ export class PersistentTimer {
175
168
return `${ hours } :${ minutes . toString ( ) . padStart ( 2 , "0" ) } :${ seconds . toString ( ) . padStart ( 2 , "0" ) } ` ;
176
169
}
177
170
}
171
+
172
+ public static timerMessage ( end : Date ) : string {
173
+ const delta = end . getTime ( ) - Date . now ( ) ;
174
+ const left = PersistentTimer . formatTime ( delta ) ;
175
+ const accuracy = delta > 300000 ? "1 minute" : "5 seconds" ;
176
+ return `Time left in the round: \`${ left } \` (accuracy ${ accuracy } ). Ends ${ time ( end ) } (${ time ( end , "R" ) } ).` ;
177
+ }
178
178
}
0 commit comments