Skip to content

Commit 44c8f06

Browse files
committed
Tick every minute if more than five minutes remain
#647
1 parent ed1ac41 commit 44c8f06

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ const logger = getLogger("index");
4444
editMessage: async (channelId, messageId, newMessage) => {
4545
const channel = await bot.channels.fetch(channelId);
4646
if (channel?.isTextBased()) {
47-
const sent = await channel.messages.fetch(messageId);
48-
await sent.edit(newMessage);
47+
await channel.messages.edit(messageId, newMessage);
4948
} else {
5049
throw new Error(`${channelId} is not a text channel`);
5150
}

src/timer/PersistentTimer.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,8 @@ export class PersistentTimer {
4848
tournamentId?: string
4949
): Promise<PersistentTimer> {
5050
// 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);
5853

5954
const entity = new Countdown();
6055
entity.end = end;
@@ -141,15 +136,13 @@ export class PersistentTimer {
141136
}
142137
const secondsRemaining = Math.ceil((now.getTime() - end.getTime()) / 1000);
143138
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})`);
147144
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);
153146
logger.verbose(`tick: ${this.entity.id} now(${iso}) edited`);
154147
} catch (error) {
155148
logger.warn(`tick: could not edit ${this.entity.channelId} ${this.entity.messageId}`);
@@ -175,4 +168,11 @@ export class PersistentTimer {
175168
return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
176169
}
177170
}
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+
}
178178
}

0 commit comments

Comments
 (0)