use async_trait::async_trait; use telers::{ errors::{EventErrorKind, HandlerError}, event::telegram::{HandlerRequest, HandlerResponse}, middlewares::{InnerMiddleware, Next}, }; use anyhow::Error as Reject; use crate::{ types::structs::message_sender::MessageSender, utils::telegram::{admin_check::is_admin, data_getters::get_all_admins}, }; #[derive(Default)] pub struct AdminCheck {} #[async_trait] impl InnerMiddleware for AdminCheck { async fn call( &self, request: HandlerRequest, next: Next, ) -> Result { let (bot, message) = (request.clone().bot, request.clone().update); let admins_list = get_all_admins(&bot, message.chat().unwrap().id()) .await .unwrap(); let chat_id: i64 = message.chat_id().unwrap(); if is_admin(&admins_list, message.from().unwrap().id) { let response = next(request).await?; return Ok(response); } else { MessageSender::builder(chat_id) .text("Недостаточно прав для использования данной команды.") .build() .send(&bot) .await .unwrap(); return Err(EventErrorKind::Handler(HandlerError::new(Reject::msg( "User isn't admin.".to_string(), )))); } } }