refactor announcement date handling to support 'soon' and streamline date parsing logic
All checks were successful
CI / build (push) Successful in 9s
All checks were successful
CI / build (push) Successful in 9s
This commit is contained in:
@@ -108,35 +108,43 @@ export default async function (_client: Client) {
|
||||
if (comingWhenRaw && comingWhenRaw.trim() !== "") {
|
||||
let epoch: number | null = null;
|
||||
const trimmed = comingWhenRaw.trim();
|
||||
// Try DD/MM/YYYY
|
||||
const ddmmyyyy = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(
|
||||
trimmed,
|
||||
);
|
||||
if (ddmmyyyy) {
|
||||
const [, dd, mm, yyyy] = ddmmyyyy;
|
||||
const date = new Date(
|
||||
Number(yyyy),
|
||||
Number(mm) - 1,
|
||||
Number(dd),
|
||||
);
|
||||
if (!isNaN(date.getTime()))
|
||||
epoch = Math.floor(date.getTime() / 1000);
|
||||
} else if (/^\d+$/.test(trimmed)) {
|
||||
// Raw epoch
|
||||
epoch = Number(trimmed);
|
||||
}
|
||||
if (epoch !== null) {
|
||||
if (trimmed.toLowerCase() === "soon") {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: `<t:${epoch}:F> (<t:${epoch}:R>)`,
|
||||
value: "Soon™",
|
||||
inline: false,
|
||||
});
|
||||
} else {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: trimmed,
|
||||
inline: false,
|
||||
});
|
||||
// Try DD/MM/YYYY
|
||||
const ddmmyyyy = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(
|
||||
trimmed,
|
||||
);
|
||||
if (ddmmyyyy) {
|
||||
const [, dd, mm, yyyy] = ddmmyyyy;
|
||||
const date = new Date(
|
||||
Number(yyyy),
|
||||
Number(mm) - 1,
|
||||
Number(dd),
|
||||
);
|
||||
if (!isNaN(date.getTime()))
|
||||
epoch = Math.floor(date.getTime() / 1000);
|
||||
} else if (/^\d+$/.test(trimmed)) {
|
||||
// Raw epoch
|
||||
epoch = Number(trimmed);
|
||||
}
|
||||
if (epoch !== null) {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: `<t:${epoch}:F> (<t:${epoch}:R>)`,
|
||||
inline: false,
|
||||
});
|
||||
} else {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: trimmed,
|
||||
inline: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -128,31 +128,41 @@ export default async function (_client: Client) {
|
||||
if (comingWhenRaw && comingWhenRaw.trim() !== "") {
|
||||
let epoch: number | null = null;
|
||||
const trimmed = comingWhenRaw.trim();
|
||||
const ddmmyyyy = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(trimmed);
|
||||
if (ddmmyyyy) {
|
||||
const [, dd, mm, yyyy] = ddmmyyyy;
|
||||
const date = new Date(
|
||||
Number(yyyy),
|
||||
Number(mm) - 1,
|
||||
Number(dd),
|
||||
);
|
||||
if (!isNaN(date.getTime()))
|
||||
epoch = Math.floor(date.getTime() / 1000);
|
||||
} else if (/^\d+$/.test(trimmed)) {
|
||||
epoch = Number(trimmed);
|
||||
}
|
||||
if (epoch !== null) {
|
||||
if (trimmed.toLowerCase() === "soon") {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: `<t:${epoch}:F> (<t:${epoch}:R>)`,
|
||||
value: "Soon™",
|
||||
inline: false,
|
||||
});
|
||||
} else {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: trimmed,
|
||||
inline: false,
|
||||
});
|
||||
const ddmmyyyy = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(
|
||||
trimmed,
|
||||
);
|
||||
if (ddmmyyyy) {
|
||||
const [, dd, mm, yyyy] = ddmmyyyy;
|
||||
const date = new Date(
|
||||
Number(yyyy),
|
||||
Number(mm) - 1,
|
||||
Number(dd),
|
||||
);
|
||||
if (!isNaN(date.getTime()))
|
||||
epoch = Math.floor(date.getTime() / 1000);
|
||||
} else if (/^\d+$/.test(trimmed)) {
|
||||
epoch = Number(trimmed);
|
||||
}
|
||||
if (epoch !== null) {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: `<t:${epoch}:F> (<t:${epoch}:R>)`,
|
||||
inline: false,
|
||||
});
|
||||
} else {
|
||||
fields.push({
|
||||
name: "📅 Coming On",
|
||||
value: trimmed,
|
||||
inline: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user