This commit is contained in:
Akulij 2025-04-03 22:51:32 +09:00
parent f5deffc3b3
commit c977500ee4
4 changed files with 39 additions and 24 deletions

View File

@ -75,7 +75,11 @@ impl DB {
}
}
pub async fn get_message(&mut self, chatid: i64, messageid: i32) -> Result<Option<Message>, Box<dyn std::error::Error>> {
pub async fn get_message(
&mut self,
chatid: i64,
messageid: i32,
) -> Result<Option<Message>, Box<dyn std::error::Error>> {
use self::schema::messages::dsl::*;
let conn = &mut self.pool.get().await.unwrap();
@ -89,15 +93,24 @@ impl DB {
Ok(msg)
}
pub async fn get_message_literal(&mut self, chatid: i64, messageid: i32) -> Result<Option<String>, Box<dyn std::error::Error>> {
pub async fn get_message_literal(
&mut self,
chatid: i64,
messageid: i32,
) -> Result<Option<String>, Box<dyn std::error::Error>> {
let msg = self.get_message(chatid, messageid).await?;
Ok(msg.map(|m| m.token))
}
pub async fn set_message_literal(&mut self, chatid: i64, messageid: i32, literal: &str) -> Result<(), Box<dyn std::error::Error>> {
pub async fn set_message_literal(
&mut self,
chatid: i64,
messageid: i32,
literal: &str,
) -> Result<(), Box<dyn std::error::Error>> {
use self::schema::messages::dsl::*;
let conn = &mut self.pool.get().await?;
let msg = self.clone().get_message(chatid, messageid).await?;
match msg {
@ -107,20 +120,19 @@ impl DB {
.set(token.eq(literal))
.execute(conn)
.await?;
},
}
None => {
diesel::insert_into(messages)
.values((
chat_id.eq(chatid),
message_id.eq(messageid as i64),
token.eq(literal)
token.eq(literal),
))
.execute(conn)
.await?;
}
};
Ok(())
}
}

View File

@ -3,7 +3,6 @@
#![allow(unused)]
#![allow(clippy::all)]
use diesel::prelude::*;
#[derive(Queryable, Debug)]
#[diesel(table_name = literals)]
@ -28,4 +27,3 @@ pub struct User {
pub id: i64,
pub is_admin: bool,
}

View File

@ -26,8 +26,4 @@ diesel::table! {
}
}
diesel::allow_tables_to_appear_in_same_query!(
literals,
messages,
users,
);
diesel::allow_tables_to_appear_in_same_query!(literals, messages, users,);

View File

@ -56,13 +56,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("{u:#?}"); // Print the update to the console with inspect
})
.branch(command_handler(config))
.branch(Update::filter_message()
.filter_async(async |msg: Message, mut db: DB| {
let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64).await;
user.is_admin
})
.filter(|msg: Message| msg == "edit")
.endpoint(edit_msg_handler)
.branch(
Update::filter_message()
.filter_async(async |msg: Message, mut db: DB| {
let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64).await;
user.is_admin
})
.filter(|msg: Message| msg == "edit")
.endpoint(edit_msg_handler),
)
.branch(Update::filter_message().endpoint(echo));
@ -81,15 +82,23 @@ async fn edit_msg_handler(bot: Bot, msg: Message) -> Result<(), teloxide::Reques
Some(replied) => {
let msgid = replied.id;
// look for message in db and set text
},
}
None => {
bot.send_message(msg.chat.id, "You have to reply to message to edit it").await?;
bot.send_message(msg.chat.id, "You have to reply to message to edit it")
.await?;
}
};
Ok(())
}
fn command_handler(config: Config) -> Handler<'static, DependencyMap, Result<(), teloxide::RequestError>, teloxide::dispatching::DpHandlerDescription> {
fn command_handler(
config: Config,
) -> Handler<
'static,
DependencyMap,
Result<(), teloxide::RequestError>,
teloxide::dispatching::DpHandlerDescription,
> {
Update::filter_message()
.branch(
dptree::entry()