Skip to content

Commit

Permalink
Tick every minute if more than five minutes remain
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlul committed Dec 5, 2024
1 parent ed1ac41 commit 44c8f06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ const logger = getLogger("index");
editMessage: async (channelId, messageId, newMessage) => {
const channel = await bot.channels.fetch(channelId);
if (channel?.isTextBased()) {
const sent = await channel.messages.fetch(messageId);
await sent.edit(newMessage);
await channel.messages.edit(messageId, newMessage);
} else {
throw new Error(`${channelId} is not a text channel`);
}
Expand Down
30 changes: 15 additions & 15 deletions src/timer/PersistentTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ export class PersistentTimer {
tournamentId?: string
): Promise<PersistentTimer> {
// TODO: check for end <= now
const endMilli = end.getTime();
const nowMilli = Date.now();
const left = this.formatTime(endMilli - nowMilli);
const messageId = await discord.sendMessage(
channelId,
`Time left in the round: \`${left}\`. Ends ${time(end)} (${time(end, "R")}).`
);
const message = PersistentTimer.timerMessage(end);
const messageId = await discord.sendMessage(channelId, message);

const entity = new Countdown();
entity.end = end;
Expand Down Expand Up @@ -141,15 +136,13 @@ export class PersistentTimer {
}
const secondsRemaining = Math.ceil((now.getTime() - end.getTime()) / 1000);
logger.verbose(`tick: ${this.entity.id} now(${iso}) secondsRemaining(${secondsRemaining})`);
if (secondsRemaining % this.entity.cronIntervalSeconds === 0) {
const left = PersistentTimer.formatTime(end.getTime() - Date.now());
logger.verbose(`tick: ${this.entity.id} now(${iso}) left(${left})`);
// Tick every minute if more than five minutes remain. Within five minutes, tick every five seconds.
// This is due to Discord rate limits.
if (secondsRemaining % (secondsRemaining > 300 ? 60 : this.entity.cronIntervalSeconds) === 0) {
const message = PersistentTimer.timerMessage(end);
logger.verbose(`tick: ${this.entity.id} now(${iso}) left(${message})`);
try {
await this.discord.editMessage(
this.entity.channelId,
this.entity.messageId,
`Time left in the round: \`${left}\`. Ends ${time(end)} (${time(end, "R")}).`
);
await this.discord.editMessage(this.entity.channelId, this.entity.messageId, message);
logger.verbose(`tick: ${this.entity.id} now(${iso}) edited`);
} catch (error) {
logger.warn(`tick: could not edit ${this.entity.channelId} ${this.entity.messageId}`);
Expand All @@ -175,4 +168,11 @@ export class PersistentTimer {
return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}
}

public static timerMessage(end: Date): string {
const delta = end.getTime() - Date.now();
const left = PersistentTimer.formatTime(delta);
const accuracy = delta > 300000 ? "1 minute" : "5 seconds";
return `Time left in the round: \`${left}\` (accuracy ${accuracy}). Ends ${time(end)} (${time(end, "R")}).`;
}
}

0 comments on commit 44c8f06

Please sign in to comment.