Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split commands code into separate files #5

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions src/cmd_authentication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use std::sync::Arc;

use sqlx::SqlitePool;
use teloxide::{requests::Requester, types::Message, Bot};

use crate::{config::config, HandlerResult};


pub async fn authenticate(
bot: Bot,
msg: Message,
(token, name): (String, String),
db: Arc<SqlitePool>,
) -> HandlerResult {
if token == config().admin_token {
let id = msg.chat.id.to_string();
sqlx::query!(
r#"INSERT INTO admins(telegram_id, "name") VALUES($1, $2)"#,
id,
name
)
.execute(db.as_ref())
.await?;
bot.send_message(msg.chat.id, "Authentification réussie !")
.await?;
} else {
bot.send_message(msg.chat.id, "Le token est incorrect")
.await?;
}

Ok(())
}

pub async fn admin_list(bot: Bot, msg: Message, db: Arc<SqlitePool>) -> HandlerResult {
let admins = sqlx::query!(r#"SELECT "name" FROM admins"#)
.fetch_all(db.as_ref())
.await?;

bot.send_message(
msg.chat.id,
format!(
"Admin(s) actuel(s):\n{}",
admins
.into_iter()
.map(|r| format!(" - {}", r.name))
.collect::<Vec<_>>()
.join("\n"),
),
)
.await?;

Ok(())
}

pub async fn admin_remove(bot: Bot, msg: Message, name: String, db: Arc<SqlitePool>) -> HandlerResult {
let mut tx = db.begin().await?;

if sqlx::query!("SELECT COUNT(*) AS count FROM admins WHERE name = $1", name)
.fetch_one(tx.as_mut())
.await?
.count
== 0
{
bot.send_message(msg.chat.id, format!("{} n'est pas admin", name))
.await?;
return Ok(());
}

sqlx::query!("DELETE FROM admins WHERE name = $1", name)
.execute(tx.as_mut())
.await?;
tx.commit().await?;

bot.send_message(msg.chat.id, format!("{} a été retiré(e) des admins", name))
.await?;

Ok(())
}

pub async fn authorize(bot: Bot, msg: Message, command: String, db: Arc<SqlitePool>) -> HandlerResult {
let mut tx = db.begin().await?;

let chat_id_str = msg.chat.id.to_string();
let already_authorized = sqlx::query!(
r#"SELECT COUNT(*) AS count FROM authorizations WHERE chat_id = $1 AND command = $2"#,
chat_id_str,
command
)
.fetch_one(tx.as_mut())
.await?;

if already_authorized.count == 0 {
sqlx::query!(
r#"INSERT INTO authorizations(command, chat_id) VALUES($1, $2)"#,
command,
chat_id_str
)
.execute(tx.as_mut())
.await?;
}

tx.commit().await?;

bot.send_message(
msg.chat.id,
format!("Ce groupe peut désormais utiliser la commande /{}", command),
)
.await?;
Ok(())
}

pub async fn unauthorize(
bot: Bot,
msg: Message,
command: String,
db: Arc<SqlitePool>,
) -> HandlerResult {
let mut tx = db.begin().await?;

let chat_id_str = msg.chat.id.to_string();
let already_authorized = sqlx::query!(
r#"SELECT COUNT(*) AS count FROM authorizations WHERE chat_id = $1 AND command = $2"#,
chat_id_str,
command
)
.fetch_one(tx.as_mut())
.await?;

if already_authorized.count > 0 {
sqlx::query!(
r#"DELETE FROM authorizations WHERE command = $1 AND chat_id = $2"#,
command,
chat_id_str
)
.execute(tx.as_mut())
.await?;
}

tx.commit().await?;

bot.send_message(
msg.chat.id,
format!(
"Ce groupe ne peut désormais plus utiliser la commande /{}",
command
),
)
.await?;
Ok(())
}

pub async fn authorizations(bot: Bot, msg: Message, db: Arc<SqlitePool>) -> HandlerResult {
let chat_id_str = msg.chat.id.to_string();
let authorizations = sqlx::query!(
r#"SELECT command FROM authorizations WHERE chat_id = $1"#,
chat_id_str
)
.fetch_all(db.as_ref())
.await?;

bot.send_message(
msg.chat.id,
format!(
"Ce groupe peut utiliser les commandes suivantes:\n{}",
authorizations
.into_iter()
.map(|s| format!(" - {}", s.command))
.collect::<Vec<_>>()
.join("\n")
),
)
.await?;

Ok(())
}
21 changes: 21 additions & 0 deletions src/cmd_bureau.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use teloxide::{payloads::SendPollSetters, requests::Requester, types::Message, Bot};

use crate::HandlerResult;

pub async fn bureau(bot: Bot, msg: Message) -> HandlerResult {
bot.send_poll(
msg.chat.id,
"Qui est au bureau ?",
[
"Je suis actuellement au bureau".to_owned(),
"Je suis à proximité du bureau".to_owned(),
"Je compte m'y rendre bientôt".to_owned(),
"J'y suis pas".to_owned(),
"Je suis à Satellite".to_owned(),
"Je suis pas en Suisse".to_owned(),
],
)
.is_anonymous(false)
.await?;
Ok(())
}
Loading
Loading