Skip to content

Commit e5683f3

Browse files
authored
Add date option to Checkin command (#46)
* Add date option to Checkin command * Update date description in Checkin command * Fix invalid date format handling in Checkin command * Fix date validation issue
1 parent d828692 commit e5683f3

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/commands/Checkin.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export default class Checkin extends Command {
2929
.addBooleanOption(option =>
3030
option.setName('widescreen').setDescription('Include a slide for the QR code.').setRequired(false)
3131
)
32+
.addStringOption(option =>
33+
option.setName('date').setDescription('The date to check for events. Use MM/DD format.').setRequired(false)
34+
)
3235
.setDescription(
3336
"Sends a DM or embed with all check-in codes from today's events. Includes Express Checkin QR code!"
3437
);
@@ -68,6 +71,31 @@ export default class Checkin extends Command {
6871
// Get arguments. Get rid of the null types by checking them.
6972
const publicArgument = interaction.options.getBoolean('public');
7073
const widescreenArgument = interaction.options.getBoolean('widescreen');
74+
const dateArgument = interaction.options.getString('date');
75+
76+
// Regex to match dates in the format of MM/DD(/YYYY) or MM-DD(-YYYY).
77+
const regexp = new RegExp('^(\\d{1,2})(/|-)(\\d{1,2})((/|-)(\\d{2}|\\d{4})){0,1}$', 'g');
78+
const dateMatches = regexp.exec(dateArgument!);
79+
80+
if (dateArgument !== null && dateMatches === null) {
81+
await super.respond(interaction, {
82+
content: 'Invalid date format. Please use MM/DD or MM-DD format.',
83+
ephemeral: true,
84+
});
85+
return;
86+
}
87+
88+
const month = dateMatches?.[1] ? parseInt(dateMatches[1], 10) : DateTime.now().month;
89+
const day = dateMatches?.[3] ? parseInt(dateMatches[3], 10) : DateTime.now().day;
90+
const year = dateMatches?.[6] ? parseInt(dateMatches[6], 10) : DateTime.now().year;
91+
92+
if (month < 1 || month > 12 || day < 1 || day > 31) {
93+
await super.respond(interaction, {
94+
content: 'Invalid date. Please use a valid date.',
95+
ephemeral: true,
96+
});
97+
return;
98+
}
7199

72100
// By default, we want the QR code to be DMed to the user.
73101
const isPublic = publicArgument !== null ? publicArgument : false;
@@ -87,22 +115,10 @@ export default class Checkin extends Command {
87115
// We need an array to store all events that have a start time within today's timeframe.
88116
const todayEvents = futureEvents.filter(event => {
89117
// get today's midnight
90-
const midnightToday = DateTime.now().set({
91-
hour: 0,
92-
minute: 0,
93-
second: 0,
94-
millisecond: 0,
95-
});
118+
const midnightToday = DateTime.local(year, month, day, 0, 0, 0, 0);
96119

97120
// get tomorrow's midnight
98-
const midnightTomorrow = DateTime.now()
99-
.set({
100-
hour: 0,
101-
minute: 0,
102-
second: 0,
103-
millisecond: 0,
104-
})
105-
.plus({ day: 1 });
121+
const midnightTomorrow = midnightToday.plus({ day: 1 });
106122

107123
// check if start time in between
108124
return Interval.fromDateTimes(midnightToday, midnightTomorrow).contains(event.start);

0 commit comments

Comments
 (0)