@@ -2,15 +2,14 @@ import { Context } from "grammy";
22import { WarnService } from "../command/warn" ;
33import { SafeExecution } from "../../decorators/SafeExecution" ;
44import { MessageCheck } from "../MessageCheck" ;
5+ import { GroupSettingsService } from "../db/group" ;
56
67export class Spam {
78 static MessageCounts : Map <
89 number ,
910 { count : number ; lastMessageTime : number }
1011 > = new Map ( ) ;
1112
12- static messageFlags : { [ groupId : number ] : { [ date : string ] : boolean } } = { } ;
13-
1413 @SafeExecution ( )
1514 static isWithinTimeRange ( current : Date , start : string , end : string ) : boolean {
1615 const [ startHour , startMinute ] = start . split ( ":" ) . map ( Number ) ;
@@ -25,36 +24,37 @@ export class Spam {
2524 }
2625
2726 @SafeExecution ( )
28- static isExactTime ( current : Date ) : boolean {
29- const randomTime = Spam . getRandomTime ( ) ;
30- const [ targetHour , targetMinute ] = randomTime . split ( ":" ) . map ( Number ) ;
31- return (
32- current . getHours ( ) === targetHour && current . getMinutes ( ) === targetMinute
33- ) ;
34- }
35-
36- private static getRandomTime ( ) : string {
37- const times = [ "01:00" , "01:05" ] ;
38- return times [ Math . floor ( Math . random ( ) * times . length ) ] ;
27+ static async toggleSpamTime ( ctx : Context , toggle : boolean ) {
28+ const groupId = ctx . chat ! . id ;
29+ const groupRepo = new GroupSettingsService ( ) ;
30+ const groupSettings = await groupRepo . getByGroupId ( groupId ) ;
31+ if ( groupSettings ) {
32+ if ( toggle && ! groupSettings . isSpamTime ) {
33+ groupSettings . isSpamTime = toggle ;
34+ await groupRepo . save ( groupSettings ) ;
35+ await ctx . reply ( "Spam time has started! 🎉" ) ;
36+ } else if ( ! toggle && groupSettings . isSpamTime ) {
37+ groupSettings . isSpamTime = toggle ;
38+ await groupRepo . save ( groupSettings ) ;
39+ }
40+ }
3941 }
4042
4143 @SafeExecution ( )
42- static async WarnSpam ( ctx : Context ) {
43- const chatId = ctx . chat ! . id ;
44+ static async checkAndToggleSpamTime ( ctx : Context ) {
4445 const now = new Date ( ) ;
45- const todayKey = now . toISOString ( ) . split ( "T" ) [ 0 ] ;
4646
47- // Check if within the specified time range
48- if ( ! Spam . messageFlags [ chatId ] ) {
49- Spam . messageFlags [ chatId ] = { } ;
47+ // Check if it's within spam time (12:45 AM to 1:00 AM)
48+ if ( await Spam . isWithinTimeRange ( now , "00:45" , "13:00" ) ) {
49+ await Spam . toggleSpamTime ( ctx , true ) ;
50+ } else {
51+ await Spam . toggleSpamTime ( ctx , false ) ;
5052 }
53+ }
5154
52- if ( ! Spam . messageFlags [ chatId ] [ todayKey ] ) {
53- if ( await Spam . isWithinTimeRange ( now , "00:45" , "00:59" ) ) {
54- await ctx . reply ( "بوی اسپم تایم میاد 😋" ) ;
55- Spam . messageFlags [ chatId ] [ todayKey ] = true ;
56- }
57- }
55+ @SafeExecution ( )
56+ static async WarnSpam ( ctx : Context ) {
57+ await Spam . checkAndToggleSpamTime ( ctx ) ;
5858
5959 // Existing spam check logic
6060 if ( ctx . message ?. sticker || ctx . message ?. animation ) {
@@ -76,7 +76,7 @@ export class Spam {
7676 return ;
7777 }
7878 } else {
79- userData . count = 1 ; // Reset count if outside the time window
79+ userData . count = 1 ;
8080 }
8181 userData . lastMessageTime = currentTime ;
8282 Spam . MessageCounts . set ( userId , userData ) ;
@@ -107,4 +107,4 @@ export class Spam {
107107 ) ;
108108 }
109109 }
110- }
110+ }
0 commit comments