diff --git a/telebot/src/connection.rs b/telebot/src/connection.rs index 1c241ff..585c2af 100644 --- a/telebot/src/connection.rs +++ b/telebot/src/connection.rs @@ -1,6 +1,6 @@ -use teloxide::{prelude::*, types::{ChatKind, PublicChatKind}}; +use teloxide::{prelude::Message, requests::Requester, types::{ChatKind, PublicChatKind}}; -use crate::handlers::chat::{private_chat_handler, public_chat_channel_handler, public_chat_group_handler, public_chat_supergroup_handler}; +use crate::{constants::BOT, handlers::chat::{private_chat_handler, public_chat_supergroup_handler}}; /** * Run the bot @@ -13,29 +13,35 @@ use crate::handlers::chat::{private_chat_handler, public_chat_channel_handler, p * * @description This function is used to run the bot */ -pub async fn run(token: String) { - let bot = Bot::new(token); +pub async fn run() { println!("Running telegram bot..."); - teloxide::repl(bot, |bot: Bot, message: Message| async move { + let me = BOT.get_me().await.unwrap(); + + println!("Logged in as: {}", me.username.clone().expect("Unable to find user")); + + teloxide::repl(BOT.clone(), |message: Message| async move { let channel_type = message.clone().chat.kind; match channel_type { ChatKind::Private(_) => { - private_chat_handler(bot, message).await; + private_chat_handler(message).await; }, ChatKind::Public(ctx) => { let public_type = ctx.kind; + + // check if the user is admin or not + let is_admin = BOT.get_chat_administrators(message.chat.id).await.unwrap().iter().any(|admin| admin.user.id == message.from().unwrap().id); + + if !is_admin { + return Ok(()); + } + match public_type { - PublicChatKind::Channel(_) => { - public_chat_channel_handler(bot, message).await; - }, - PublicChatKind::Group(_) => { - public_chat_group_handler(bot, message).await; - }, PublicChatKind::Supergroup(_) => { - public_chat_supergroup_handler(bot, message).await; - } + public_chat_supergroup_handler(message).await; + }, + _ => {} } } }