Splitting code
This commit is contained in:
		
							
								
								
									
										13
									
								
								src/commands.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/commands.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
use teloxide::utils::command::BotCommands;
 | 
			
		||||
#[derive(BotCommands, Clone)]
 | 
			
		||||
#[command(rename_rule = "lowercase", description = "Вот мои команды:")]
 | 
			
		||||
pub enum Command {
 | 
			
		||||
    #[command(description = "Отобразить это сообщение")]
 | 
			
		||||
    Help,
 | 
			
		||||
    #[command(description = "Написать сообщение от третьего лица")]
 | 
			
		||||
    Me(String),
 | 
			
		||||
    #[command(description = "Выдать предупреждение пользователю (только для админов)")]
 | 
			
		||||
    Warn(String),
 | 
			
		||||
    #[command(description = "Снять предупреждения и убрать мут")]
 | 
			
		||||
    Unwarn,
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										73
									
								
								src/godette.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								src/godette.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,73 @@
 | 
			
		||||
use teloxide::{
 | 
			
		||||
    prelude::*, types::ParseMode::MarkdownV2, utils::command::BotCommands, utils::markdown,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use crate::commands::Command;
 | 
			
		||||
 | 
			
		||||
pub struct Godette {
 | 
			
		||||
    pub bot: Bot,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Godette {
 | 
			
		||||
    pub fn new() -> Godette {
 | 
			
		||||
        Godette {
 | 
			
		||||
            bot: Bot::from_env(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn commands_dispatcher(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
 | 
			
		||||
        match cmd {
 | 
			
		||||
            Command::Help => Godette::show_help(bot, msg).await?,
 | 
			
		||||
            Command::Me(quote) => Godette::me(bot, msg, quote).await?,
 | 
			
		||||
            Command::Warn(reason) => Godette::warn(bot, msg, reason).await?,
 | 
			
		||||
            Command::Unwarn => Godette::unwarn(bot, msg).await?,
 | 
			
		||||
        };
 | 
			
		||||
        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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										77
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								src/main.rs
									
									
									
									
									
								
							@ -1,3 +1,6 @@
 | 
			
		||||
mod commands;
 | 
			
		||||
mod godette;
 | 
			
		||||
 | 
			
		||||
use teloxide::{
 | 
			
		||||
    prelude::*, types::ParseMode::MarkdownV2, utils::command::BotCommands, utils::markdown,
 | 
			
		||||
};
 | 
			
		||||
@ -6,71 +9,11 @@ use teloxide::{
 | 
			
		||||
async fn main() {
 | 
			
		||||
    pretty_env_logger::init();
 | 
			
		||||
 | 
			
		||||
    let bot = Bot::from_env();
 | 
			
		||||
    teloxide::commands_repl(bot, answer, Command::ty()).await;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(BotCommands, Clone)]
 | 
			
		||||
#[command(rename_rule = "lowercase", description = "Вот мои команды:")]
 | 
			
		||||
enum Command {
 | 
			
		||||
    #[command(description = "Отобразить это сообщение")]
 | 
			
		||||
    Help,
 | 
			
		||||
    #[command(description = "Написать сообщение от третьего лица")]
 | 
			
		||||
    Me(String),
 | 
			
		||||
    #[command(description = "Выдать предупреждение пользователю (только для админов)")]
 | 
			
		||||
    Warn(String),
 | 
			
		||||
    #[command(description = "Снять предупреждения и убрать мут")]
 | 
			
		||||
    Unwarn,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn answer(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
 | 
			
		||||
    match cmd {
 | 
			
		||||
        Command::Help => {
 | 
			
		||||
            bot.send_message(msg.chat.id, Command::descriptions().to_string())
 | 
			
		||||
                .await?
 | 
			
		||||
        }
 | 
			
		||||
        Command::Me(quote) => {
 | 
			
		||||
            let user = msg.from().unwrap().to_owned();
 | 
			
		||||
            let username = user.username.unwrap_or("noname".to_owned());
 | 
			
		||||
            let esc_username = markdown::escape(&username);
 | 
			
		||||
            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?
 | 
			
		||||
        }
 | 
			
		||||
        Command::Warn(reason) => match msg.reply_to_message() {
 | 
			
		||||
            Some(guilty) => {
 | 
			
		||||
                let username = guilty
 | 
			
		||||
                    .from()
 | 
			
		||||
                    .unwrap()
 | 
			
		||||
                    .to_owned()
 | 
			
		||||
                    .username
 | 
			
		||||
                    .unwrap_or("noname".to_string());
 | 
			
		||||
                let username_formatted = markdown::bold(&username);
 | 
			
		||||
                let reason_formatted = markdown::italic(&markdown::escape(&reason));
 | 
			
		||||
                let text = format!(
 | 
			
		||||
                    "{username_formatted} получил предупреждение по причине \"{reason_formatted}\""
 | 
			
		||||
                );
 | 
			
		||||
                bot.send_message(msg.chat.id, text)
 | 
			
		||||
                    .parse_mode(MarkdownV2)
 | 
			
		||||
                    .await?
 | 
			
		||||
            }
 | 
			
		||||
            None => {
 | 
			
		||||
                bot.send_message(
 | 
			
		||||
                    msg.chat.id,
 | 
			
		||||
                    "Используйте эту команду как ответ на сообщение, требующее действий."
 | 
			
		||||
                        .to_string(),
 | 
			
		||||
                )
 | 
			
		||||
                .await?
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
        Command::Unwarn => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "Это разбан".to_string())
 | 
			
		||||
                .await?
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    Ok(())
 | 
			
		||||
    let bot = godette::Godette::new();
 | 
			
		||||
    teloxide::commands_repl(
 | 
			
		||||
        bot.bot,
 | 
			
		||||
        godette::Godette::commands_dispatcher,
 | 
			
		||||
        commands::Command::ty(),
 | 
			
		||||
    )
 | 
			
		||||
    .await;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user