Admin commands, minor refactoring
This commit is contained in:
		@ -1,8 +1,8 @@
 | 
				
			|||||||
use teloxide::{
 | 
					use teloxide::prelude::*;
 | 
				
			||||||
    prelude::*, types::ParseMode::MarkdownV2, utils::command::BotCommands, utils::markdown,
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::commands::Command;
 | 
					pub mod commands;
 | 
				
			||||||
 | 
					mod handlers;
 | 
				
			||||||
 | 
					use commands::{AdminCommand, Command};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Godette {
 | 
					pub struct Godette {
 | 
				
			||||||
    pub bot: Bot,
 | 
					    pub bot: Bot,
 | 
				
			||||||
@ -17,10 +17,27 @@ impl Godette {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    pub async fn commands_dispatcher(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
 | 
					    pub async fn commands_dispatcher(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
 | 
				
			||||||
        match cmd {
 | 
					        match cmd {
 | 
				
			||||||
            Command::Help => Godette::show_help(bot, msg).await?,
 | 
					            Command::Help => handlers::show_help(bot, msg).await?,
 | 
				
			||||||
            Command::Me(quote) => Godette::me(bot, msg, quote).await?,
 | 
					            Command::Me(quote) => handlers::me(bot, msg, quote).await?,
 | 
				
			||||||
            Command::Warn(reason) => Godette::warn(bot, msg, reason).await?,
 | 
					        };
 | 
				
			||||||
            Command::Unwarn => Godette::unwarn(bot, msg).await?,
 | 
					        Ok(())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub async fn admin_dispatcher(bot: Bot, msg: Message, cmd: AdminCommand) -> ResponseResult<()> {
 | 
				
			||||||
 | 
					        let mut is_admin = true;
 | 
				
			||||||
 | 
					        if msg.chat.is_group() || msg.chat.is_supergroup() {
 | 
				
			||||||
 | 
					            let sender = msg.from().unwrap();
 | 
				
			||||||
 | 
					            let admins = bot.get_chat_administrators(msg.chat.id).await?;
 | 
				
			||||||
 | 
					            is_admin = admins.iter().any(|member| member.user.id == sender.id);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if !is_admin {
 | 
				
			||||||
 | 
					            return Ok(());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        match cmd {
 | 
				
			||||||
 | 
					            AdminCommand::HelpAdmin => handlers::show_adminhelp(bot, msg).await?,
 | 
				
			||||||
 | 
					            AdminCommand::Say(quote) => handlers::say(bot, msg, quote).await?,
 | 
				
			||||||
 | 
					            AdminCommand::Warn(reason) => handlers::warn(bot, msg, reason).await?,
 | 
				
			||||||
 | 
					            AdminCommand::Unwarn => handlers::unwarn(bot, msg).await?,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -31,55 +48,11 @@ impl Godette {
 | 
				
			|||||||
            .unwrap_or(msg.caption().unwrap_or_default())
 | 
					            .unwrap_or(msg.caption().unwrap_or_default())
 | 
				
			||||||
            .to_string();
 | 
					            .to_string();
 | 
				
			||||||
        match text.to_lowercase().find("спасибо") {
 | 
					        match text.to_lowercase().find("спасибо") {
 | 
				
			||||||
            Some(_id) => bot.send_message(msg.chat.id, "Не за что!").await?,
 | 
					            Some(_id) => {
 | 
				
			||||||
            None => todo!(),
 | 
					                bot.send_message(msg.chat.id, "Не за что!").await?;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            None => (),
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn show_help(bot: Bot, msg: Message) -> ResponseResult<Message> {
 | 
					 | 
				
			||||||
        bot.send_message(msg.chat.id, Command::descriptions().to_string())
 | 
					 | 
				
			||||||
            .await
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn me(bot: Bot, msg: Message, quote: String) -> ResponseResult<Message> {
 | 
					 | 
				
			||||||
        let name = msg.from().unwrap().to_owned().full_name();
 | 
					 | 
				
			||||||
        let esc_username = markdown::escape(&name);
 | 
					 | 
				
			||||||
        let esc_quote = markdown::escape("e);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        let text = format!("*_{esc_username}_* {esc_quote}").to_string();
 | 
					 | 
				
			||||||
        bot.delete_message(msg.chat.id, msg.id).await?;
 | 
					 | 
				
			||||||
        bot.send_message(msg.chat.id, text)
 | 
					 | 
				
			||||||
            .parse_mode(MarkdownV2)
 | 
					 | 
				
			||||||
            .await
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn warn(bot: Bot, msg: Message, reason: String) -> ResponseResult<Message> {
 | 
					 | 
				
			||||||
        match msg.reply_to_message() {
 | 
					 | 
				
			||||||
            Some(guilty) => {
 | 
					 | 
				
			||||||
                let username = guilty.from().unwrap().to_owned().full_name();
 | 
					 | 
				
			||||||
                let username_formatted = markdown::bold(&markdown::escape(&username));
 | 
					 | 
				
			||||||
                let reason_formatted = markdown::italic(&markdown::escape(&reason));
 | 
					 | 
				
			||||||
                let text = format!(
 | 
					 | 
				
			||||||
                    "{username_formatted} получил предупреждение по причине:\n\"{reason_formatted}\""
 | 
					 | 
				
			||||||
                );
 | 
					 | 
				
			||||||
                bot.send_message(msg.chat.id, text)
 | 
					 | 
				
			||||||
                    .parse_mode(MarkdownV2)
 | 
					 | 
				
			||||||
                    .await
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            None => {
 | 
					 | 
				
			||||||
                bot.send_message(
 | 
					 | 
				
			||||||
                    msg.chat.id,
 | 
					 | 
				
			||||||
                    "Используйте эту команду как ответ на сообщение, требующее действий."
 | 
					 | 
				
			||||||
                        .to_string(),
 | 
					 | 
				
			||||||
                )
 | 
					 | 
				
			||||||
                .await
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async fn unwarn(bot: Bot, msg: Message) -> ResponseResult<Message> {
 | 
					 | 
				
			||||||
        bot.send_message(msg.chat.id, "Это разбан".to_string())
 | 
					 | 
				
			||||||
            .await
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -6,8 +6,17 @@ pub enum Command {
 | 
				
			|||||||
    Help,
 | 
					    Help,
 | 
				
			||||||
    #[command(description = "Написать сообщение от третьего лица")]
 | 
					    #[command(description = "Написать сообщение от третьего лица")]
 | 
				
			||||||
    Me(String),
 | 
					    Me(String),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(BotCommands, Clone)]
 | 
				
			||||||
 | 
					#[command(rename_rule = "lowercase", description = "Админские команды:")]
 | 
				
			||||||
 | 
					pub enum AdminCommand {
 | 
				
			||||||
 | 
					    #[command(description = "Отобразить эту помощь")]
 | 
				
			||||||
 | 
					    HelpAdmin,
 | 
				
			||||||
    #[command(description = "Выдать предупреждение пользователю (только для админов)")]
 | 
					    #[command(description = "Выдать предупреждение пользователю (только для админов)")]
 | 
				
			||||||
    Warn(String),
 | 
					    Warn(String),
 | 
				
			||||||
    #[command(description = "Снять предупреждения и убрать мут")]
 | 
					    #[command(description = "Снять предупреждения и убрать мут")]
 | 
				
			||||||
    Unwarn,
 | 
					    Unwarn,
 | 
				
			||||||
 | 
					    #[command(description = "Сказать от моего лица")]
 | 
				
			||||||
 | 
					    Say(String),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										68
									
								
								src/godette/handlers.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/godette/handlers.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,68 @@
 | 
				
			|||||||
 | 
					use teloxide::{
 | 
				
			||||||
 | 
					    payloads::SendMessageSetters,
 | 
				
			||||||
 | 
					    prelude::*,
 | 
				
			||||||
 | 
					    types::ParseMode::MarkdownV2,
 | 
				
			||||||
 | 
					    utils::command::BotCommands,
 | 
				
			||||||
 | 
					    utils::markdown::{self, bold, escape, italic},
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::commands::{AdminCommand, Command};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub async fn show_help(bot: Bot, msg: Message) -> ResponseResult<Message> {
 | 
				
			||||||
 | 
					    bot.send_message(msg.chat.id, Command::descriptions().to_string())
 | 
				
			||||||
 | 
					        .await
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					pub async fn show_adminhelp(bot: Bot, msg: Message) -> ResponseResult<Message> {
 | 
				
			||||||
 | 
					    bot.send_message(msg.chat.id, AdminCommand::descriptions().to_string())
 | 
				
			||||||
 | 
					        .await
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub async fn me(bot: Bot, msg: Message, quote: String) -> ResponseResult<Message> {
 | 
				
			||||||
 | 
					    let name = msg.from().unwrap().to_owned().full_name();
 | 
				
			||||||
 | 
					    let esc_username = markdown::escape(&name);
 | 
				
			||||||
 | 
					    let esc_quote = markdown::escape("e);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let text = format!("*_{esc_username}_* {esc_quote}").to_string();
 | 
				
			||||||
 | 
					    bot.delete_message(msg.chat.id, msg.id).await?;
 | 
				
			||||||
 | 
					    bot.send_message(msg.chat.id, text)
 | 
				
			||||||
 | 
					        .parse_mode(MarkdownV2)
 | 
				
			||||||
 | 
					        .await
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub async fn say(bot: Bot, msg: Message, quote: String) -> ResponseResult<Message> {
 | 
				
			||||||
 | 
					    let text = bold(&italic(&escape("e)));
 | 
				
			||||||
 | 
					    bot.delete_message(msg.chat.id, msg.id).await?;
 | 
				
			||||||
 | 
					    let message = bot.send_message(msg.chat.id, text).parse_mode(MarkdownV2);
 | 
				
			||||||
 | 
					    match msg.reply_to_message() {
 | 
				
			||||||
 | 
					        Some(reply) => message.reply_to_message_id(reply.id).await,
 | 
				
			||||||
 | 
					        None => message.await,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub async fn warn(bot: Bot, msg: Message, reason: String) -> ResponseResult<Message> {
 | 
				
			||||||
 | 
					    match msg.reply_to_message() {
 | 
				
			||||||
 | 
					        Some(guilty) => {
 | 
				
			||||||
 | 
					            let username = guilty.from().unwrap().to_owned().full_name();
 | 
				
			||||||
 | 
					            let username_formatted = markdown::bold(&markdown::escape(&username));
 | 
				
			||||||
 | 
					            let reason_formatted = markdown::italic(&markdown::escape(&reason));
 | 
				
			||||||
 | 
					            let text = format!(
 | 
				
			||||||
 | 
					                "{username_formatted} получил предупреждение по причине:\n\"{reason_formatted}\""
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					            bot.send_message(msg.chat.id, text)
 | 
				
			||||||
 | 
					                .parse_mode(MarkdownV2)
 | 
				
			||||||
 | 
					                .await
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        None => {
 | 
				
			||||||
 | 
					            bot.send_message(
 | 
				
			||||||
 | 
					                msg.chat.id,
 | 
				
			||||||
 | 
					                "Используйте эту команду как ответ на сообщение, требующее действий.".to_string(),
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .await
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub async fn unwarn(bot: Bot, msg: Message) -> ResponseResult<Message> {
 | 
				
			||||||
 | 
					    bot.send_message(msg.chat.id, "Это разбан".to_string())
 | 
				
			||||||
 | 
					        .await
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,8 +1,8 @@
 | 
				
			|||||||
mod commands;
 | 
					 | 
				
			||||||
mod godette;
 | 
					mod godette;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use teloxide::prelude::*;
 | 
					use teloxide::prelude::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use godette::commands;
 | 
				
			||||||
use godette::Godette;
 | 
					use godette::Godette;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[tokio::main]
 | 
					#[tokio::main]
 | 
				
			||||||
@ -16,6 +16,11 @@ async fn main() {
 | 
				
			|||||||
                .filter_command::<commands::Command>()
 | 
					                .filter_command::<commands::Command>()
 | 
				
			||||||
                .endpoint(Godette::commands_dispatcher),
 | 
					                .endpoint(Godette::commands_dispatcher),
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					        .branch(
 | 
				
			||||||
 | 
					            dptree::entry()
 | 
				
			||||||
 | 
					                .filter_command::<commands::AdminCommand>()
 | 
				
			||||||
 | 
					                .endpoint(Godette::admin_dispatcher),
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        .branch(
 | 
					        .branch(
 | 
				
			||||||
            dptree::filter(|msg: Message| {
 | 
					            dptree::filter(|msg: Message| {
 | 
				
			||||||
                msg.from()
 | 
					                msg.from()
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user