discord-kurante/src/commands/heck.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

2020-05-02 18:37:34 +00:00
use crate::{util::debug_say, DbConnPool};
2020-05-02 02:17:53 +00:00
use serenity::framework::standard::{macros::command, CommandResult};
use serenity::model::channel::Message;
2020-05-03 03:58:15 +00:00
use serenity::{async_trait, prelude::Context};
use sqlx::Error;
2020-05-02 02:17:53 +00:00
#[command]
2020-05-02 18:37:34 +00:00
async fn heck(ctx: &Context, msg: &Message) -> CommandResult {
2020-05-02 02:17:53 +00:00
let db_pool = ctx.data.clone();
2020-05-03 03:56:04 +00:00
let db_pool = db_pool.read().await;
2020-05-02 05:04:51 +00:00
let db_pool = db_pool
2020-05-03 03:56:04 +00:00
.get::<DbConnPool>()
2020-05-02 05:04:51 +00:00
.expect("No db pool in context?!");
2020-05-02 02:17:53 +00:00
let value = db_pool.get_heck().await;
2020-05-02 18:37:34 +00:00
debug_say(
msg,
ctx,
format!("This command has been hecked {} times", value?),
)
.await?;
2020-05-02 02:17:53 +00:00
Ok(())
}
2020-05-03 03:58:15 +00:00
#[async_trait]
trait HeckQueries {
async fn get_heck(&self) -> Result<i32, Error>;
}
#[async_trait]
impl HeckQueries for DbConnPool {
async fn get_heck(&self) -> Result<i32, Error> {
sqlx::query!("UPDATE Heck SET count = count + 1")
.execute(&self.pool)
.await?;
Ok(sqlx::query!("SELECT count FROM Heck")
.fetch_one(&self.pool)
.await?
.count)
}
}