Skip to content

Commit 32db900

Browse files
committed
refactor: Removed unused imports, rearranged imports, and modified code organization
- `index.ts`: Updated import statement for logger and reformatted error logging code
1 parent 89fbe64 commit 32db900

2 files changed

Lines changed: 14 additions & 33 deletions

File tree

src/app.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import { Bot } from "grammy";
2-
import { AppDataSource } from "./config/db";
3-
import { logger } from "./config/logging";
42
import { GenerateCommand } from "./service/command/generator";
5-
import { groupJoin } from "./service/bot/groupJoin";
63
import { MessageCheck } from "./service/MessageCheck";
74
import { Spam } from "./service/bot/spam";
8-
5+
import { groupJoin } from "./service/bot/groupJoin";
6+
import { Logger } from "./config/logger";
7+
import { db } from "./service/db";
8+
const logger = new Logger({ file: "app.log", level: "info" });
99
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN!);
1010
new GenerateCommand(bot).generate();
11-
const messageFlags: { [groupId: number]: { [date: string]: boolean } } = {};
12-
1311
bot.on("message", async (ctx) => {
1412
await MessageCheck.CheckBlackList(ctx);
1513
await Spam.WarnSpam(ctx);
16-
const now = new Date();
17-
const todayKey = now.toISOString().split("T")[0]; // Get today's date as a key
18-
const chatId = ctx.chat.id;
1914
if (ctx.message?.new_chat_members?.length! > 0) {
2015
const users = ctx.message?.new_chat_members!;
2116
for (const user of users) {
@@ -27,34 +22,16 @@ bot.on("message", async (ctx) => {
2722
}
2823
}
2924
}
30-
// Initialize if not already
31-
if (!messageFlags[chatId]) {
32-
messageFlags[chatId] = {};
33-
}
34-
// Check if the message has been sent today in this group
35-
if (!messageFlags[chatId][todayKey]) {
36-
// Check if it's 00:45 AM to send the first message
37-
if (await Spam.isWithinTimeRange(now, "00:45", "00:59")) {
38-
await ctx.reply("بوی اسپم تایم میاد 😋");
39-
messageFlags[chatId][todayKey] = true;
40-
}
41-
// Check if it's the exact random time to send the spam time message
42-
if (await Spam.isExactTime(now)) {
43-
await ctx.reply("اسپم تایم شروع شد!");
44-
messageFlags[chatId][todayKey] = true;
45-
}
46-
}
4725
});
4826
bot.on("my_chat_member", groupJoin);
49-
5027
(async () => {
5128
try {
52-
await AppDataSource.initialize();
53-
logger.info("DATABASE INITIALIZED", "DATABASE");
29+
await db.initialize();
5430
bot.start();
5531
logger.info("BOT STARTED", "APP");
5632
} catch (error: any) {
57-
logger.error("ERROR FROM START APP", error, "APP");
58-
process.exit(1)
33+
logger.error("ERROR FROM START APP", error.message, "APP");
34+
await db.close();
35+
process.exit(1);
5936
}
6037
})();

src/service/bot/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Context } from "grammy";
22
import { ChatMemberAdministrator, ChatMemberOwner, User } from "grammy/types";
33
import { SafeExecution } from "../../decorators/SafeExecution";
44
import { BotInfo, RepliedMessage } from "../../types/types";
5-
import { logger } from "../../config/logging";
5+
import { logger } from "../../config/logger";
66
export class BotOverseer {
77
private ctx: Context;
88

@@ -22,7 +22,11 @@ export class BotOverseer {
2222

2323
const chatMember = await this.ctx.getChatMember(botInfo.id);
2424
if (!chatMember) {
25-
logger.error("Failed to fetch chat member info for the bot.", undefined, "isBotAdmin");
25+
logger.error(
26+
"Failed to fetch chat member info for the bot.",
27+
undefined,
28+
"isBotAdmin"
29+
);
2630
return false;
2731
}
2832
return ["administrator", "creator"].includes(chatMember.status);

0 commit comments

Comments
 (0)