implement request command

This commit is contained in:
mykola2312 2024-03-03 19:26:30 +02:00
parent d47f62919f
commit 1796a360ae
5 changed files with 53 additions and 11 deletions

View file

@ -7,4 +7,6 @@ started_public_chat: "For using the bot you may want to request access via /requ
request_text_is_too_short: "Fuck off unless you write meaningful request message"
request_text_is_too_long: "I ain't reading allat"
already_can_download: "You already have permission to download, no need to request it then"
already_has_requested: "Stop spamming requests. That ain't gonna grant ya the download perm"
already_has_requested: "Stop spamming requests. That ain't gonna grant ya the download perm"
request_added: "Your requested has been added. Admins have been notifed"
admin_notify_request: "User %{user} awaits request approval"

View file

@ -1,8 +1,9 @@
pub mod bot;
pub mod dl;
pub mod notify;
pub mod op;
pub mod sanitize;
pub mod request;
pub mod sanitize;
pub mod start;
pub mod types;
@ -11,5 +12,5 @@ macro_rules! reply_i18n_and_return {
($bot:expr, $chat_id:expr, $line:expr) => {
$bot.send_message($chat_id, t!($line)).await?;
return Ok(())
}
}
};
}

View file

@ -17,6 +17,7 @@ use crate::db::DbPool;
use super::dl::cmd_download;
use super::op::cmd_op;
use super::start::{cmd_start, handle_my_chat_member};
use super::request::cmd_request;
fn parse_env<T>(name: &str) -> T
where
@ -61,7 +62,8 @@ fn schema() -> UpdateHandler<HandlerErr> {
.branch(case![Command::Test].endpoint(cmd_test))
.branch(case![Command::Start].endpoint(cmd_start))
.branch(case![Command::Download(url)].endpoint(cmd_download))
.branch(case![Command::OP].endpoint(cmd_op));
.branch(case![Command::OP].endpoint(cmd_op))
.branch(case![Command::Request(text)].endpoint(cmd_request));
let message_handler = Update::filter_message().branch(command_handler);
let raw_message_handler = Update::filter_message().branch(dptree::endpoint(handle_message));
@ -114,6 +116,9 @@ enum Command {
#[command(alias = "op")]
OP,
#[command(alias = "request")]
Request(String)
}
async fn cmd_test(bot: Bot, msg: Message, _db: DbPool) -> HandlerResult {

23
src/bot/notify.rs Normal file
View file

@ -0,0 +1,23 @@
use teloxide::{prelude::*, types::Recipient};
use tracing::{event, Level};
use crate::db::{DbPool, User};
use super::types::HandlerResult;
pub async fn notify_admins(bot: &Bot, db: &DbPool, message: String) -> HandlerResult {
let admins: Vec<User> =
sqlx::query_as("SELECT * FROM user WHERE is_admin = 1 AND has_private_chat = 1;")
.fetch_all(db)
.await?;
for admin in admins {
let res = bot
.send_message(Recipient::Id(ChatId(admin.tg_id)), &message)
.await;
if let Err(e) = res {
event!(Level::WARN, "notify admin {} error {}", admin, e);
}
}
Ok(())
}

View file

@ -1,18 +1,19 @@
use sqlx::Row;
use rust_i18n::t;
use sqlx::Row;
use teloxide::prelude::*;
use tracing::{event, Level};
use super::notify::notify_admins;
use super::types::HandlerResult;
use crate::reply_i18n_and_return;
use crate::db::chat::find_or_create_chat;
use crate::db::user::find_or_create_user;
use crate::db::DbPool;
use crate::reply_i18n_and_return;
pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> HandlerResult {
if text.len() < 16 {
reply_i18n_and_return!(bot, msg.chat.id, "request_text_is_too_short");
} else if text.len() > 255 {
} else if text.len() > 100 {
reply_i18n_and_return!(bot, msg.chat.id, "request_text_is_too_long");
}
@ -24,7 +25,8 @@ pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> Ha
let requests: i64 = sqlx::query("SELECT COUNT(1) FROM request WHERE requested_by = $1;")
.bind(user.id)
.fetch_one(&db).await?
.fetch_one(&db)
.await?
.get(0);
if requests > 0 {
reply_i18n_and_return!(bot, msg.chat.id, "already_has_requested");
@ -35,11 +37,20 @@ pub async fn cmd_request(bot: Bot, msg: Message, text: String, db: DbPool) -> Ha
.bind(user.id)
.bind(text)
.bind(0)
.execute(&db).await?;
.execute(&db)
.await?;
event!(Level::INFO, "added request for {}", user);
// notify admins
notify_admins(
&bot,
&db,
t!("admin_notify_request", user = user.to_string()).to_string(),
)
.await?;
bot.send_message(msg.chat.id, t!("request_added")).await?;
}
todo!()
}
}