-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.ts
110 lines (94 loc) · 3.46 KB
/
scheduler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Telegraf } from 'telegraf';
import dotenv from 'dotenv';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import * as winston from 'winston';
import { utilities } from "nest-winston";
import 'winston-daily-rotate-file';
dotenv.config({ path: '.env.production' });
interface MessageInfo {
chatId: number;
messageId: number;
expireAt?: number;
}
const createLogger = (level: string) => {
const dailyRotateFileTransport = new winston.transports.DailyRotateFile({
level: level,
filename: `logs/scheduler/${level}/%DATE%.${level}.log`,
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxFiles: 7,
format: winston.format.combine(
winston.format.timestamp(),
utilities.format.nestLike(),
),
});
const logger = winston.createLogger({
level: level,
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.json()
),
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
),
}),
dailyRotateFileTransport
]
});
return logger;
}
const infoLogger = createLogger('info');
const errorLogger = createLogger('error');
const exceptionLogger = createLogger('exception');
const startScheduler = async () => {
infoLogger.log("info", `🟢 Scheduler Timer : ${new Date()} (${Date.now()})`);
const TOKEN: string | undefined = process.env.TOKEN;
if (TOKEN) {
const bot = new Telegraf(TOKEN);
// Restrict
const restrictFilePath: string | undefined = process.env.RESTRICT_FILE_PATH;
if (restrictFilePath) {
if (existsSync(restrictFilePath)) {
const messages: MessageInfo[] = JSON.parse(readFileSync(restrictFilePath, 'utf8'));
const notDeleteMessages: MessageInfo[] = [];
const deleteMessages: MessageInfo[] = [];
for (const message of messages) {
if (Date.now() - message.expireAt! <= (3 * 60 * 1000)) {
// if ((message.expireAt! + (3 * 60 * 1000)) <= Date.now()) {
notDeleteMessages.push(message);
} else {
deleteMessages.push(message);
}
}
for (const message of deleteMessages) {
try {
const editMessage = await bot.telegram.editMessageText(message.chatId, message.messageId, undefined, "Deleting...");
// const checkMessage = await bot.telegram.getChatMember(CHAT_ID, message.messageId);
if (editMessage) {
await bot.telegram.deleteMessage(message.chatId, message.messageId);
infoLogger.log("info", `🟢 Delete the message.`);
} else {
errorLogger.log("error", `🔴 The message you want to delete does not exist. id - '${message.messageId}`);
}
} catch (e) {
exceptionLogger.log("exception", `❌ Message lookup failed. id - '${message.messageId}`);
}
}
writeFileSync(restrictFilePath, JSON.stringify(notDeleteMessages), { flag: 'w', encoding: 'utf8' });
}
} else {
errorLogger.log("error", `🔴 Restrict file path is undefined`);
}
} else {
errorLogger.log("error", `🔴 Token is undefined`);
}
}
infoLogger.log("info", `🟢 Start community message scheduler`);
setInterval(startScheduler, 60 * 1000);