rustfmt
This commit is contained in:
parent
30117624c4
commit
5cadb2bd76
46
src/admin.rs
46
src/admin.rs
@ -1,16 +1,20 @@
|
||||
use teloxide::{dispatching::dialogue::GetChatId, payloads::SendMessageSetters, prelude::*, types::InputFile, utils::{command::BotCommands, render::RenderMessageTextHelper}};
|
||||
use teloxide::{
|
||||
dispatching::dialogue::GetChatId,
|
||||
payloads::SendMessageSetters,
|
||||
prelude::*,
|
||||
types::InputFile,
|
||||
utils::{command::BotCommands, render::RenderMessageTextHelper},
|
||||
};
|
||||
|
||||
use crate::db::DB;
|
||||
use crate::LogMsg;
|
||||
use crate::db::DB;
|
||||
|
||||
// These are should not appear in /help
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename_rule = "lowercase")]
|
||||
pub enum SecretCommands {
|
||||
/// Activate admin mode
|
||||
Secret {
|
||||
pass: String
|
||||
},
|
||||
Secret { pass: String },
|
||||
}
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
@ -34,20 +38,28 @@ pub async fn admin_command_handler(
|
||||
println!("MSG: {}", msg.html_text().unwrap());
|
||||
match cmd {
|
||||
AdminCommands::MyId => {
|
||||
bot.send_message(msg.chat.id, format!("Your ID is: {}", tguser.id)).log().await?;
|
||||
bot.send_message(msg.chat.id, format!("Your ID is: {}", tguser.id))
|
||||
.log()
|
||||
.await?;
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
AdminCommands::Pin => {
|
||||
if let Some(msg_to_pin) = msg.reply_to_message() {
|
||||
bot.pin_chat_message(msg.chat.id, msg_to_pin.id).await?;
|
||||
} else {
|
||||
bot.send_message(msg.chat.id, "you need to reply to some message with this command").log().await?;
|
||||
bot.send_message(
|
||||
msg.chat.id,
|
||||
"you need to reply to some message with this command",
|
||||
)
|
||||
.log()
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
AdminCommands::Deop => {
|
||||
db.set_admin(tguser.id.0 as i64, false).await;
|
||||
bot.send_message(msg.chat.id, "You are not an admin anymore").await?;
|
||||
bot.send_message(msg.chat.id, "You are not an admin anymore")
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -59,20 +71,24 @@ pub async fn secret_command_handler(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
cmd: SecretCommands,
|
||||
admin_password: String
|
||||
admin_password: String,
|
||||
) -> Result<(), teloxide::RequestError> {
|
||||
println!("Admin Pass: {}", admin_password);
|
||||
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());
|
||||
match cmd {
|
||||
SecretCommands::Secret { pass } => {
|
||||
if user.is_admin == true {
|
||||
bot.send_message(msg.from.unwrap().id, "You are an admin already").await?;
|
||||
bot.send_message(msg.from.unwrap().id, "You are an admin already")
|
||||
.await?;
|
||||
} else if pass == admin_password {
|
||||
db.set_admin(user.id, true).await;
|
||||
bot.send_message(msg.from.unwrap().id, "You are admin now!").await?;
|
||||
bot.send_message(msg.from.unwrap().id, "You are admin now!")
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
29
src/db.rs
29
src/db.rs
@ -8,14 +8,14 @@ use diesel::prelude::*;
|
||||
//use diesel::query_dsl::methods::FilterDsl;
|
||||
//use diesel::{prelude::*, r2d2::{ConnectionManager, Pool}};
|
||||
use diesel_async::AsyncPgConnection;
|
||||
use diesel_async::RunQueryDsl;
|
||||
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
|
||||
use diesel_async::pooled_connection::bb8::Pool;
|
||||
use diesel_async::RunQueryDsl;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DB {
|
||||
//pool: Pool<ConnectionManager<AsyncPgConnection>>
|
||||
pool: diesel_async::pooled_connection::bb8::Pool<AsyncPgConnection>
|
||||
pool: diesel_async::pooled_connection::bb8::Pool<AsyncPgConnection>,
|
||||
}
|
||||
|
||||
impl DB {
|
||||
@ -34,7 +34,11 @@ impl DB {
|
||||
use self::schema::users::dsl::*;
|
||||
let mut conn = self.pool.get().await.unwrap();
|
||||
//let mut conn = AsyncPgConnection::establish(&std::env::var("DATABASE_URL").unwrap()).await.unwrap();
|
||||
users.filter(id.gt(0)).load::<User>(&mut conn).await.unwrap()
|
||||
users
|
||||
.filter(id.gt(0))
|
||||
.load::<User>(&mut conn)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn set_admin(&mut self, userid: i64, isadmin: bool) {
|
||||
@ -45,20 +49,29 @@ impl DB {
|
||||
diesel::update(users)
|
||||
.filter(id.eq(userid))
|
||||
.set(is_admin.eq(isadmin))
|
||||
.execute(connection).await.unwrap();
|
||||
.execute(connection)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn get_or_init_user(&mut self, userid: i64) -> User {
|
||||
use self::schema::users::dsl::*;
|
||||
let connection = &mut self.pool.get().await.unwrap();
|
||||
|
||||
let user = users.filter(id.eq(userid)).first::<User>(connection).await.optional().unwrap();
|
||||
let user = users
|
||||
.filter(id.eq(userid))
|
||||
.first::<User>(connection)
|
||||
.await
|
||||
.optional()
|
||||
.unwrap();
|
||||
|
||||
match user {
|
||||
Some(existing_user) => existing_user,
|
||||
None => {
|
||||
diesel::insert_into(users).values((id.eq(userid as i64), is_admin.eq(false))).get_result(connection).await.unwrap()
|
||||
}
|
||||
None => diesel::insert_into(users)
|
||||
.values((id.eq(userid as i64), is_admin.eq(false)))
|
||||
.get_result(connection)
|
||||
.await
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
#![allow(unused)]
|
||||
#![allow(clippy::all)]
|
||||
|
||||
|
||||
use diesel::prelude::*;
|
||||
#[derive(Queryable, Debug)]
|
||||
#[diesel(table_name = users)]
|
||||
@ -11,4 +10,3 @@ pub struct User {
|
||||
pub id: i64,
|
||||
pub is_admin: bool,
|
||||
}
|
||||
|
||||
|
||||
55
src/main.rs
55
src/main.rs
@ -1,12 +1,18 @@
|
||||
pub mod db;
|
||||
pub mod admin;
|
||||
pub mod db;
|
||||
|
||||
use crate::db::DB;
|
||||
use crate::admin::{AdminCommands, admin_command_handler};
|
||||
use crate::admin::{secret_command_handler, SecretCommands};
|
||||
use crate::admin::{SecretCommands, secret_command_handler};
|
||||
use crate::db::DB;
|
||||
|
||||
use teloxide::{dispatching::dialogue::GetChatId, payloads::SendMessageSetters, prelude::*, types::InputFile, utils::{command::BotCommands, render::RenderMessageTextHelper}};
|
||||
use envconfig::Envconfig;
|
||||
use teloxide::{
|
||||
dispatching::dialogue::GetChatId,
|
||||
payloads::SendMessageSetters,
|
||||
prelude::*,
|
||||
types::InputFile,
|
||||
utils::{command::BotCommands, render::RenderMessageTextHelper},
|
||||
};
|
||||
|
||||
#[derive(Envconfig)]
|
||||
struct Config {
|
||||
@ -39,7 +45,7 @@ impl LogMsg for <teloxide::Bot as teloxide::prelude::Requester>::SendMessage {
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>>{
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenvy::dotenv()?;
|
||||
let config = Config::init_from_env()?;
|
||||
|
||||
@ -53,24 +59,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>>{
|
||||
.branch(
|
||||
Update::filter_message()
|
||||
.branch(
|
||||
dptree::entry().filter_command::<UserCommands>().endpoint(user_command_handler)
|
||||
dptree::entry()
|
||||
.filter_command::<UserCommands>()
|
||||
.endpoint(user_command_handler),
|
||||
)
|
||||
.branch(
|
||||
dptree::entry().filter_command::<SecretCommands>()
|
||||
dptree::entry()
|
||||
.filter_command::<SecretCommands>()
|
||||
.map(move || config.admin_password.clone())
|
||||
.endpoint(secret_command_handler)
|
||||
.endpoint(secret_command_handler),
|
||||
)
|
||||
.branch(
|
||||
dptree::entry().filter_async(async |msg: Message, mut db: DB| {
|
||||
dptree::entry()
|
||||
.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_command::<AdminCommands>().endpoint(admin_command_handler)
|
||||
})
|
||||
.filter_command::<AdminCommands>()
|
||||
.endpoint(admin_command_handler),
|
||||
),
|
||||
)
|
||||
)
|
||||
.branch(
|
||||
Update::filter_message().endpoint(echo)
|
||||
)
|
||||
;
|
||||
.branch(Update::filter_message().endpoint(echo));
|
||||
|
||||
Dispatcher::builder(bot, handler)
|
||||
.dependencies(dptree::deps![db])
|
||||
@ -94,11 +103,12 @@ async fn user_command_handler(
|
||||
UserCommands::Start => {
|
||||
bot.send_photo(msg.chat.id, InputFile::file_id("AgACAgIAAxkBAANRZ-2EJWUdkgwG4tfJfNwut4bssVkAAunyMRvTJ2FLn4FTtVdyfOoBAAMCAANzAAM2BA")).await?;
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
UserCommands::Help => {
|
||||
bot.send_message(msg.chat.id, UserCommands::descriptions().to_string()).await?;
|
||||
bot.send_message(msg.chat.id, UserCommands::descriptions().to_string())
|
||||
.await?;
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
bot.send_message(msg.chat.id, "Not yet implemented").await?;
|
||||
Ok(())
|
||||
@ -106,13 +116,12 @@ async fn user_command_handler(
|
||||
}
|
||||
}
|
||||
|
||||
async fn echo(
|
||||
bot: Bot,
|
||||
msg: Message,
|
||||
) -> Result<(), teloxide::RequestError> {
|
||||
async fn echo(bot: Bot, msg: Message) -> Result<(), teloxide::RequestError> {
|
||||
if let Some(photo) = msg.photo() {
|
||||
println!("File ID: {}", photo[0].file.id);
|
||||
}
|
||||
bot.send_message(msg.chat.id, msg.html_text().unwrap_or("UNWRAP".into())).parse_mode(teloxide::types::ParseMode::Html).await?;
|
||||
bot.send_message(msg.chat.id, msg.html_text().unwrap_or("UNWRAP".into()))
|
||||
.parse_mode(teloxide::types::ParseMode::Html)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user