Skip to content

Commit

Permalink
Refactor bearweek command
Browse files Browse the repository at this point in the history
  • Loading branch information
ttbowen committed Nov 27, 2023
1 parent a3277e4 commit a9983a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
37 changes: 27 additions & 10 deletions packages/mrwhale-commands/src/commands/useful/bearweek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,43 @@ export const data: CommandOptions = {
};

const AUGUST = 8;
const BEAR_WEEK_START_DAY = 11;
const BEAR_WEEK_END_DAY = 18;

export function action(): string {
const now = new Date();
const currentMonth = now.getMonth() + 1;

const currentDay = now.getDate();
let nextBearWeekYear = now.getFullYear();
if (now >= new Date(`${nextBearWeekYear}-08-19T00:00:00.000Z`)) {
nextBearWeekYear++;
}

const nextBearWeekDate = `${nextBearWeekYear}-08-11T00:00:00.000Z`;
const bearWeek = new Date(nextBearWeekDate);

const nextBearWeekYear = getBearWeekYear(now);
const nextBearWeekDate = getBearWeekDate(nextBearWeekYear);

const ms = Math.floor(bearWeek.valueOf() - now.valueOf());
const ms = Math.floor(nextBearWeekDate.valueOf() - now.valueOf());
const time = TimeUtilities.convertMs(ms);

if (currentMonth === AUGUST && currentDay <= 18 && currentDay >= 11) {
if (
currentMonth === AUGUST &&
currentDay >= BEAR_WEEK_START_DAY &&
currentDay <= BEAR_WEEK_END_DAY
) {
return `It is Bear week! 🐻 Visit https://mudgolt.com/`;
}

return `${time.toString()} until bear week! 🐻`;
}

export function getBearWeekYear(now: Date): number {
const nextBearWeekYear = now.getFullYear();
const nextBearWeekDate = getBearWeekDate(nextBearWeekYear);
if (now >= nextBearWeekDate) {
return nextBearWeekYear + 1;
}

return nextBearWeekYear;
}

function getBearWeekDate(bearWeekYear: number): Date {
return new Date(
`${bearWeekYear}-0${AUGUST}-${BEAR_WEEK_START_DAY}T00:00:00.000Z`
);
}
5 changes: 1 addition & 4 deletions packages/mrwhale-discord/src/commands/useful/bearweek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ export default class extends DiscordCommand {
interaction: ChatInputCommandInteraction | Message
): Promise<Message<boolean>> | Promise<InteractionResponse<boolean>> {
const now = new Date();
let nextBearWeekYear = now.getFullYear();
if (now >= new Date(`${nextBearWeekYear}-08-19T00:00:00.000Z`)) {
nextBearWeekYear++;
}
const nextBearWeekYear = bearweek.getBearWeekYear(now);

const embed = new EmbedBuilder()
.setTitle(`Bear week ${nextBearWeekYear} countdown!`)
Expand Down

0 comments on commit a9983a2

Please sign in to comment.