fix(bot): сделать проверку isAdmin асинхронной и добавить логирование ошибок

- Исправлен метод isAdmin на асинхронный с ожиданием результата
- Добавлен вывод ошибок в лог при проверке статуса пользователя
- В хэндлере команды /summy добавлена проверка прав администратора с удалением сообщения, если пользователь не админ
This commit is contained in:
Vufer 2025-06-30 20:41:54 +03:00
parent 523c533aea
commit c0cda72db5

12
bot.js
View File

@ -29,14 +29,16 @@ class TelegramHistoryBot {
this.init(); this.init();
} }
isAdmin (ctx, userId = ctx.from.id) { async isAdmin(ctx, userId = ctx.from.id) {
try { try {
const member = ctx.getChatMember(userId) const member = await ctx.getChatMember(userId)
return ['creator', 'administrator'].includes(member.status) return ['creator', 'administrator'].includes(member.status)
} catch { } catch (error) {
logger.error('Ошибка проверки статуса пользователя:', error)
return false return false
} }
} }
async init() { async init() {
try { try {
await this.loadHistory(); await this.loadHistory();
@ -70,6 +72,10 @@ class TelegramHistoryBot {
setupHandlers() { setupHandlers() {
this.bot.command('summy', async (ctx) => { this.bot.command('summy', async (ctx) => {
if (!(await this.isAdmin(ctx))) {
await ctx.deleteMessage()
return
}
const message = ctx.message.text || ''; const message = ctx.message.text || '';
const args = message.replace(/^\/summy(@\w+)?\s*/, ''); // удаляет /summy и возможный @botname const args = message.replace(/^\/summy(@\w+)?\s*/, ''); // удаляет /summy и возможный @botname
const trimmed = args.trim(); // удаляет лишние пробелы по краям, если нужно const trimmed = args.trim(); // удаляет лишние пробелы по краям, если нужно