Skip to content

Commit 79054f4

Browse files
committed
feat(command): Added initGroupSetting decorator to several methods in BlacklistService, MuteService, and Rules classes; added logging to WarnService class; and made minor formatting changes.
1 parent d032932 commit 79054f4

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/service/command/blacklist.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { GroupSettings } from "../../entities/GroupSettings";
44
import * as BlackListJson from "../../helper/black_list.json";
55
import { Context } from "grammy";
66
import { GroupSettingsService } from "../db/group";
7+
import { initGroupSetting } from "../../decorators/db";
78

89
export class BlacklistService {
9-
private static GroupSettings = new GroupSettingsService()
10+
private static GroupSettings = new GroupSettingsService();
1011
// Store JSON terms into the database
1112
@SafeExecution()
13+
@initGroupSetting()
1214
static async storeBlacklistTerms(groupSettings: GroupSettings) {
1315
const newTerms = BlackListJson.map((item: { term: string }) =>
1416
item.term.toLowerCase()
@@ -26,6 +28,7 @@ export class BlacklistService {
2628

2729
// Display the blacklist in a formatted manner
2830
@SafeExecution()
31+
@initGroupSetting()
2932
static async BlackList(ctx: Context) {
3033
const groupId = ctx.chat?.id;
3134
if (!groupId) {
@@ -49,6 +52,7 @@ export class BlacklistService {
4952

5053
// Add new term to the blacklist
5154
@SafeExecution()
55+
@initGroupSetting()
5256
static async addBlackList(ctx: Context) {
5357
const groupId = ctx.chat?.id;
5458
if (!groupId) {
@@ -75,11 +79,12 @@ export class BlacklistService {
7579
}
7680

7781
@SafeExecution()
82+
@initGroupSetting()
7883
static async remove(ctx: Context) {
7984
const groupId = ctx.chat?.id!;
8085
const termToRemove = String(ctx.match);
8186
const repo = BlacklistService.GroupSettings;
82-
const groupSettings = await repo.getByGroupId(groupId)
87+
const groupSettings = await repo.getByGroupId(groupId);
8388
if (!groupSettings || !groupSettings.black_list) {
8489
return ctx.reply("The blacklist is currently empty.");
8590
}

src/service/command/mute.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Context } from "grammy";
22
import { MESSAGE } from "../../helper/message";
3+
import { initGroupSetting } from "../../decorators/db";
34

45
export class MuteService {
56
private userId: number;
@@ -13,10 +14,11 @@ export class MuteService {
1314
/**
1415
* Mutes a user with an optional expiration time.
1516
*/
17+
@initGroupSetting()
1618
async mute(expiration?: Date | null) {
1719
await this.isMute(false, expiration!);
1820
return MESSAGE.MUTE_SET(
19-
(this.ctx.message?.reply_to_message?.from!) || (this.ctx.message?.from!),
21+
this.ctx.message?.reply_to_message?.from! || this.ctx.message?.from!,
2022
expiration!
2123
);
2224
}
@@ -43,6 +45,7 @@ export class MuteService {
4345
/**
4446
* Lifts the mute restrictions.
4547
*/
48+
@initGroupSetting()
4649
async unmute() {
4750
await this.isMute(true);
4851
return MESSAGE.MUTE_CLEAR(this.ctx.message?.reply_to_message?.from!);

src/service/command/rules.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Context } from "grammy";
22
import { GroupSettingsService } from "../db/group";
3+
import { initGroupSetting } from "../../decorators/db";
34

45
export class Rules {
6+
@initGroupSetting()
57
static async rules(ctx: Context) {
68
const groupId = ctx.chat?.id;
79

@@ -12,7 +14,7 @@ export class Rules {
1214
const rulesInput = String(ctx.match).trim();
1315
const groupRepo: GroupSettingsService = new GroupSettingsService();
1416

15-
let groupSettings = await groupRepo.getByGroupId(groupId)
17+
let groupSettings = await groupRepo.getByGroupId(groupId);
1618

1719
// Check if the admin wants to delete all rules
1820
if (rulesInput.toLowerCase() === "r") {
@@ -29,10 +31,10 @@ export class Rules {
2931

3032
// If the user provides new rules input, update the rules
3133
if (rulesInput) {
32-
groupSettings!.rules = rulesInput;
34+
groupSettings!.rules +="\n"+ rulesInput;
3335
await groupRepo.save(groupSettings!);
3436
return ctx.reply(
35-
`Group rules have been updated:\n${groupSettings!.rules}`
37+
`Group rules have been updated:${groupSettings!.rules}\n`
3638
);
3739
} else {
3840
// If no rules input, display the existing rules

src/service/command/warn.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { BanService } from "./ban";
33
import { MESSAGE } from "../../helper/message";
44
import { UserService } from "../db/user";
55
import { WarningServiceDb } from "../db/user/warning";
6-
6+
import { Logger } from "../../config/logger";
7+
import { SafeExecution } from "../../decorators/SafeExecution";
8+
const logger = new Logger({file:"warnService.log",level:'debug',timestampFormat:'locale',})
79
export class WarnService {
810
private userId: number;
911
private ctx: Context;
@@ -20,10 +22,11 @@ export class WarnService {
2022
/**
2123
* Adds a warning to a user.
2224
*/
25+
// @SafeExecution()
2326
async warn(reason: string = "unknown") {
2427
let user = await this.userRepo.findByRelations(this.userId, "warnings");
25-
2628
if (!user) {
29+
logger.info('User not found, creating new user', 'WARN_SERVICE', { userId: this.userId });
2730
user = await this.userRepo.createUser(this.ctx, this.userId);
2831
}
2932
// Create and save the new warning
@@ -32,16 +35,19 @@ export class WarnService {
3235
reason,
3336
});
3437
await this.warnRepo.save(warning);
35-
38+
logger.info('Warning added to user', 'WARN_SERVICE', { userId: this.userId, warningId: warning.id });
3639
// Update user warnings list
3740
user.warnings.push(warning);
3841
await this.userRepo.save(user);
42+
logger.debug('User warnings updated', 'WARN_SERVICE', { userId: this.userId, warningCount: user.warnings.length });
3943

4044
// Check warning count
41-
const warningCount = await this.warnRepo.count(user)
45+
const warningCount = await this.warnRepo.count(user);
4246
if (warningCount >= 3) {
47+
logger.warn('User has reached warning limit', 'WARN_SERVICE', { userId: this.userId, warningCount });
4348
const msg: string = await new BanService(this.ctx, this.userId).ban();
44-
await this.warnRepo.clear({ user });
49+
await this.warnRepo.clear(user.id);
50+
logger.info('User banned and warnings cleared', 'WARN_SERVICE', { userId: this.userId, message: msg });
4551
return { warning, banned: true, message: msg };
4652
}
4753
return { warning, banned: false, count: warningCount };
@@ -50,29 +56,39 @@ export class WarnService {
5056
/**
5157
* Clears all warnings for a user.
5258
*/
59+
@SafeExecution()
5360
async clear() {
61+
logger.debug('Attempting to clear warnings for user', 'WARN_SERVICE', { userId: this.userId });
5462
const user = await this.userRepo.findByRelations(this.userId, "warnings");
5563
if (!user) {
64+
logger.info('No warnings found for user', 'WARN_SERVICE', { userId: this.userId });
5665
return MESSAGE.NO_WARNINGS();
5766
}
5867

59-
await this.warnRepo.clear(user.warnings!);
68+
await this.warnRepo.clear(user.id);
6069
user.warnings = [];
6170
await this.userRepo.save(user);
71+
logger.info('All warnings cleared for user', 'WARN_SERVICE', { userId: this.userId });
6272

6373
return MESSAGE.WARN_CLEAR();
6474
}
6575

6676
/**
6777
* Counts the number of warnings for a user.
6878
*/
79+
@SafeExecution()
6980
async count(): Promise<number> {
70-
const user = await this.userRepo.findByRelations(this.userId, "warnings");
81+
logger.debug('Counting warnings for user', 'WARN_SERVICE', { userId: this.userId });
7182

83+
const user = await this.userRepo.findByRelations(this.userId, "warnings");
7284
if (!user) {
85+
logger.info('No user found for warning count', 'WARN_SERVICE', { userId: this.userId });
7386
return 0;
7487
}
7588

76-
return user.warnings.length;
89+
const warningCount = user.warnings.length;
90+
logger.debug('Warning count for user', 'WARN_SERVICE', { userId: this.userId, warningCount });
91+
92+
return warningCount;
7793
}
7894
}

0 commit comments

Comments
 (0)