Refactor examples/admin.rs and buttons.rs

This commit is contained in:
Hirrolot 2022-03-24 17:31:01 +06:00
parent 3075dfd8ac
commit f8438f1772
2 changed files with 73 additions and 73 deletions

View file

@ -53,17 +53,35 @@ impl FromStr for UnitOfTime {
}
}
// Calculates time of user restriction.
fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration {
match unit {
UnitOfTime::Hours => Duration::hours(time as i64),
UnitOfTime::Minutes => Duration::minutes(time as i64),
UnitOfTime::Seconds => Duration::seconds(time as i64),
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting admin_bot...");
let bot = teloxide::Bot::from_env().auto_send();
teloxide::commands_repl(bot, action, Command::ty()).await;
}
type Bot = AutoSend<teloxide::Bot>;
async fn action(
bot: Bot,
msg: Message,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match command {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions()).await?;
}
Command::Kick => kick_user(bot, msg).await?,
Command::Ban { time, unit } => ban_user(bot, msg, calc_restrict_time(time, unit)).await?,
Command::Mute { time, unit } => mute_user(bot, msg, calc_restrict_time(time, unit)).await?,
};
Ok(())
}
// Kick a user with a replied message.
async fn kick_user(bot: Bot, msg: Message) -> Result<(), Box<dyn Error + Send + Sync>> {
match msg.reply_to_message() {
@ -78,6 +96,29 @@ async fn kick_user(bot: Bot, msg: Message) -> Result<(), Box<dyn Error + Send +
Ok(())
}
// Ban a user with replied message.
async fn ban_user(
bot: Bot,
msg: Message,
time: Duration,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match msg.reply_to_message() {
Some(replied) => {
bot.kick_chat_member(
msg.chat.id,
replied.from().expect("Must be MessageKind::Common").id,
)
.until_date(msg.date + time)
.await?;
}
None => {
bot.send_message(msg.chat.id, "Use this command in a reply to another message!")
.await?;
}
}
Ok(())
}
// Mute a user with a replied message.
async fn mute_user(
bot: Bot,
@ -102,52 +143,11 @@ async fn mute_user(
Ok(())
}
// Ban a user with replied message.
async fn ban_user(
bot: Bot,
msg: Message,
time: Duration,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match msg.reply_to_message() {
Some(replied) => {
bot.kick_chat_member(
msg.chat.id,
replied.from().expect("Must be MessageKind::Common").id,
)
.until_date(msg.date + time)
.await?;
}
None => {
bot.send_message(msg.chat.id, "Use this command in a reply to another message!")
.await?;
}
// Calculates time of user restriction.
fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration {
match unit {
UnitOfTime::Hours => Duration::hours(time as i64),
UnitOfTime::Minutes => Duration::minutes(time as i64),
UnitOfTime::Seconds => Duration::seconds(time as i64),
}
Ok(())
}
async fn action(
bot: Bot,
msg: Message,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match command {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions()).await?;
}
Command::Kick => kick_user(bot, msg).await?,
Command::Ban { time, unit } => ban_user(bot, msg, calc_restrict_time(time, unit)).await?,
Command::Mute { time, unit } => mute_user(bot, msg, calc_restrict_time(time, unit)).await?,
};
Ok(())
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
log::info!("Starting admin_bot...");
let bot = teloxide::Bot::from_env().auto_send();
teloxide::commands_repl(bot, action, Command::ty()).await;
}

View file

@ -18,6 +18,25 @@ enum Command {
Start,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
log::info!("Starting bot...");
let bot = Bot::from_env().auto_send();
let handler = dptree::entry()
.branch(Update::filter_message().endpoint(message_handler))
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
log::info!("Closing bot... Goodbye!");
Ok(())
}
/// Creates a keyboard made by buttons in a big column.
fn make_keyboard() -> InlineKeyboardMarkup {
let mut keyboard: Vec<Vec<InlineKeyboardButton>> = vec![];
@ -111,22 +130,3 @@ async fn callback_handler(
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
log::info!("Starting bot...");
let bot = Bot::from_env().auto_send();
let handler = dptree::entry()
.branch(Update::filter_message().endpoint(message_handler))
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
log::info!("Closing bot... Goodbye!");
Ok(())
}