use teloxide::prelude::*; mod database; use std::error::Error; use std::fmt; pub fn check_media(msg: Message) -> bool { check_gif(msg.clone()) || check_stiker(msg.clone()) || check_photo(msg.clone()) } pub fn check_stiker(msg: Message) -> bool { let stiker = match msg.sticker() { Some(s) => s.file.clone(), None => return false, }; let db = database::Database::get_database(); db.media_is_banned(stiker.unique_id.as_str(), database::T_STIKER) } pub fn check_gif(msg: Message) -> bool { let gif = match msg.animation() { Some(s) => s.file.clone(), None => return false, }; let db = database::Database::get_database(); db.media_is_banned(gif.unique_id.as_str(), database::T_GIF) } pub fn check_photo(msg: Message) -> bool { match msg.photo() { Some(s) => { let db = database::Database::get_database(); for p in s { if db.media_is_banned(p.file.unique_id.as_str(), database::T_PHOTO) { return true; } } false } None => false, } } pub async fn ban_media(msg: Message, bot: Bot) -> anyhow::Result<()> { if !is_admin(msg.clone(), bot).await { return get_error("No tienes permiso para hacer esto"); } match msg.reply_to_message() { Some(s) => { _ = insert_ban_stiker((*s).clone()); _ = insert_ban_gif((*s).clone()); _ = insert_ban_photo((*s).clone()); Ok(()) } None => get_error("No has seleccionado nada"), } } fn insert_ban_stiker(msg: Message) -> anyhow::Result<()> { match msg.sticker() { Some(s) => { let db = database::Database::get_database(); match db.add_media( s.file.unique_id.as_str(), msg.chat.id.to_string().as_str(), database::T_STIKER, ) { true => Ok(()), false => get_error("Stiker ya quemado"), } } None => Ok(()), } } fn insert_ban_gif(msg: Message) -> anyhow::Result<()> { match msg.animation() { Some(s) => { let db = database::Database::get_database(); match db.add_media( s.file.unique_id.as_str(), msg.chat.id.to_string().as_str(), database::T_GIF, ) { true => Ok(()), false => get_error("Gif ya quemado"), } } None => Ok(()), } } fn insert_ban_photo(msg: Message) -> anyhow::Result<()> { match msg.photo() { Some(s) => { let db = database::Database::get_database(); for p in s { match db.add_media( p.file.unique_id.as_str(), msg.chat.id.to_string().as_str(), database::T_PHOTO, ) { true => {} false => return get_error("Foto ya quemado"), } } } None => {} } Ok(()) } async fn is_admin(msg: Message, bot: Bot) -> bool { match bot .get_chat_member(msg.chat.id, msg.from().unwrap().id) .send() .await { Ok(k) => k.kind.is_privileged(), Err(_e) => false, } } #[derive(Debug)] struct ErrorBanStiker { message: String, } impl fmt::Display for ErrorBanStiker { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.message) } } impl Error for ErrorBanStiker {} fn get_error(msg: &str) -> anyhow::Result<()> { Err(anyhow::Error::new(ErrorBanStiker { message: String::from(msg), })) }