Split greet sound enabling

This commit is contained in:
jude
2023-03-22 18:31:49 +00:00
parent 208440a7ff
commit 4edcee2567
9 changed files with 93 additions and 23 deletions

View File

@ -8,6 +8,7 @@ pub trait JoinSoundCtx {
&self,
user_id: U,
guild_id: Option<G>,
guild_only: bool,
) -> Option<u32>;
async fn update_join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
&self,
@ -17,12 +18,17 @@ pub trait JoinSoundCtx {
) -> Result<(), sqlx::Error>;
}
struct JoinSound {
join_sound_id: u32,
}
#[async_trait]
impl JoinSoundCtx for Data {
async fn join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
&self,
user_id: U,
guild_id: Option<G>,
guild_only: bool,
) -> Option<u32> {
let user_id = user_id.into();
let guild_id = guild_id.map(|g| g.into());
@ -37,19 +43,37 @@ impl JoinSoundCtx for Data {
join_sound_id
} else {
let join_sound_id = {
let join_id_res = sqlx::query!(
"
let join_id_res = if guild_only {
sqlx::query_as!(
JoinSound,
"
SELECT join_sound_id
FROM join_sounds
WHERE user = ?
AND guild = ?
ORDER BY guild IS NULL
",
user_id.as_u64(),
guild_id.map(|g| g.0)
)
.fetch_one(&self.database)
.await
} else {
sqlx::query_as!(
JoinSound,
"
SELECT join_sound_id
FROM join_sounds
WHERE user = ?
AND (guild IS NULL OR guild = ?)
ORDER BY guild IS NULL
",
user_id.as_u64(),
guild_id.map(|g| g.0)
)
.fetch_one(&self.database)
.await;
user_id.as_u64(),
guild_id.map(|g| g.0)
)
.fetch_one(&self.database)
.await
};
if let Ok(row) = join_id_res {
Some(row.join_sound_id)