Skip to content

Commit 5cecd14

Browse files
committed
refactor(command): BlacklistService to extract getBlackList method and updated GenerateCommand to modify command creation logic and add logging
1 parent 9660a1d commit 5cecd14

2 files changed

Lines changed: 36 additions & 34 deletions

File tree

src/service/command/blacklist.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Repository } from "typeorm";
21
import { SafeExecution } from "../../decorators/SafeExecution";
32
import { GroupSettings } from "../../entities/GroupSettings";
43
import * as BlackListJson from "../../helper/black_list.json";
@@ -12,20 +11,21 @@ export class BlacklistService {
1211
@SafeExecution()
1312
@initGroupSetting()
1413
static async storeBlacklistTerms(groupSettings: GroupSettings) {
15-
const newTerms = BlackListJson.map((item: { term: string }) =>
16-
item.term.toLowerCase()
17-
);
18-
14+
const bl = this.getBlackList()
1915
const existingBlacklist = groupSettings.black_list || [];
2016

2117
// Add only new terms
22-
const updatedBlacklist = [...new Set([...existingBlacklist, ...newTerms])];
18+
const updatedBlacklist = [...new Set([...existingBlacklist, ...bl])];
2319

2420
groupSettings.black_list = updatedBlacklist;
2521
await BlacklistService.GroupSettings.save(groupSettings);
2622
return updatedBlacklist;
2723
}
28-
24+
static getBlackList() {
25+
return BlackListJson.map((item: { term: string }) =>
26+
item.term.toLowerCase()
27+
);
28+
}
2929
// Display the blacklist in a formatted manner
3030
@SafeExecution()
3131
@initGroupSetting()
@@ -102,4 +102,4 @@ export class BlacklistService {
102102

103103
return ctx.reply("The desired word was removed from the blacklist");
104104
}
105-
}
105+
}

src/service/command/generator.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Command } from "../../controller/command";
33
import { CmdMid } from "../../middleware";
44
import { botIsAdmin } from "../../middleware/isAdmin";
55
import { COMMANDS } from "../../helper";
6+
import { Logger } from "../../config/logger";
67
export class GenerateCommand {
78
private bot: Bot;
89
constructor(bot: Bot) {
@@ -11,7 +12,7 @@ export class GenerateCommand {
1112
private create(name: string, mids?: ((ctx: Context, next: NextFunction) => Promise<void>)[]) {
1213
const middleware = mids ? this.compose(mids) : undefined;
1314
if (middleware) {
14-
this.bot.command(name, botIsAdmin, middleware, Command.handleCommand);
15+
this.bot.command(name, middleware, botIsAdmin, Command.handleCommand);
1516
} else {
1617
this.bot.command(name, Command.handleCommand);
1718
}
@@ -47,30 +48,31 @@ export class GenerateCommand {
4748
*/
4849
generate() {
4950
for (const command of COMMANDS) {
50-
if (["start", "help", "date","future","rules","shahin"].includes(command)) {
51-
this.create(command);
52-
} else if (
53-
["lock", "blacklist", "abl", "unLock", "rmbl","approvedList"].includes(command)
54-
) {
55-
this.create(command, [CmdMid.isSupergroupOrChannel,CmdMid.AdminStatus]);
56-
} else if (["purge"].includes(command)) {
57-
this.create(command, [CmdMid.isSupergroupOrChannel,CmdMid.isReplied, CmdMid.AdminStatus]);
58-
} else if (["unBan"].includes(command)) {
59-
this.create(command, [
60-
CmdMid.isSupergroupOrChannel,
61-
CmdMid.isReplied,
62-
CmdMid.AdminStatus,
63-
CmdMid.adminCheckForRepliedUser,
64-
]);
65-
} else {
66-
this.create(command, [
67-
CmdMid.userInGroup,
68-
CmdMid.isSupergroupOrChannel,
69-
CmdMid.isReplied,
70-
CmdMid.AdminStatus,
71-
CmdMid.adminCheckForRepliedUser,
72-
]);
73-
}
51+
if (["start", "help", "date", "future"].includes(command)) {
52+
this.create(command);
53+
} else if (["rules", "shahin"].includes(command)) {
54+
this.create(command, [CmdMid.isValidChatType]);
55+
} else if (["lock", "blacklist", "abl", "unLock", "rmbl", "approvedList"].includes(command)) {
56+
this.create(command, [CmdMid.isValidChatType, CmdMid.AdminStatus]);
57+
} else if (["purge"].includes(command)) {
58+
this.create(command, [CmdMid.isValidChatType, CmdMid.isReplied, CmdMid.AdminStatus]);
59+
} else if (["unBan"].includes(command)) {
60+
this.create(command, [
61+
CmdMid.isValidChatType,
62+
CmdMid.isReplied,
63+
CmdMid.AdminStatus,
64+
CmdMid.adminCheckForRepliedUser,
65+
]);
66+
} else {
67+
this.create(command, [
68+
CmdMid.userInGroup,
69+
CmdMid.isValidChatType,
70+
CmdMid.isReplied,
71+
CmdMid.AdminStatus,
72+
CmdMid.adminCheckForRepliedUser,
73+
]);
74+
}
7475
}
75-
}
76+
}
77+
7678
}

0 commit comments

Comments
 (0)