feat(playdate-formats): adds format hint and adds partial end entry
This commit is contained in:
parent
b852c06f83
commit
62a32c8222
1 changed files with 47 additions and 5 deletions
|
|
@ -4,7 +4,7 @@ import {
|
||||||
ChatInputCommandInteraction,
|
ChatInputCommandInteraction,
|
||||||
CommandInteraction,
|
CommandInteraction,
|
||||||
EmbedBuilder,
|
EmbedBuilder,
|
||||||
GuildMember,
|
GuildMember, hyperlink,
|
||||||
MessageFlags,
|
MessageFlags,
|
||||||
SlashCommandBuilder,
|
SlashCommandBuilder,
|
||||||
time
|
time
|
||||||
|
|
@ -45,10 +45,12 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
|
||||||
.addStringOption((option) => option
|
.addStringOption((option) => option
|
||||||
.setName("from")
|
.setName("from")
|
||||||
.setDescription("Defines the start date & time. Your desired format is probably support.")
|
.setDescription("Defines the start date & time. Your desired format is probably support.")
|
||||||
|
.setRequired(true)
|
||||||
)
|
)
|
||||||
.addStringOption((option) => option
|
.addStringOption((option) => option
|
||||||
.setName("to")
|
.setName("to")
|
||||||
.setDescription("Defines the end date & time. Your desired format is probably support.")
|
.setDescription("Defines the end date & time. Your desired format is probably support.")
|
||||||
|
.setRequired(true)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.addSubcommand((subcommand) => subcommand
|
.addSubcommand((subcommand) => subcommand
|
||||||
|
|
@ -130,20 +132,54 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
|
||||||
group
|
group
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const fromDateString = <string>interaction.options.get("from")?.value ?? '';
|
||||||
|
const toDateString = <string>interaction.options.get("to")?.value ?? '';
|
||||||
|
|
||||||
|
if (!this.checkDateString(fromDateString) || !this.checkDateString(toDateString)) {
|
||||||
|
throw new UserError(
|
||||||
|
"Please do not use words like 'Uhr', since those may result in values you did not intent.",
|
||||||
|
`Write the time out completely. Either in military time (15:00) or in the 12-hour format (3pm). For more valid formats check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const [fromDate, toDate] = timezoneHandler.use(() => {
|
const [fromDate, toDate] = timezoneHandler.use(() => {
|
||||||
|
const fromDate = parser.fromString(fromDateString);
|
||||||
|
const toAttempt = parser.attempt(toDateString);
|
||||||
|
|
||||||
|
if (toAttempt.invalid) {
|
||||||
|
throw new UserError(
|
||||||
|
"No date or invalid date format for the to parameter.",
|
||||||
|
`If you want to make sure your format is valid, please check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const toDate = new Date(fromDate.getTime());
|
||||||
|
if (toAttempt.year !== undefined) {
|
||||||
|
toDate.setFullYear(toAttempt.year, toAttempt.month ?? 0, toAttempt.day ?? 0);
|
||||||
|
}
|
||||||
|
if (toAttempt.hour !== undefined) {
|
||||||
|
toDate.setHours(toAttempt.hour, toAttempt.minute ?? 0, toAttempt.second ?? 0);
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
parser.fromString(<string>interaction.options.get("from")?.value ?? ''),
|
fromDate,
|
||||||
parser.fromString(<string>interaction.options.get("to")?.value ?? '')
|
toDate
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
if (!fromDate.isValid()) {
|
if (!fromDate.isValid()) {
|
||||||
throw new UserError("No date or invalid date format for the from parameter.");
|
throw new UserError(
|
||||||
|
"No date or invalid date format for the from parameter.",
|
||||||
|
`If you want to make sure your format is valid, please check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}.`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fromDate.isValid()) {
|
if (!fromDate.isValid()) {
|
||||||
throw new UserError("No date or invalid date format for the to parameter.");
|
throw new UserError(
|
||||||
|
"No date or invalid date format for the to parameter.",
|
||||||
|
`If you want to make sure your format is valid, please check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}.`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fromDate.getTime() > toDate.getTime()) {
|
if (fromDate.getTime() > toDate.getTime()) {
|
||||||
|
|
@ -212,6 +248,12 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private checkDateString(value: string): boolean {
|
||||||
|
return [
|
||||||
|
'Uhr'
|
||||||
|
].some((section) => value.includes(section));
|
||||||
|
}
|
||||||
|
|
||||||
private async list(interaction: ChatInputCommandInteraction, group: GroupModel) {
|
private async list(interaction: ChatInputCommandInteraction, group: GroupModel) {
|
||||||
const playdates = Container.get<PlaydateRepository>(PlaydateRepository.name).findFromGroup(group);
|
const playdates = Container.get<PlaydateRepository>(PlaydateRepository.name).findFromGroup(group);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue