Skip to content

Commit

Permalink
fix: now only allow admins to call commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bartick committed Jun 16, 2024
1 parent 7eec982 commit ab1a4da
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions telebot/src/connection.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
},
_ => {}
}
}
}
Expand Down

0 comments on commit ab1a4da

Please sign in to comment.