Now the admin_check function gets a second argument in the form of user_id, and this function is used in try_restrict

This commit is contained in:
doryan 2024-06-08 03:12:16 +04:00
parent 0ec0c94501
commit 7b31cccf31
2 changed files with 18 additions and 6 deletions

View File

@ -1,11 +1,11 @@
use telers::types::{chat_member::ChatMember, User}; use telers::types::chat_member::ChatMember;
pub fn is_admin(all_admin_members: &Vec<ChatMember>, user: &User) -> bool { pub fn is_admin(all_admin_members: &Vec<ChatMember>, user_id: i64) -> bool {
all_admin_members all_admin_members
.iter() .iter()
.any(|admin: &ChatMember| match admin { .any(|admin: &ChatMember| match admin {
ChatMember::Administrator(admin) => &admin.user == user, ChatMember::Administrator(admin) => admin.user.id == user_id,
ChatMember::Owner(owner) => &owner.user == user, ChatMember::Owner(owner) => owner.user.id == user_id,
_ => false, _ => false,
}) })
} }

View File

@ -1,9 +1,9 @@
use telers::{errors::SessionErrorKind as ErrorKind, event::EventReturn, Bot}; use telers::{errors::SessionErrorKind as ErrorKind, event::EventReturn, types::ChatMember, Bot};
use crate::types::structs::message_sender::MessageSender; use crate::types::structs::message_sender::MessageSender;
use std::future::Future; use std::future::Future;
use super::demote::demote_user; use super::{admin_check::is_admin, demote::demote_user, get_all_admins::get_all_admins};
const DEMOTE_FAILURE_MESSAGE: &str = "Команда не может быть выполнена: \ const DEMOTE_FAILURE_MESSAGE: &str = "Команда не может быть выполнена: \
не удалось удалить административные привилегии пользователя."; не удалось удалить административные привилегии пользователя.";
@ -19,6 +19,18 @@ where
{ {
let (bot, user_id, chat_id): (&Bot, i64, i64) = demote_args; let (bot, user_id, chat_id): (&Bot, i64, i64) = demote_args;
let admins: Vec<ChatMember> = get_all_admins(bot, chat_id).await.unwrap();
if is_admin(&admins, user_id) {
MessageSender::builder(chat_id)
.text("Нельзя применить эту команду на администраторе или бота.")
.build()
.send(bot)
.await
.unwrap();
return Err(EventReturn::Cancel);
}
if future_callback().await.is_err() { if future_callback().await.is_err() {
if demote_user(bot, user_id, chat_id).await.is_err() { if demote_user(bot, user_id, chat_id).await.is_err() {
MessageSender::builder(chat_id) MessageSender::builder(chat_id)