pikadick/database/
disabled_commands.rs1use crate::database::Database;
2use anyhow::Context;
3use rusqlite::{
4 params,
5 OptionalExtension,
6 TransactionBehavior,
7};
8use serenity::model::prelude::GuildId;
9
10const GET_COMMAND_DISABLED_SQL: &str = include_str!("../../sql/get_command_disabled.sql");
12const SET_COMMAND_DISABLED_SQL: &str = include_str!("../../sql/set_command_disabled.sql");
13
14impl Database {
15 pub async fn set_disabled_command(
20 &self,
21 id: GuildId,
22 cmd: &str,
23 disable: bool,
24 ) -> anyhow::Result<bool> {
25 let cmd = cmd.to_string();
26 self.access_db(move |db| {
27 let txn = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
28 let old_value = txn
29 .prepare_cached(GET_COMMAND_DISABLED_SQL)?
30 .query_row(params![i64::from(id), cmd], |row| row.get(0))
31 .optional()?
32 .unwrap_or(false);
33
34 txn.prepare_cached(SET_COMMAND_DISABLED_SQL)?
35 .execute(params![i64::from(id), cmd, disable])?;
36 txn.commit()
37 .context("failed to update disabled command")
38 .map(|_| old_value)
39 })
40 .await?
41 }
42
43 pub async fn is_command_disabled(&self, id: GuildId, name: &str) -> anyhow::Result<bool> {
45 let name = name.to_string();
46 self.access_db(move |db| {
47 db.prepare_cached(GET_COMMAND_DISABLED_SQL)?
48 .query_row(params![i64::from(id), name], |row| row.get(0))
49 .optional()
50 .context("failed to access db")
51 .map(|row| row.unwrap_or(false))
52 })
53 .await?
54 }
55}