mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Refactor
This commit is contained in:
@@ -20,18 +20,17 @@ use super::{ignore_channel_messages, BotCommands, BotHandler, bots_manager::get_
|
|||||||
|
|
||||||
async fn _update_activity(me: teloxide::types::Me, user: teloxide::types::User) -> Option<()> {
|
async fn _update_activity(me: teloxide::types::Me, user: teloxide::types::User) -> Option<()> {
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(_) = update_user_activity(user.id).await {
|
if update_user_activity(user.id).await.is_err() {
|
||||||
let allowed_langs = get_user_or_default_lang_codes(user.id).await;
|
let allowed_langs = get_user_or_default_lang_codes(user.id).await;
|
||||||
|
|
||||||
if let Ok(_) = create_or_update_user_settings(
|
if create_or_update_user_settings(
|
||||||
user.id,
|
user.id,
|
||||||
user.last_name.clone().unwrap_or("".to_string()),
|
user.last_name.clone().unwrap_or("".to_string()),
|
||||||
user.first_name.clone(),
|
user.first_name.clone(),
|
||||||
user.username.clone().unwrap_or("".to_string()),
|
user.username.clone().unwrap_or("".to_string()),
|
||||||
me.username.clone().unwrap(),
|
me.username.clone().unwrap(),
|
||||||
allowed_langs,
|
allowed_langs,
|
||||||
)
|
).await.is_ok()
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
#[allow(unused_must_use)]
|
#[allow(unused_must_use)]
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -100,15 +100,13 @@ async fn _send_downloaded_file(
|
|||||||
|
|
||||||
let document: InputFile = InputFile::read(data).file_name(filename);
|
let document: InputFile = InputFile::read(data).file_name(filename);
|
||||||
|
|
||||||
match bot
|
bot
|
||||||
.send_document(message.chat.id, document)
|
.send_document(message.chat.id, document)
|
||||||
.caption(caption)
|
.caption(caption)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await?;
|
||||||
{
|
|
||||||
Ok(_) => Ok(()),
|
Ok(())
|
||||||
Err(err) => Err(Box::new(err)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_with_download_from_channel(
|
async fn send_with_download_from_channel(
|
||||||
|
|||||||
@@ -25,17 +25,17 @@ enum SettingsCommand {
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
enum SettingsCallbackData {
|
enum SettingsCallbackData {
|
||||||
LangSettings,
|
Settings,
|
||||||
LangOn { code: String },
|
On { code: String },
|
||||||
LangOff { code: String },
|
Off { code: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for SettingsCallbackData {
|
impl FromStr for SettingsCallbackData {
|
||||||
type Err = strum::ParseError;
|
type Err = strum::ParseError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
if s == SettingsCallbackData::LangSettings.to_string().as_str() {
|
if s == SettingsCallbackData::Settings.to_string().as_str() {
|
||||||
return Ok(SettingsCallbackData::LangSettings);
|
return Ok(SettingsCallbackData::Settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
let re = Regex::new(r"^lang_(?P<action>(off)|(on))_(?P<code>[a-zA-z]+)$").unwrap();
|
let re = Regex::new(r"^lang_(?P<action>(off)|(on))_(?P<code>[a-zA-z]+)$").unwrap();
|
||||||
@@ -50,8 +50,8 @@ impl FromStr for SettingsCallbackData {
|
|||||||
let code = caps["code"].to_string();
|
let code = caps["code"].to_string();
|
||||||
|
|
||||||
match action {
|
match action {
|
||||||
"on" => Ok(SettingsCallbackData::LangOn { code }),
|
"on" => Ok(SettingsCallbackData::On { code }),
|
||||||
"off" => Ok(SettingsCallbackData::LangOff { code }),
|
"off" => Ok(SettingsCallbackData::Off { code }),
|
||||||
_ => Err(strum::ParseError::VariantNotFound),
|
_ => Err(strum::ParseError::VariantNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,9 +60,9 @@ impl FromStr for SettingsCallbackData {
|
|||||||
impl ToString for SettingsCallbackData {
|
impl ToString for SettingsCallbackData {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
SettingsCallbackData::LangSettings => "lang_settings".to_string(),
|
SettingsCallbackData::Settings => "lang_settings".to_string(),
|
||||||
SettingsCallbackData::LangOn { code } => format!("lang_on_{code}"),
|
SettingsCallbackData::On { code } => format!("lang_on_{code}"),
|
||||||
SettingsCallbackData::LangOff { code } => format!("lang_off_{code}"),
|
SettingsCallbackData::Off { code } => format!("lang_off_{code}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ async fn settings_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotH
|
|||||||
inline_keyboard: vec![vec![InlineKeyboardButton {
|
inline_keyboard: vec![vec![InlineKeyboardButton {
|
||||||
text: "Языки".to_string(),
|
text: "Языки".to_string(),
|
||||||
kind: teloxide::types::InlineKeyboardButtonKind::CallbackData(
|
kind: teloxide::types::InlineKeyboardButtonKind::CallbackData(
|
||||||
SettingsCallbackData::LangSettings.to_string(),
|
SettingsCallbackData::Settings.to_string(),
|
||||||
),
|
),
|
||||||
}]],
|
}]],
|
||||||
};
|
};
|
||||||
@@ -95,11 +95,11 @@ fn get_lang_keyboard(all_langs: Vec<Lang>, allowed_langs: HashSet<String>) -> In
|
|||||||
let (emoji, callback_data) = match allowed_langs.contains(&lang.code) {
|
let (emoji, callback_data) = match allowed_langs.contains(&lang.code) {
|
||||||
true => (
|
true => (
|
||||||
"🟢".to_string(),
|
"🟢".to_string(),
|
||||||
SettingsCallbackData::LangOff { code: lang.code }.to_string(),
|
SettingsCallbackData::Off { code: lang.code }.to_string(),
|
||||||
),
|
),
|
||||||
false => (
|
false => (
|
||||||
"🔴".to_string(),
|
"🔴".to_string(),
|
||||||
SettingsCallbackData::LangOn { code: lang.code }.to_string(),
|
SettingsCallbackData::On { code: lang.code }.to_string(),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -141,11 +141,11 @@ async fn settings_callback_handler(
|
|||||||
});
|
});
|
||||||
|
|
||||||
match callback_data {
|
match callback_data {
|
||||||
SettingsCallbackData::LangSettings => (),
|
SettingsCallbackData::Settings => (),
|
||||||
SettingsCallbackData::LangOn { code } => {
|
SettingsCallbackData::On { code } => {
|
||||||
allowed_langs_set.insert(code);
|
allowed_langs_set.insert(code);
|
||||||
}
|
}
|
||||||
SettingsCallbackData::LangOff { code } => {
|
SettingsCallbackData::Off { code } => {
|
||||||
allowed_langs_set.remove(&code);
|
allowed_langs_set.remove(&code);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -49,9 +49,8 @@ pub async fn get_random_book_by_genre(
|
|||||||
) -> Result<types::Book, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<types::Book, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let mut params: Vec<(&str, String)> = get_allowed_langs_params(allowed_langs);
|
let mut params: Vec<(&str, String)> = get_allowed_langs_params(allowed_langs);
|
||||||
|
|
||||||
match genre {
|
if let Some(v) = genre {
|
||||||
Some(v) => params.push(("genre", v.to_string())),
|
params.push(("genre", v.to_string()));
|
||||||
None => (),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_make_request("/api/v1/books/random", params).await
|
_make_request("/api/v1/books/random", params).await
|
||||||
|
|||||||
@@ -139,20 +139,21 @@ impl BotsManager {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn sd_token(token: &ShutdownToken) {
|
||||||
|
for _ in 1..10 {
|
||||||
|
if let Ok(v) = token.clone().shutdown() { return v.await }
|
||||||
|
|
||||||
|
sleep(Duration::from_millis(100)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn stop_bot(&mut self, bot_id: u32) {
|
async fn stop_bot(&mut self, bot_id: u32) {
|
||||||
let shutdown_token = match self.bot_shutdown_token_map.remove(&bot_id) {
|
let shutdown_token = match self.bot_shutdown_token_map.remove(&bot_id) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
for _ in 1..100 {
|
BotsManager::sd_token(&shutdown_token).await;
|
||||||
match shutdown_token.clone().shutdown() {
|
|
||||||
Ok(v) => return v.await,
|
|
||||||
Err(_) => (),
|
|
||||||
};
|
|
||||||
|
|
||||||
sleep(Duration::from_millis(100)).await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_data(&mut self, bots_data: Vec<BotData>) {
|
async fn update_data(&mut self, bots_data: Vec<BotData>) {
|
||||||
@@ -202,15 +203,7 @@ impl BotsManager {
|
|||||||
|
|
||||||
async fn stop_all(&mut self) {
|
async fn stop_all(&mut self) {
|
||||||
for token in self.bot_shutdown_token_map.values() {
|
for token in self.bot_shutdown_token_map.values() {
|
||||||
for _ in 1..100 {
|
BotsManager::sd_token(token).await;
|
||||||
match token.clone().shutdown() {
|
|
||||||
Ok(v) => {
|
|
||||||
v.await;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Err(_) => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user