cargo fmt
This commit is contained in:
parent
d74e691009
commit
8e3ebc936c
@ -3,8 +3,8 @@ use teloxide::{
|
|||||||
utils::{command::BotCommands, render::RenderMessageTextHelper},
|
utils::{command::BotCommands, render::RenderMessageTextHelper},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::LogMsg;
|
|
||||||
use crate::db::DB;
|
use crate::db::DB;
|
||||||
|
use crate::LogMsg;
|
||||||
|
|
||||||
// These are should not appear in /help
|
// These are should not appear in /help
|
||||||
#[derive(BotCommands, Clone)]
|
#[derive(BotCommands, Clone)]
|
||||||
|
|||||||
20
src/db.rs
20
src/db.rs
@ -5,10 +5,10 @@ use crate::Config;
|
|||||||
use self::models::*;
|
use self::models::*;
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use diesel_async::pooled_connection::bb8::Pool;
|
||||||
|
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
|
||||||
use diesel_async::AsyncPgConnection;
|
use diesel_async::AsyncPgConnection;
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
|
|
||||||
use diesel_async::pooled_connection::bb8::Pool;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DB {
|
pub struct DB {
|
||||||
@ -125,7 +125,10 @@ impl DB {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_literal(&mut self, literal: &str) -> Result<Option<Literal>, Box<dyn std::error::Error>> {
|
async fn get_literal(
|
||||||
|
&mut self,
|
||||||
|
literal: &str,
|
||||||
|
) -> Result<Option<Literal>, Box<dyn std::error::Error>> {
|
||||||
use self::schema::literals::dsl::*;
|
use self::schema::literals::dsl::*;
|
||||||
let conn = &mut self.pool.get().await.unwrap();
|
let conn = &mut self.pool.get().await.unwrap();
|
||||||
|
|
||||||
@ -138,13 +141,20 @@ impl DB {
|
|||||||
Ok(literal)
|
Ok(literal)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_literal_value(&mut self, literal: &str) -> Result<Option<String>, Box<dyn std::error::Error>> {
|
pub async fn get_literal_value(
|
||||||
|
&mut self,
|
||||||
|
literal: &str,
|
||||||
|
) -> Result<Option<String>, Box<dyn std::error::Error>> {
|
||||||
let literal = self.get_literal(literal).await?;
|
let literal = self.get_literal(literal).await?;
|
||||||
|
|
||||||
Ok(literal.map(|l| l.value))
|
Ok(literal.map(|l| l.value))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_literal(&mut self, literal: &str, valuestr: &str) -> Result<(), Box<dyn std::error::Error>> {
|
pub async fn set_literal(
|
||||||
|
&mut self,
|
||||||
|
literal: &str,
|
||||||
|
valuestr: &str,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
use self::schema::literals::dsl::*;
|
use self::schema::literals::dsl::*;
|
||||||
let conn = &mut self.pool.get().await.unwrap();
|
let conn = &mut self.pool.get().await.unwrap();
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
#![allow(clippy::all)]
|
#![allow(clippy::all)]
|
||||||
|
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
#[derive(Queryable, Debug)]
|
#[derive(Queryable, Debug)]
|
||||||
#[diesel(table_name = literals)]
|
#[diesel(table_name = literals)]
|
||||||
@ -28,4 +27,3 @@ pub struct User {
|
|||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub is_admin: bool,
|
pub is_admin: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,8 +26,4 @@ diesel::table! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
diesel::allow_tables_to_appear_in_same_query!(
|
diesel::allow_tables_to_appear_in_same_query!(literals, messages, users,);
|
||||||
literals,
|
|
||||||
messages,
|
|
||||||
users,
|
|
||||||
);
|
|
||||||
|
|||||||
69
src/main.rs
69
src/main.rs
@ -1,8 +1,8 @@
|
|||||||
pub mod admin;
|
pub mod admin;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
|
|
||||||
use crate::admin::{AdminCommands, admin_command_handler};
|
use crate::admin::{admin_command_handler, AdminCommands};
|
||||||
use crate::admin::{SecretCommands, secret_command_handler};
|
use crate::admin::{secret_command_handler, SecretCommands};
|
||||||
use crate::db::DB;
|
use crate::db::DB;
|
||||||
|
|
||||||
use envconfig::Envconfig;
|
use envconfig::Envconfig;
|
||||||
@ -54,7 +54,7 @@ pub enum State {
|
|||||||
Start,
|
Start,
|
||||||
Edit {
|
Edit {
|
||||||
literal: String,
|
literal: String,
|
||||||
lang: String,
|
lang: String,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,9 +85,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.filter(|msg: Message| msg.text().unwrap_or("") == "edit")
|
.filter(|msg: Message| msg.text().unwrap_or("") == "edit")
|
||||||
.endpoint(edit_msg_cmd_handler),
|
.endpoint(edit_msg_cmd_handler),
|
||||||
)
|
)
|
||||||
.branch(
|
.branch(dptree::case![State::Edit { literal, lang }].endpoint(edit_msg_handler)),
|
||||||
dptree::case![State::Edit { literal, lang }].endpoint(edit_msg_handler),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.branch(Update::filter_message().endpoint(echo));
|
.branch(Update::filter_message().endpoint(echo));
|
||||||
|
|
||||||
@ -101,22 +99,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn edit_msg_cmd_handler(bot: Bot, mut db: DB, dialogue: BotDialogue, msg: Message) -> Result<(), teloxide::RequestError> {
|
async fn edit_msg_cmd_handler(
|
||||||
|
bot: Bot,
|
||||||
|
mut db: DB,
|
||||||
|
dialogue: BotDialogue,
|
||||||
|
msg: Message,
|
||||||
|
) -> Result<(), teloxide::RequestError> {
|
||||||
match msg.reply_to_message() {
|
match msg.reply_to_message() {
|
||||||
Some(replied) => {
|
Some(replied) => {
|
||||||
let msgid = replied.id;
|
let msgid = replied.id;
|
||||||
// look for message in db and set text
|
// look for message in db and set text
|
||||||
let literal = match db.get_message_literal(msg.chat.id.0, msgid.0).await.unwrap() {
|
let literal = match db
|
||||||
|
.get_message_literal(msg.chat.id.0, msgid.0)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
Some(l) => l,
|
Some(l) => l,
|
||||||
None => {
|
None => {
|
||||||
bot.send_message(msg.chat.id, "No such message found to edit. Look if you replying bot's message and this message is supposed to be editable").await?;
|
bot.send_message(msg.chat.id, "No such message found to edit. Look if you replying bot's message and this message is supposed to be editable").await?;
|
||||||
return Ok(())
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// TODO: language selector will be implemented in future 😈
|
// TODO: language selector will be implemented in future 😈
|
||||||
let lang = "ru".to_string();
|
let lang = "ru".to_string();
|
||||||
dialogue.update(State::Edit { literal, lang }).await.unwrap();
|
dialogue
|
||||||
bot.send_message(msg.chat.id, "Ok, now you have to send message text (formatting supported)").await?;
|
.update(State::Edit { literal, lang })
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
bot.send_message(
|
||||||
|
msg.chat.id,
|
||||||
|
"Ok, now you have to send message text (formatting supported)",
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
bot.send_message(msg.chat.id, "You have to reply to message to edit it")
|
bot.send_message(msg.chat.id, "You have to reply to message to edit it")
|
||||||
@ -126,13 +140,21 @@ async fn edit_msg_cmd_handler(bot: Bot, mut db: DB, dialogue: BotDialogue, msg:
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn edit_msg_handler(bot: Bot, mut db: DB, dialogue: BotDialogue, (literal, lang): (String, String), msg: Message) -> Result<(), teloxide::RequestError> {
|
async fn edit_msg_handler(
|
||||||
|
bot: Bot,
|
||||||
|
mut db: DB,
|
||||||
|
dialogue: BotDialogue,
|
||||||
|
(literal, lang): (String, String),
|
||||||
|
msg: Message,
|
||||||
|
) -> Result<(), teloxide::RequestError> {
|
||||||
match msg.html_text() {
|
match msg.html_text() {
|
||||||
Some(text) => {
|
Some(text) => {
|
||||||
db.set_literal(&literal, &text).await.unwrap();
|
db.set_literal(&literal, &text).await.unwrap();
|
||||||
bot.send_message(msg.chat.id, "Updated text of message!").await.unwrap();
|
bot.send_message(msg.chat.id, "Updated text of message!")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
dialogue.exit().await.unwrap();
|
dialogue.exit().await.unwrap();
|
||||||
},
|
}
|
||||||
None => {
|
None => {
|
||||||
bot.send_message(msg.chat.id, "Send text!").await.unwrap();
|
bot.send_message(msg.chat.id, "Send text!").await.unwrap();
|
||||||
}
|
}
|
||||||
@ -178,14 +200,25 @@ async fn user_command_handler(
|
|||||||
msg: Message,
|
msg: Message,
|
||||||
cmd: UserCommands,
|
cmd: UserCommands,
|
||||||
) -> Result<(), teloxide::RequestError> {
|
) -> Result<(), teloxide::RequestError> {
|
||||||
let user = db.get_or_init_user(msg.from.clone().unwrap().id.0 as i64).await;
|
let user = db
|
||||||
|
.get_or_init_user(msg.from.clone().unwrap().id.0 as i64)
|
||||||
|
.await;
|
||||||
println!("MSG: {}", msg.html_text().unwrap());
|
println!("MSG: {}", msg.html_text().unwrap());
|
||||||
match cmd {
|
match cmd {
|
||||||
UserCommands::Start => {
|
UserCommands::Start => {
|
||||||
let literal = "start";
|
let literal = "start";
|
||||||
let text = db.get_literal_value(literal).await.unwrap().unwrap_or("Please, set content of this message".into());
|
let text = db
|
||||||
let msg = bot.send_message(msg.chat.id, text).parse_mode(teloxide::types::ParseMode::Html).await?;
|
.get_literal_value(literal)
|
||||||
db.set_message_literal(msg.chat.id.0, msg.id.0, literal).await.unwrap();
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap_or("Please, set content of this message".into());
|
||||||
|
let msg = bot
|
||||||
|
.send_message(msg.chat.id, text)
|
||||||
|
.parse_mode(teloxide::types::ParseMode::Html)
|
||||||
|
.await?;
|
||||||
|
db.set_message_literal(msg.chat.id.0, msg.id.0, literal)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
UserCommands::Help => {
|
UserCommands::Help => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user