11import { Bot } from "grammy" ;
2- import { AppDataSource } from "./config/db" ;
3- import { logger } from "./config/logging" ;
42import { GenerateCommand } from "./service/command/generator" ;
5- import { groupJoin } from "./service/bot/groupJoin" ;
63import { MessageCheck } from "./service/MessageCheck" ;
74import { 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" } ) ;
99const bot = new Bot ( process . env . TELEGRAM_BOT_TOKEN ! ) ;
1010new GenerateCommand ( bot ) . generate ( ) ;
11- const messageFlags : { [ groupId : number ] : { [ date : string ] : boolean } } = { } ;
12-
1311bot . 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} ) ;
4826bot . 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} ) ( ) ;
0 commit comments