Skip to content

Commit 3b2139a

Browse files
authored
Add minutesLeft as an alternative to period in competition starting/ending events (#289)
* Add `minutesLeft` as alternative to `period` * bump
1 parent 22cc1dc commit 3b2139a

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wise-old-man-bot",
3-
"version": "1.4.11",
3+
"version": "1.4.12",
44
"description": "A Discord bot for the Wise Old Man projects (https://github.com/wise-old-man/wise-old-man/)",
55
"author": "Psikoi",
66
"license": "ISC",

src/events/instances/CompetitionEnding.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ import {
1212
} from '../../utils';
1313
import { Event } from '../../utils/events';
1414

15-
interface CompetitionEndingData {
15+
type CompetitionEndingData = {
1616
groupId: number;
1717
competition: Competition;
18-
period: {
19-
hours?: number;
20-
minutes?: number;
21-
};
22-
}
18+
} & (
19+
| {
20+
period: { hours?: number; minutes?: number };
21+
}
22+
| {
23+
minutesLeft: number;
24+
}
25+
);
2326

2427
class CompetitionEnding implements Event {
2528
type: string;
@@ -35,7 +38,7 @@ class CompetitionEnding implements Event {
3538
true,
3639
{ code: 'MISSING_GROUP_ID' } | { code: 'MISSING_TIME_LEFT' } | MessagePropagationError
3740
> {
38-
const { groupId, competition, period } = data;
41+
const { groupId, competition } = data;
3942
const { id, metric, type, title, startsAt, endsAt } = competition;
4043

4144
if (!groupId) {
@@ -44,7 +47,8 @@ class CompetitionEnding implements Event {
4447
});
4548
}
4649

47-
const timeLeft = getTimeLeft(period);
50+
// Soon the API will always return "minutesLeft", so "period" can be removed
51+
const timeLeft = 'period' in data ? getPeriodTimeLeft(data.period) : getTimeLeft(data.minutesLeft);
4852

4953
if (!timeLeft) {
5054
return errored({
@@ -58,11 +62,17 @@ class CompetitionEnding implements Event {
5862
{ name: 'Duration', value: durationBetween(new Date(startsAt), new Date(endsAt)) }
5963
];
6064

61-
if (period.minutes && period.minutes < 60) {
62-
fields.push({
63-
name: `\u200B`,
64-
value: `⚠️ Don't forget to update your account's hiscores **before the time is up!**`
65-
});
65+
const warningField = {
66+
name: `\u200B`,
67+
value: `⚠️ Don't forget to update your account's hiscores **before the time is up!**`
68+
};
69+
70+
if ('period' in data) {
71+
if (data.period.minutes && data.period.minutes < 60) {
72+
fields.push(warningField);
73+
}
74+
} else if (data.minutesLeft < 60) {
75+
fields.push(warningField);
6676
}
6777

6878
const message = new EmbedBuilder()
@@ -75,7 +85,16 @@ class CompetitionEnding implements Event {
7585
}
7686
}
7787

78-
function getTimeLeft(period: { hours?: number; minutes?: number }) {
88+
function getTimeLeft(minutesLeft: number) {
89+
if (minutesLeft >= 60) {
90+
const hours = Math.floor(minutesLeft / 60);
91+
return `${hours} ${hours === 1 ? 'hour' : 'hours'}`;
92+
}
93+
94+
return `${minutesLeft} ${minutesLeft === 1 ? 'minute' : 'minutes'}`;
95+
}
96+
97+
function getPeriodTimeLeft(period: { hours?: number; minutes?: number }) {
7998
const { hours, minutes } = period;
8099

81100
if (hours && hours > 0) {

src/events/instances/CompetitionStarting.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1+
import { AsyncResult, errored } from '@attio/fetchable';
12
import { Competition, getMetricName } from '@wise-old-man/utils';
23
import { Client, EmbedBuilder } from 'discord.js';
34
import { capitalize } from 'lodash';
45
import config from '../../config';
5-
import { Event } from '../../utils/events';
66
import {
7-
propagateMessage,
8-
NotificationType,
97
durationBetween,
108
getEmoji,
11-
MessagePropagationError
9+
MessagePropagationError,
10+
NotificationType,
11+
propagateMessage
1212
} from '../../utils';
13-
import { AsyncResult, errored } from '@attio/fetchable';
13+
import { Event } from '../../utils/events';
1414

15-
interface CompetitionStartingData {
15+
type CompetitionStartingData = {
1616
groupId: number;
1717
competition: Competition;
18-
period: {
19-
hours?: number;
20-
minutes?: number;
21-
};
22-
}
18+
} & (
19+
| {
20+
period: { hours?: number; minutes?: number };
21+
}
22+
| {
23+
minutesLeft: number;
24+
}
25+
);
2326

2427
class CompetitionStarting implements Event {
2528
type: string;
@@ -35,7 +38,7 @@ class CompetitionStarting implements Event {
3538
true,
3639
{ code: 'MISSING_GROUP_ID' } | { code: 'MISSING_TIME_LEFT' } | MessagePropagationError
3740
> {
38-
const { groupId, competition, period } = data;
41+
const { groupId, competition } = data;
3942
const { id, metric, startsAt, endsAt, type, title } = competition;
4043

4144
if (!groupId) {
@@ -44,7 +47,8 @@ class CompetitionStarting implements Event {
4447
});
4548
}
4649

47-
const timeLeft = getTimeLeft(period);
50+
// Soon the API will always return "minutesLeft", so "period" can be removed
51+
const timeLeft = 'period' in data ? getPeriodTimeLeft(data.period) : getTimeLeft(data.minutesLeft);
4852

4953
if (!timeLeft) {
5054
return errored({
@@ -68,7 +72,16 @@ class CompetitionStarting implements Event {
6872
}
6973
}
7074

71-
function getTimeLeft(period: { hours?: number; minutes?: number }) {
75+
function getTimeLeft(minutesLeft: number) {
76+
if (minutesLeft >= 60) {
77+
const hours = Math.floor(minutesLeft / 60);
78+
return `${hours} ${hours === 1 ? 'hour' : 'hours'}`;
79+
}
80+
81+
return `${minutesLeft} ${minutesLeft === 1 ? 'minute' : 'minutes'}`;
82+
}
83+
84+
function getPeriodTimeLeft(period: { hours?: number; minutes?: number }) {
7285
const { hours, minutes } = period;
7386

7487
if (hours && hours > 0) {

0 commit comments

Comments
 (0)