Skip to content

Commit 1241805

Browse files
committed
feat(admin-commands): implement approval and disapproval commands for group management
- Added `approved` command for approving users in a group, using the `updateApproved` method from `ApprovedService`. - Added `disapproved` command for removing approval of users in a group, leveraging the `updateDisapproved` method from `ApprovedService`. - Added `approvedlist` command to list all approved users in a group, using the `getApprovedUsers` method from `ApprovedService`. **Improvements**: - Introduced validation using `AdminValidationService` to ensure proper context handling in commands. - Utilized `BotReply` for uniform and consistent bot replies to users. **Error Handling**: - Wrapped commands with the `@Catch` decorator to handle unexpected errors gracefully and provide user-friendly messages.
1 parent 1a34b38 commit 1241805

9 files changed

Lines changed: 239 additions & 399 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { Context } from 'grammy';
2+
import { BotReply } from '../../../utils/chat/BotReply';
3+
import { Catch } from '../../../decorators/Catch';
4+
import { AdminValidationService } from '../../service/admin/validation';
5+
import { ApprovedService } from '../../service/admin/Approved';
6+
7+
export class AdminCommands {
8+
/** Approved Commands */
9+
@Catch({
10+
category: 'Admin Command',
11+
message: 'An unexpected error occurred while processing the "approved" command. Please try again later.',
12+
statusCode: 500,
13+
})
14+
static async approved(ctx: Context) {
15+
const reply = new BotReply(ctx);
16+
const validationResult = await AdminValidationService.validateApprovalContext(ctx);
17+
18+
if (!validationResult) {
19+
return;
20+
}
21+
const { groupId, userId } = validationResult;
22+
await ApprovedService.updateApproved(ctx, groupId, userId);
23+
await reply.textReply(`User with ID ${userId} has been successfully approved for group ${groupId}.`);
24+
}
25+
@Catch({
26+
category: 'Admin Command',
27+
message: 'An unexpected error occurred while processing the "disapproved" command. Please try again later.',
28+
statusCode: 500,
29+
})
30+
static async disapproved(ctx: Context) {
31+
const reply = new BotReply(ctx);
32+
// Validate the context to extract required group and user IDs
33+
const validationResult = await AdminValidationService.validateApprovalContext(ctx);
34+
35+
if (!validationResult) {
36+
return;
37+
}
38+
39+
const { groupId, userId } = validationResult;
40+
await ApprovedService.updateDisapproved(ctx,groupId, userId);
41+
await reply.textReply(`User with ID ${userId} has been successfully disapproved for group ${groupId}.`);
42+
}
43+
@Catch({
44+
category: 'Admin Command',
45+
message: 'An unexpected error occurred while processing the "approvedlist" command. Please try again later.',
46+
statusCode: 500,
47+
})
48+
static async approvedlist(ctx: Context) {
49+
const reply = new BotReply(ctx);
50+
const chat = ctx.chat;
51+
if (!(chat?.type === 'group' || chat?.type === 'supergroup')) {
52+
await ctx.reply('This command can only be used in groups.');
53+
return;
54+
}
55+
56+
const groupId = chat.id;
57+
const approvedUsers = await ApprovedService.getApprovedUsers(ctx, groupId);
58+
if (!approvedUsers.length) {
59+
await reply.textReply('No users are approved in this group.');
60+
return;
61+
}
62+
const approvedListMessage = approvedUsers.map((user) => `ID: ${user.telegram_id},\nFirstname: ${user.first_name}`).join('\n');
63+
await reply.textReply(`Approved users in this group:\n${approvedListMessage}`);
64+
}
65+
/** Ban Commands */
66+
static async ban() {}
67+
static async unban() {}
68+
/** Warn Commands */
69+
static async warn() {}
70+
static async rmwarn() {}
71+
static async warns() {}
72+
static async warnslist() {}
73+
/** Mute Commands */
74+
static async mute() {}
75+
static async unmute() {}
76+
static async mutelist() {}
77+
/** Admin Command */
78+
static async grant() {}
79+
static async revoke() {}
80+
/** BlackList Command */
81+
static async blacklist() {}
82+
static async rmbl() {}
83+
static async abl() {}
84+
/** Rules Commands */
85+
/** add-rule|edit-rule|delete-rule|delete_last-rule|view-rule| */
86+
static async rules() {}
87+
/** Pin Command */
88+
static async pin() {}
89+
static async unpin() {}
90+
/** Purge Command */
91+
static async purge() {}
92+
/** Group Setting Command */
93+
static async lock() {}
94+
static async unlock() {}
95+
static async title() {}
96+
static async welcome() {}
97+
98+
/** General Commands */
99+
static async group_stats() {}
100+
}

src/bot/commands/genearl/date.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/bot/commands/user/ReportCommand.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/bot/service/admin/Approved.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { Context } from 'grammy';
2+
import { ServiceProvider } from '../../../service/database/ServiceProvider';
3+
export class ApprovedService {
4+
static async updateApproved(ctx: Context, groupId: number, userId: number) {
5+
const { groupService, userService } = await ApprovedService.getServices();
6+
let user = await userService.getByTelegramId(userId);
7+
let group = await groupService.getByGroupId(groupId);
8+
const userData = { first_name: ctx!.message?.reply_to_message?.from?.first_name!, id: userId, username: ctx.message?.reply_to_message?.from?.username! };
9+
if (!user) {
10+
user = await userService.save(userData);
11+
}
12+
if (!group) {
13+
group = await groupService.save(ctx);
14+
}
15+
const approvedGroups = user.approved_groups ? [...user.approved_groups] : [];
16+
const approvedUsers = group.approved_users ? [...group.approved_users.map(Number)] : [];
17+
18+
if (!approvedUsers.includes(userId)) {
19+
approvedUsers.push(userId);
20+
}
21+
if (!approvedGroups.includes(groupId)) {
22+
approvedGroups.push(groupId);
23+
}
24+
const updatedUser = await userService.update({
25+
...user,
26+
approved_groups: approvedGroups,
27+
});
28+
const updatedGroup = await groupService.update({
29+
...group,
30+
approved_users: approvedUsers,
31+
});
32+
return { updatedUser, updatedGroup };
33+
}
34+
static async updateDisapproved(ctx: Context, groupId: number, userId: number) {
35+
const { groupService, userService } = await ApprovedService.getServices();
36+
// Fetch user and group data
37+
let user = await userService.getByTelegramId(userId);
38+
let group = await groupService.getByGroupId(groupId);
39+
const userData = { first_name: ctx!.message?.reply_to_message?.from?.first_name!, id: userId, username: ctx.message?.reply_to_message?.from?.username! };
40+
if (!user) {
41+
user = await userService.save(userData);
42+
}
43+
if (!group) {
44+
group = await groupService.save(ctx);
45+
}
46+
// Update disapproved lists
47+
const approvedGroups = user.approved_groups ? [...user.approved_groups] : [];
48+
const approvedUsers = group.approved_users ? [...group.approved_users.map(Number)] : [];
49+
50+
// Remove group from user's approved groups
51+
const updatedApprovedGroups = approvedGroups.filter((id) => id !== groupId);
52+
53+
// Remove user from group's approved users
54+
const updatedApprovedUsers = approvedUsers.filter((id) => id !== userId);
55+
56+
// Update the user and group in the database
57+
const updatedUser = await userService.update({
58+
...user,
59+
approved_groups: updatedApprovedGroups,
60+
});
61+
const updatedGroup = await groupService.update({
62+
...group,
63+
approved_users: updatedApprovedUsers,
64+
});
65+
66+
return { updatedUser, updatedGroup };
67+
}
68+
static async getApprovedUsers(ctx: Context, groupId: number) {
69+
const { groupService, userService } = await ApprovedService.getServices();
70+
let group = await groupService.getByGroupId(groupId);
71+
72+
if (!group) {
73+
group = await groupService.save(ctx);
74+
}
75+
const approvedUserIds = group.approved_users ? group.approved_users.map(Number) : [];
76+
const approvedUsers = [];
77+
for (const userId of approvedUserIds) {
78+
const user = await userService.getByTelegramId(userId);
79+
if (user) {
80+
approvedUsers.push(user);
81+
}
82+
}
83+
console.log('approvedUsers', approvedUsers);
84+
85+
return approvedUsers;
86+
}
87+
private static async getServices() {
88+
const service = ServiceProvider.getInstance();
89+
const userService = await service.getUserService();
90+
const groupService = await service.getGroupService();
91+
return {
92+
userService,
93+
groupService,
94+
};
95+
}
96+
}

0 commit comments

Comments
 (0)