diff --git a/locales/en.yml b/locales/en.yml index 2b6b89e..2e38028 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -3,4 +3,8 @@ op_yourself: "Now you're an admin" has_to_reply: "You have to reply on target's message" cant_do_that: "You can't do that bruh" started_private_chat: "Since you've initiated private chat now you could receive messages from bot" -started_public_chat: "For using the bot you may want to request access via /request or /request_chat" \ No newline at end of file +started_public_chat: "For using the bot you may want to request access via /request or /request_chat" +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" \ No newline at end of file diff --git a/src/bot/bot.rs b/src/bot/bot.rs index bd9660f..d435bf0 100644 --- a/src/bot/bot.rs +++ b/src/bot/bot.rs @@ -91,7 +91,7 @@ async fn handle_message( match msg.kind { MessageKind::NewChatMembers(MessageNewChatMembers { new_chat_members }) => { handle_new_chat_member(bot, &msg.chat, new_chat_members, db, me).await? - }, + } MessageKind::Common(_) => (), _ => { dbg!(msg); diff --git a/src/bot/request.rs b/src/bot/request.rs new file mode 100644 index 0000000..7a7ff83 --- /dev/null +++ b/src/bot/request.rs @@ -0,0 +1,45 @@ +use sqlx::Row; +use rust_i18n::t; +use teloxide::prelude::*; +use tracing::{event, Level}; + +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; + +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 { + reply_i18n_and_return!(bot, msg.chat.id, "request_text_is_too_long"); + } + + if let Some(user) = msg.from() { + let user = find_or_create_user(&db, user).await?; + if user.can_download == 1 { + reply_i18n_and_return!(bot, msg.chat.id, "already_can_download"); + } + + let requests: i64 = sqlx::query("SELECT COUNT(1) FROM request WHERE requested_by = $1;") + .bind(user.id) + .fetch_one(&db).await? + .get(0); + if requests > 0 { + reply_i18n_and_return!(bot, msg.chat.id, "already_has_requested"); + } + + // put the request + sqlx::query("INSERT INTO request (requested_by,message,is_approved) VALUES ($1,$2,$3);") + .bind(user.id) + .bind(text) + .bind(0) + .execute(&db).await?; + event!(Level::INFO, "added request for {}", user); + + // notify admins + } + + todo!() +} \ No newline at end of file