Skip to content

Commit 92bbeb8

Browse files
committed
refactor(service/bot) BotOverseer and Spam classes: removed logger statements, updated imports, and modified spam time logic
1 parent 243ca85 commit 92bbeb8

2 files changed

Lines changed: 31 additions & 46 deletions

File tree

src/service/bot/index.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Context } from "grammy";
22
import { ChatMemberAdministrator, ChatMemberOwner, User } from "grammy/types";
33
import { SafeExecution } from "../../decorators/SafeExecution";
4-
import { BotInfo, RepliedMessage } from "../../types/types";
5-
import { logger } from "../../config/logger";
4+
import { BotInfo, RepliedMessage } from "../../types/index";
65
export class BotOverseer {
76
private ctx: Context;
87

@@ -16,17 +15,11 @@ export class BotOverseer {
1615
async isBotAdmin(): Promise<boolean> {
1716
const botInfo = await this.getBotInfo();
1817
if (!botInfo) {
19-
logger.error("Bot info is not available.", undefined, "isBotAdmin");
2018
return false;
2119
}
2220

2321
const chatMember = await this.ctx.getChatMember(botInfo.id);
2422
if (!chatMember) {
25-
logger.error(
26-
"Failed to fetch chat member info for the bot.",
27-
undefined,
28-
"isBotAdmin"
29-
);
3023
return false;
3124
}
3225
return ["administrator", "creator"].includes(chatMember.status);
@@ -75,20 +68,12 @@ export class BotOverseer {
7568
@SafeExecution()
7669
async getUser(): Promise<User> {
7770
const user = await this.ctx.message?.from;
78-
if (!user) {
79-
logger.error(
80-
"User information is not available in the context in getUser",
81-
undefined,
82-
"BotOverseer Class",
83-
{
84-
context: this.ctx,
85-
}
86-
);
87-
}
8871
return user!;
8972
}
9073
@SafeExecution()
91-
async userInGroup(msg: string = "The user is no longer a member of this group."): Promise<boolean | undefined> {
74+
async userInGroup(
75+
msg: string = "The user is no longer a member of this group."
76+
): Promise<boolean | undefined> {
9277
const id = await this.getRepliedUserId();
9378
if (!id) {
9479
await this.ctx.reply(

src/service/bot/spam.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import { Context } from "grammy";
22
import { WarnService } from "../command/warn";
33
import { SafeExecution } from "../../decorators/SafeExecution";
44
import { MessageCheck } from "../MessageCheck";
5+
import { GroupSettingsService } from "../db/group";
56

67
export 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

Comments
 (0)