Skip to content

Commit 1e0cece

Browse files
committed
refactor(commands-adminCommands) removed db initialization, added Catch decorators, updated imports and variable references, and added new methods and logic."
1 parent 0b7488f commit 1e0cece

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ServiceProvider } from './service/database/ServiceProvider';
33
import logger from './utils/logger';
44
async function main() {
55
const cop = CopBot.getInstance();
6-
const db = await ServiceProvider.initialize();
6+
await ServiceProvider.initialize();
77
logger.info('initialize Database');
88
await cop.initial();
99
logger.info('initial bot');

src/bot/commands/admin/AdminCommands.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,40 @@ export class AdminCommands {
135135
await reply.markdownReply(`${warns}`);
136136
}
137137
/** Mute Commands */
138+
@Catch()
138139
static async mute(ctx: Context) {
139140
const reply = new BotReply(ctx);
140141
const message = await MuteService.muteUser(ctx);
141142
return await reply.textReply(message);
142143
}
144+
@Catch()
143145
static async unmute(ctx: Context) {
144146
const reply = new BotReply(ctx);
145147
const message = await MuteService.unmuteUser(ctx);
146148
return await reply.textReply(message);
147149
}
148150
/** Admin Command */
151+
@Catch()
149152
static async grant(ctx: Context) {
150153
const reply = new BotReply(ctx);
154+
const chatInfo = new ChatInfo(ctx);
155+
const isOwner = await chatInfo.userIsOwner();
156+
if (isOwner) {
157+
await reply.textReply('The owner of the chat cannot be granted administrator privileges.');
158+
return;
159+
}
151160
const grantUser = await AdminService.grant(ctx);
152161
await reply.textReply(grantUser);
153162
}
163+
@Catch()
154164
static async revoke(ctx: Context) {
155165
const reply = new BotReply(ctx);
166+
const chatInfo = new ChatInfo(ctx);
167+
const isOwner = await chatInfo.userIsOwner();
168+
if (isOwner) {
169+
await reply.textReply("You cannot revoke the owner's rights.");
170+
return;
171+
}
156172
const revokeUser = await AdminService.revoke(ctx);
157173
await reply.textReply(revokeUser);
158174
}

src/bot/commands/genearl/GeneralCommands.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BotReply } from '../../../utils/chat/BotReply';
44
import { help, start, commands } from '../../../utils/jsons/botMessages.json';
55
import { ChatInfo } from '../../../utils/chat/ChatInfo';
66
import { DateCommand } from '../../service/general/date';
7-
import { info, user_support } from '../../../../docs/BotInfo.json';
7+
import * as BotInfoJson from '../../../../docs/BotInfo.json';
88
/**
99
* Reason for lowercase command names:
1010
*
@@ -92,10 +92,10 @@ export class GeneralCommands {
9292

9393
// Contact information from BotInfo.json
9494
const contactMessage = `
95-
**Help Contact**: ${user_support.help_contact}\n\r
96-
**Support Email**: ${user_support.support_email}
95+
**Help Contact**: ${BotInfoJson.user_support.help_contact}\n\r
96+
**Support Email**: ${BotInfoJson.user_support.support_email}
9797
**Support Groups**:
98-
${user_support.support_groups.map((group) => `- ${group.name}: ${group.link}`).join('\n')}
98+
${BotInfoJson.user_support.support_groups.map((group) => `- ${group.name}: ${group.link}`).join('\n')}
9999
100100
Please reach out to us for assistance.
101101
`;
@@ -113,14 +113,14 @@ Please reach out to us for assistance.
113113
// Extract detailed information from BotInfo.json
114114
const botInfoMessage = `
115115
**Bot Information**:
116-
- **Name**: ${info.bot_name}
117-
- **Version**: ${info.bot_version}
118-
- **Status**: ${info.bot_status}
119-
- **Creation Date**: ${new Date(info.created_at).toLocaleString()}
120-
- **Last Updated**: ${new Date(info.last_update).toLocaleString()}
121-
- **Supported Languages**: ${info.supported_languages.join(', ')}
116+
- **Name**: ${BotInfoJson.info.bot_name}
117+
- **Version**: ${BotInfoJson.info.bot_version}
118+
- **Status**: ${BotInfoJson.info.bot_status}
119+
- **Creation Date**: ${new Date(BotInfoJson.info.created_at).toLocaleString()}
120+
- **Last Updated**: ${new Date(BotInfoJson.info.last_update).toLocaleString()}
121+
- **Supported Languages**: ${BotInfoJson.info.supported_languages.join(', ')}
122122
123-
**Description**: ${info.description}
123+
**Description**: ${BotInfoJson.info.description}
124124
125125
This bot is designed to deliver fast and secure responses to users.
126126
`;

src/utils/chat/ChatInfo.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ export class ChatInfo {
5656

5757
return chatMember.status === 'administrator' || chatMember.status === 'creator';
5858
}
59+
async userIsOwner(): Promise<boolean> {
60+
const userId = this._ctx.from!.id;
61+
const chatMember = await this._ctx.getChatMember(userId);
62+
63+
return chatMember.status === 'creator';
64+
}
5965
async userReplyIsAdmin(): Promise<boolean | undefined> {
6066
const reply = new BotReply(this._ctx);
6167
const repliedMessage = await reply.getReplyMessage();

0 commit comments

Comments
 (0)