add date check and fix send next post not working
This commit is contained in:
@@ -14,14 +14,17 @@
|
||||
"axios": "^0.26.0",
|
||||
"body-parser": "^1.20.2",
|
||||
"cheerio": "^1.0.0-rc.10",
|
||||
"dayjs": "^1.11.10",
|
||||
"discord.js": "^12.5.1",
|
||||
"dotenv": "^8.2.0",
|
||||
"elysia": "^0.7.15",
|
||||
"lodash-es": "^4.17.21",
|
||||
"minimatch": "^9.0.3",
|
||||
"pino": "^8.15.4",
|
||||
"typescript": "^4.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/lodash-es": "^4.17.9",
|
||||
"@types/node": "^20.8.2",
|
||||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||
"@typescript-eslint/parser": "^6.7.4",
|
||||
|
||||
1
process-env.d.ts
vendored
1
process-env.d.ts
vendored
@@ -6,6 +6,7 @@ declare global {
|
||||
CRON_LEGICA: string;
|
||||
PASSWORD: string;
|
||||
TIMEZONE: string;
|
||||
LEGICA_DATE_FORMAT: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
src/app.ts
18
src/app.ts
@@ -22,12 +22,20 @@ const logger = pino(
|
||||
fileTransport
|
||||
);
|
||||
|
||||
async function jobRunner() {
|
||||
try {
|
||||
await sendNextMessage(client);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
const taskPlugin = new Elysia({ prefix: "/job" })
|
||||
.use(
|
||||
cron({
|
||||
name: "job",
|
||||
pattern: config.CRON_LEGICA,
|
||||
run: () => sendNextMessage(client),
|
||||
run: jobRunner,
|
||||
paused: true,
|
||||
timezone: config.TIMEZONE,
|
||||
})
|
||||
@@ -143,9 +151,11 @@ const taskPlugin = new Elysia({ prefix: "/job" })
|
||||
}
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
url: t.String(),
|
||||
}),
|
||||
body: t.Optional(
|
||||
t.Object({
|
||||
url: t.Optional(t.String()),
|
||||
})
|
||||
),
|
||||
detail: {
|
||||
summary: "Send legica-dana post to discord channels",
|
||||
},
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
import { getFirstHtml, getImgTitle } from "@common";
|
||||
import { config } from "@constants";
|
||||
import dayjs from "dayjs";
|
||||
import customParseFormat from "dayjs/plugin/customParseFormat";
|
||||
import { Client, MessageEmbed, TextChannel } from "discord.js";
|
||||
|
||||
dayjs.extend(customParseFormat);
|
||||
|
||||
const dateRegex = /\d{1,2}.\d{1,2}.\d{4}/g;
|
||||
|
||||
export async function sendDiscordMessage(
|
||||
client: Client,
|
||||
url: string
|
||||
url: string,
|
||||
dateCheck?: dayjs.Dayjs
|
||||
): Promise<void> {
|
||||
if (!url) return;
|
||||
const { img, title } = await getImgTitle(url);
|
||||
|
||||
if (dateCheck) {
|
||||
const date = dateRegex.exec(title)?.[0];
|
||||
const dayjsDate = dayjs(date, config.LEGICA_DATE_FORMAT);
|
||||
if (!dateCheck.isSame(dayjsDate, "D"))
|
||||
throw new Error(
|
||||
`Post failed date check, date from post ${date}, date checked ${dateCheck.format(
|
||||
config.LEGICA_DATE_FORMAT
|
||||
)}`
|
||||
);
|
||||
}
|
||||
|
||||
client.channels.cache.forEach(async (channel) => {
|
||||
try {
|
||||
if (channel.type !== "text") return null;
|
||||
@@ -39,10 +58,6 @@ export async function sendDiscordMessage(
|
||||
}
|
||||
|
||||
export async function sendNextMessage(client: Client): Promise<void> {
|
||||
try {
|
||||
const href = await getFirstHtml();
|
||||
await sendDiscordMessage(client, href);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
await sendDiscordMessage(client, href, dayjs());
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ const config: ProjectConfig = {
|
||||
APP_VERSION: version,
|
||||
LEGICA_URL: "https://sib.net.hr/legica-dana",
|
||||
TIMEZONE: process.env.TIMEZONE || "utc",
|
||||
LEGICA_DATE_FORMAT: process.env.LEGICA_DATE_FORMAT || "DD.MM.YYYY",
|
||||
};
|
||||
|
||||
export { config };
|
||||
|
||||
Reference in New Issue
Block a user