mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 09:49:07 +01:00
Refactor examples/admin.rs
and buttons.rs
This commit is contained in:
parent
ae227be4f0
commit
d4709c8061
2 changed files with 77 additions and 49 deletions
|
@ -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,29 +143,7 @@ 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?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
async fn action(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
|
@ -150,4 +169,13 @@ async fn main() {
|
|||
let bot = teloxide::Bot::from_env().auto_send();
|
||||
|
||||
teloxide::commands_repl(bot, action, Command::ty()).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),
|
||||
}
|
||||
>>>>>>> Refactor `examples/admin.rs` and `buttons.rs`
|
||||
}
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue