begin implementing permission requesting system

This commit is contained in:
mykola2312 2024-03-03 19:05:29 +02:00
parent e692ebceb8
commit 8e649c043f
3 changed files with 51 additions and 2 deletions

View file

@ -4,3 +4,7 @@ has_to_reply: "You have to reply on target's message"
cant_do_that: "You can't do that bruh" 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_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" 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"

View file

@ -91,7 +91,7 @@ async fn handle_message(
match msg.kind { match msg.kind {
MessageKind::NewChatMembers(MessageNewChatMembers { new_chat_members }) => { MessageKind::NewChatMembers(MessageNewChatMembers { new_chat_members }) => {
handle_new_chat_member(bot, &msg.chat, new_chat_members, db, me).await? handle_new_chat_member(bot, &msg.chat, new_chat_members, db, me).await?
}, }
MessageKind::Common(_) => (), MessageKind::Common(_) => (),
_ => { _ => {
dbg!(msg); dbg!(msg);

45
src/bot/request.rs Normal file
View file

@ -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!()
}