Admin commands, minor refactoring

This commit is contained in:
Nefrace 2022-10-11 22:59:37 +03:00
parent 9ebd181161
commit b03d76c494
4 changed files with 112 additions and 57 deletions

View File

@ -1,8 +1,8 @@
use teloxide::{
prelude::*, types::ParseMode::MarkdownV2, utils::command::BotCommands, utils::markdown,
};
use teloxide::prelude::*;
use crate::commands::Command;
pub mod commands;
mod handlers;
use commands::{AdminCommand, Command};
pub struct Godette {
pub bot: Bot,
@ -17,10 +17,27 @@ impl Godette {
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?,
Command::Help => handlers::show_help(bot, msg).await?,
Command::Me(quote) => handlers::me(bot, msg, quote).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(())
}
@ -31,55 +48,11 @@ impl Godette {
.unwrap_or(msg.caption().unwrap_or_default())
.to_string();
match text.to_lowercase().find("спасибо") {
Some(_id) => bot.send_message(msg.chat.id, "Не за что!").await?,
None => todo!(),
Some(_id) => {
bot.send_message(msg.chat.id, "Не за что!").await?;
}
None => (),
};
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(&quote);
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
}
}

View File

@ -6,8 +6,17 @@ pub enum Command {
Help,
#[command(description = "Написать сообщение от третьего лица")]
Me(String),
}
#[derive(BotCommands, Clone)]
#[command(rename_rule = "lowercase", description = "Админские команды:")]
pub enum AdminCommand {
#[command(description = "Отобразить эту помощь")]
HelpAdmin,
#[command(description = "Выдать предупреждение пользователю (только для админов)")]
Warn(String),
#[command(description = "Снять предупреждения и убрать мут")]
Unwarn,
#[command(description = "Сказать от моего лица")]
Say(String),
}

68
src/godette/handlers.rs Normal file
View 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(&quote);
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(&quote)));
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
}

View File

@ -1,8 +1,8 @@
mod commands;
mod godette;
use teloxide::prelude::*;
use godette::commands;
use godette::Godette;
#[tokio::main]
@ -16,6 +16,11 @@ async fn main() {
.filter_command::<commands::Command>()
.endpoint(Godette::commands_dispatcher),
)
.branch(
dptree::entry()
.filter_command::<commands::AdminCommand>()
.endpoint(Godette::admin_dispatcher),
)
.branch(
dptree::filter(|msg: Message| {
msg.from()