Skip to content

Commit 0cd3f8f

Browse files
committed
feat(group-management): Added logging functionality to AdminCommand class, including info, warn, and log execution decorators.
1 parent 6c3c617 commit 0cd3f8f

1 file changed

Lines changed: 87 additions & 7 deletions

File tree

src/group-management/AdminCommand.ts

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import { DateCommand } from "../service/command/date";
1111
import { Rules } from "../service/command/rules";
1212
import { Approved } from "../service/command/approved";
1313
import { Permissions } from "../service/command/Permissions";
14+
import { Logger } from "../config/logger";
15+
import { LogExecution } from "../decorators/Logger";
16+
const logger = new Logger({
17+
file: "admin-command.log",
18+
level: "info",
19+
timestampFormat: "locale",
20+
rotation: {
21+
enabled: true,
22+
maxSize: 5 * 1024 * 1024,
23+
maxFiles: 3,
24+
},
25+
});
26+
1427
export class AdminCommand {
1528
@SafeExecution()
1629
static async warn(ctx: Context): Promise<void> {
@@ -21,6 +34,12 @@ export class AdminCommand {
2134
]);
2235
const reason: string = String(ctx.match);
2336
const username = repliedMessage?.from?.username;
37+
38+
logger.info(
39+
`warn command triggered by ${ctx.from?.id} in chat ${ctx.chat?.id}`,
40+
"AdminCommand"
41+
);
42+
2443
if (repliedUserId && username) {
2544
const result = await new WarnService(ctx, repliedUserId).warn(reason);
2645
if (result.banned) {
@@ -30,11 +49,24 @@ export class AdminCommand {
3049
reply_to_message_id: ctx.message?.message_id,
3150
}
3251
);
52+
logger.info(
53+
`User ${username} banned for warnings in chat ${ctx.chat?.id}`,
54+
"AdminCommand"
55+
);
3356
} else if (result.warning) {
3457
await ctx.reply(MESSAGE.WARN(repliedMessage, result.count!, reason), {
3558
reply_to_message_id: ctx.message?.message_id,
3659
});
60+
logger.info(
61+
`Warning issued to ${username} in chat ${ctx.chat?.id}`,
62+
"AdminCommand"
63+
);
3764
}
65+
} else {
66+
logger.warn(
67+
`Failed to get user information for warning in chat ${ctx.chat?.id}`,
68+
"AdminCommand"
69+
);
3870
}
3971
}
4072

@@ -50,108 +82,156 @@ export class AdminCommand {
5082
durationMs = parseDuration(durationStr);
5183
}
5284

53-
// Set expiration date
5485
const expiration = durationMs ? new Date(Date.now() + durationMs) : null;
55-
// Create a new instance of MuteService and apply mute
86+
87+
logger.info(
88+
`mute command triggered by ${ctx.from?.id} in chat ${ctx.chat?.id} with duration ${durationStr}`,
89+
"AdminCommand"
90+
);
91+
5692
const result = await new MuteService(ctx, userId!).mute(expiration);
5793
await ctx.reply(result, {
5894
reply_to_message_id: ctx.message?.message_id,
5995
});
96+
logger.info(
97+
`User ${userId} muted in chat ${ctx.chat?.id} until ${expiration}`,
98+
"AdminCommand"
99+
);
60100
}
61101

62102
@SafeExecution()
63103
static async purge(ctx: Context): Promise<any> {
64104
const chatId = ctx.chat?.id;
65105
const replyToMessageId = ctx.message?.reply_to_message?.message_id;
66106
if (!chatId || !replyToMessageId) {
107+
logger.warn(
108+
`Purge command missing chatId or replyToMessageId from ${ctx.from?.id} in chat ${ctx.chat?.id}`,
109+
"AdminCommand"
110+
);
67111
return ctx.reply("Please reply to a message and use the /purge command.");
68112
}
113+
69114
let lastMessageId = ctx.message?.message_id;
70115
if (!lastMessageId) {
116+
logger.warn(
117+
`Purge command missing lastMessageId from ${ctx.from?.id} in chat ${ctx.chat?.id}`,
118+
"AdminCommand"
119+
);
71120
return ctx.reply("No message ID found.");
72121
}
73-
// Calculate the number of messages to delete
74-
const countMessages = lastMessageId - replyToMessageId;
75122

123+
const countMessages = lastMessageId - replyToMessageId;
76124
if (countMessages <= 0) {
125+
logger.warn(
126+
`No messages to delete or replyToMessageId is newer than lastMessageId from ${ctx.from?.id} in chat ${ctx.chat?.id}`,
127+
"AdminCommand"
128+
);
77129
return ctx.reply(
78130
"The reply-to message is not older than the current message or no messages to delete."
79131
);
80132
}
81-
// Prepare the array of message IDs to delete
133+
82134
const messagesToDelete = [];
83135
for (let i = 0; i <= countMessages; i++) {
84136
messagesToDelete.push(replyToMessageId + i);
85137
}
86138

87-
// Use ctx.deleteMessages to delete the messages
88-
await ctx.deleteMessages(messagesToDelete);
139+
const deleteMessagesInBatches = async (messages: number[]) => {
140+
const batchSize = 100;
141+
for (let i = 0; i < messages.length; i += batchSize) {
142+
const batch = messages.slice(i, i + batchSize);
143+
await ctx.deleteMessages(batch);
144+
}
145+
};
146+
147+
await deleteMessagesInBatches(messagesToDelete);
89148
await ctx.reply("Deleting done.");
149+
logger.info(`Purge completed in chat ${ctx.chat?.id}`, "AdminCommand");
90150
}
91151

92152
@SafeExecution()
153+
@LogExecution()
93154
static async lock(ctx: Context) {
94155
const type = String(ctx.match);
95156
await Permissions.modify(ctx, "lock", type);
96157
}
97158

98159
@SafeExecution()
160+
@LogExecution()
99161
static async unLock(ctx: Context) {
100162
const type = String(ctx.match);
101163
await Permissions.modify(ctx, "unlock", type);
102164
}
165+
103166
@SafeExecution()
167+
@LogExecution()
104168
static async rmWarn(ctx: Context) {
105169
await executeServiceAdmin(ctx, WarnService, "clear");
106170
}
107171

108172
@SafeExecution()
173+
@LogExecution()
109174
static async unMute(ctx: Context) {
110175
await executeServiceAdmin(ctx, MuteService, "unmute");
111176
}
112177

113178
@SafeExecution()
179+
@LogExecution()
114180
static async ban(ctx: Context) {
115181
await executeServiceAdmin(ctx, BanService, "ban");
116182
}
117183

118184
@SafeExecution()
185+
@LogExecution()
119186
static async unBan(ctx: Context) {
120187
await executeServiceAdmin(ctx, BanService, "unban");
121188
}
122189

123190
@SafeExecution()
191+
@LogExecution()
124192
static async abl(ctx: Context) {
125193
await executeService(ctx, BlacklistService, "addBlackList");
126194
}
127195

128196
@SafeExecution()
197+
@LogExecution()
129198
static async blacklist(ctx: Context) {
130199
await executeService(ctx, BlacklistService, "BlackList");
131200
}
132201

133202
@SafeExecution()
203+
@LogExecution()
134204
static async rmbl(ctx: Context) {
135205
await executeService(ctx, BlacklistService, "remove");
136206
}
137207

138208
@SafeExecution()
209+
@LogExecution()
139210
static async date(ctx: Context) {
140211
await executeService(ctx, DateCommand, "date");
141212
}
142213

143214
@SafeExecution()
215+
@LogExecution()
144216
static async rules(ctx: Context) {
145217
await executeService(ctx, Rules, "rules");
146218
}
147219

148220
@SafeExecution()
221+
@LogExecution()
149222
static async approved(ctx: Context) {
150223
await executeService(ctx, Approved, "add");
151224
}
152225

153226
@SafeExecution()
227+
@LogExecution()
154228
static async unApproved(ctx: Context) {
155229
await executeService(ctx, Approved, "remove");
156230
}
231+
232+
@SafeExecution()
233+
@LogExecution()
234+
static async approvedList(ctx: Context) {
235+
await executeService(ctx, Approved, "list");
236+
}
157237
}

0 commit comments

Comments
 (0)