clean warnings and add tests

This commit is contained in:
2025-05-11 21:20:05 +02:00
parent baf7ba2ffb
commit 91f6c5f9f9
14 changed files with 262 additions and 166 deletions

View File

@@ -4,9 +4,7 @@ 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())
check_gif(msg.clone()) || check_stiker(msg.clone()) || check_photo(msg.clone())
}
pub fn check_stiker(msg: Message) -> bool {
@@ -15,7 +13,7 @@ pub fn check_stiker(msg: Message) -> bool {
None => return false,
};
let db = database::Database::get_database();
db.media_is_banned(stiker.unique_id.as_str(),database::T_STIKER)
db.media_is_banned(stiker.unique_id.as_str(), database::T_STIKER)
}
pub fn check_gif(msg: Message) -> bool {
@@ -24,7 +22,7 @@ pub fn check_gif(msg: Message) -> bool {
None => return false,
};
let db = database::Database::get_database();
db.media_is_banned(gif.unique_id.as_str(),database::T_GIF)
db.media_is_banned(gif.unique_id.as_str(), database::T_GIF)
}
pub fn check_photo(msg: Message) -> bool {
@@ -32,30 +30,27 @@ pub fn check_photo(msg: Message) -> bool {
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) {
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<()>{
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());
_ = insert_ban_stiker((*s).clone());
_ = insert_ban_gif((*s).clone());
_ = insert_ban_photo((*s).clone());
Ok(())
},
}
None => get_error("No has seleccionado nada"),
}
}
@@ -64,15 +59,15 @@ 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"),
}
},
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(()),
}
}
@@ -81,15 +76,15 @@ 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"),
}
},
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(()),
}
}
@@ -98,23 +93,28 @@ 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 => {},
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 => {},
}
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 {
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,
}
@@ -133,7 +133,8 @@ impl fmt::Display for ErrorBanStiker {
impl Error for ErrorBanStiker {}
fn get_error(msg: &str)-> anyhow::Result<()> {
Err(anyhow::Error::new(ErrorBanStiker{message: String::from(msg),}))
}
fn get_error(msg: &str) -> anyhow::Result<()> {
Err(anyhow::Error::new(ErrorBanStiker {
message: String::from(msg),
}))
}