1use std::str::FromStr;
2
3use chrono::Duration;
4use teloxide::{prelude::*, types::ChatPermissions, utils::command::BotCommands};
5
6#[derive(BotCommands, Clone)]
18#[command(rename_rule = "lowercase", parse_with = "split")]
19enum Command {
20 Kick,
22 Ban {
24 time: u64,
25 unit: UnitOfTime,
26 },
27 Mute {
29 time: u64,
30 unit: UnitOfTime,
31 },
32 Help,
33}
34
35#[derive(Clone)]
36enum UnitOfTime {
37 Seconds,
38 Minutes,
39 Hours,
40}
41
42impl FromStr for UnitOfTime {
43 type Err = &'static str;
44 fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
45 match s {
46 "h" | "hours" => Ok(UnitOfTime::Hours),
47 "m" | "minutes" => Ok(UnitOfTime::Minutes),
48 "s" | "seconds" => Ok(UnitOfTime::Seconds),
49 _ => Err("Allowed units: h, m, s"),
50 }
51 }
52}
53
54#[tokio::main]
55async fn main() {
56 pretty_env_logger::init();
57 log::info!("Starting admin bot...");
58
59 let bot = teloxide::Bot::from_env();
60
61 Command::repl(bot, action).await;
62}
63
64async fn action(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
65 match cmd {
66 Command::Help => {
67 bot.send_message(Command::descriptions().to_string()).chat_id(msg.chat.id).await?;
68 }
69 Command::Kick => kick_user(bot, msg).await?,
70 Command::Ban { time, unit } => ban_user(bot, msg, calc_restrict_time(time, unit)).await?,
71 Command::Mute { time, unit } => mute_user(bot, msg, calc_restrict_time(time, unit)).await?,
72 };
73
74 Ok(())
75}
76
77async fn kick_user(bot: Bot, msg: Message) -> ResponseResult<()> {
79 match msg.reply_to_message() {
80 Some(replied) => {
81 bot.unban_chat_member(msg.chat.id, replied.from.as_ref().unwrap().id).await?;
83 }
84 None => {
85 bot.send_message("Use this command in reply to another message")
86 .chat_id(msg.chat.id)
87 .await?;
88 }
89 }
90 Ok(())
91}
92
93async fn ban_user(bot: Bot, msg: Message, time: Duration) -> ResponseResult<()> {
95 match msg.reply_to_message() {
96 Some(replied) => {
97 bot.kick_chat_member(
98 msg.chat.id,
99 replied.from.as_ref().expect("Must be MessageKind::Common").id,
100 )
101 .until_date(msg.date + time)
102 .await?;
103 }
104 None => {
105 bot.send_message("Use this command in a reply to another message!")
106 .chat_id(msg.chat.id)
107 .await?;
108 }
109 }
110 Ok(())
111}
112
113async fn mute_user(bot: Bot, msg: Message, time: Duration) -> ResponseResult<()> {
115 match msg.reply_to_message() {
116 Some(replied) => {
117 bot.restrict_chat_member(
118 msg.chat.id,
119 replied.from.as_ref().expect("Must be MessageKind::Common").id,
120 ChatPermissions::empty(),
121 )
122 .until_date(msg.date + time)
123 .await?;
124 }
125 None => {
126 bot.send_message("Use this command in a reply to another message!")
127 .chat_id(msg.chat.id)
128 .await?;
129 }
130 }
131 Ok(())
132}
133
134fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration {
136 match unit {
139 UnitOfTime::Hours => Duration::try_hours(time as i64).unwrap(),
140 UnitOfTime::Minutes => Duration::try_minutes(time as i64).unwrap(),
141 UnitOfTime::Seconds => Duration::try_seconds(time as i64).unwrap(),
142 }
143}