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

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="dataSourceStorageLocal" created-in="CL-222.4345.21"> <component name="dataSourceStorageLocal" created-in="CL-223.8836.42">
<data-source name="MySQL for 5.1 - soundfx@localhost" uuid="1067c1d0-1386-4a39-b3f5-6d48d6f279eb"> <data-source name="MySQL for 5.1 - soundfx@localhost" uuid="1067c1d0-1386-4a39-b3f5-6d48d6f279eb">
<database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="MYSQL" exact-version="0" /> <database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="MYSQL" exact-version="0" />
<secret-storage>master_key</secret-storage> <secret-storage>master_key</secret-storage>

2
Cargo.lock generated
View File

@ -2017,7 +2017,7 @@ dependencies = [
[[package]] [[package]]
name = "soundfx-rs" name = "soundfx-rs"
version = "1.5.6" version = "1.5.7"
dependencies = [ dependencies = [
"dashmap", "dashmap",
"env_logger", "env_logger",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "soundfx-rs" name = "soundfx-rs"
version = "1.5.6" version = "1.5.7"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -0,0 +1 @@
ALTER TABLE servers MODIFY COLUMN allow_greets INT NOT NULL DEFAULT 1;

View File

@ -2,7 +2,11 @@ use poise::serenity_prelude::{GuildId, User};
use crate::{ use crate::{
cmds::autocomplete_sound, cmds::autocomplete_sound,
models::{guild_data::CtxGuildData, join_sound::JoinSoundCtx, sound::SoundCtx}, models::{
guild_data::{AllowGreet, CtxGuildData},
join_sound::JoinSoundCtx,
sound::SoundCtx,
},
Context, Error, Context, Error,
}; };
@ -46,7 +50,7 @@ pub async fn guild_greet_sound(_ctx: Context<'_>) -> Result<(), Error> {
Ok(()) Ok(())
} }
/// Set a user's guild-specific join sound /// Set a user's server-specific join sound
#[poise::command(slash_command, rename = "set")] #[poise::command(slash_command, rename = "set")]
pub async fn set_guild_greet_sound( pub async fn set_guild_greet_sound(
ctx: Context<'_>, ctx: Context<'_>,
@ -98,7 +102,7 @@ pub async fn set_guild_greet_sound(
Ok(()) Ok(())
} }
/// Unset your global join sound /// Unset a user's server-specific join sound
#[poise::command(slash_command, rename = "unset", guild_only = true)] #[poise::command(slash_command, rename = "unset", guild_only = true)]
pub async fn unset_guild_greet_sound( pub async fn unset_guild_greet_sound(
ctx: Context<'_>, ctx: Context<'_>,
@ -189,7 +193,7 @@ pub async fn unset_user_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
Ok(()) Ok(())
} }
/// Disable greet sounds on this server /// Disable all greet sounds on this server
#[poise::command( #[poise::command(
slash_command, slash_command,
rename = "disable", rename = "disable",
@ -200,7 +204,7 @@ pub async fn disable_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await; let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await;
if let Ok(guild_data) = guild_data_opt { if let Ok(guild_data) = guild_data_opt {
guild_data.write().await.allow_greets = false; guild_data.write().await.allow_greets = AllowGreet::Disabled;
guild_data.read().await.commit(&ctx.data().database).await?; guild_data.read().await.commit(&ctx.data().database).await?;
} }
@ -211,7 +215,29 @@ pub async fn disable_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
Ok(()) Ok(())
} }
/// Enable greet sounds on this server /// Enable only server greet sounds on this server
#[poise::command(
slash_command,
rename = "enable",
guild_only = true,
required_permissions = "MANAGE_GUILD"
)]
pub async fn enable_guild_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await;
if let Ok(guild_data) = guild_data_opt {
guild_data.write().await.allow_greets = AllowGreet::GuildOnly;
guild_data.read().await.commit(&ctx.data().database).await?;
}
ctx.say("Greet sounds have been partially enable in this server. Use \"/greet server set\" to configure server greet sounds.")
.await?;
Ok(())
}
/// Enable all greet sounds on this server
#[poise::command( #[poise::command(
slash_command, slash_command,
rename = "enable", rename = "enable",
@ -222,7 +248,7 @@ pub async fn enable_greet_sound(ctx: Context<'_>) -> Result<(), Error> {
let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await; let guild_data_opt = ctx.guild_data(ctx.guild_id().unwrap()).await;
if let Ok(guild_data) = guild_data_opt { if let Ok(guild_data) = guild_data_opt {
guild_data.write().await.allow_greets = true; guild_data.write().await.allow_greets = AllowGreet::Enabled;
guild_data.read().await.commit(&ctx.data().database).await?; guild_data.read().await.commit(&ctx.data().database).await?;
} }

View File

@ -11,7 +11,11 @@ use poise::serenity_prelude::{
use crate::{ use crate::{
cmds::search::SoundPager, cmds::search::SoundPager,
models::{guild_data::CtxGuildData, join_sound::JoinSoundCtx, sound::Sound}, models::{
guild_data::{AllowGreet, CtxGuildData},
join_sound::JoinSoundCtx,
sound::Sound,
},
utils::{join_channel, play_audio, play_from_query}, utils::{join_channel, play_audio, play_from_query},
Data, Error, Data, Error,
}; };
@ -89,8 +93,14 @@ pub async fn listener(ctx: &Context, event: &poise::Event<'_>, data: &Data) -> R
allowed_greets = read.allow_greets; allowed_greets = read.allow_greets;
} }
if allowed_greets { if allowed_greets != AllowGreet::Disabled {
if let Some(join_id) = data.join_sound(new.user_id, new.guild_id).await if let Some(join_id) = data
.join_sound(
new.user_id,
new.guild_id,
allowed_greets == AllowGreet::GuildOnly,
)
.await
{ {
let mut sound = sqlx::query_as_unchecked!( let mut sound = sqlx::query_as_unchecked!(
Sound, Sound,

View File

@ -103,6 +103,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
subcommands: vec![ subcommands: vec![
cmds::settings::set_guild_greet_sound(), cmds::settings::set_guild_greet_sound(),
cmds::settings::unset_guild_greet_sound(), cmds::settings::unset_guild_greet_sound(),
cmds::settings::enable_guild_greet_sound(),
], ],
..cmds::settings::guild_greet_sound() ..cmds::settings::guild_greet_sound()
}, },

View File

@ -1,17 +1,25 @@
use std::sync::Arc; use std::sync::Arc;
use poise::serenity_prelude::{async_trait, model::id::GuildId}; use poise::serenity_prelude::{async_trait, model::id::GuildId};
use sqlx::Executor; use sqlx::{Executor, Type};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use crate::{Context, Data, Database}; use crate::{Context, Data, Database};
#[derive(Copy, Clone, Type, PartialEq)]
#[repr(i32)]
pub enum AllowGreet {
Enabled = 1,
GuildOnly = 0,
Disabled = -1,
}
#[derive(Clone)] #[derive(Clone)]
pub struct GuildData { pub struct GuildData {
pub id: u64, pub id: u64,
pub prefix: String, pub prefix: String,
pub volume: u8, pub volume: u8,
pub allow_greets: bool, pub allow_greets: AllowGreet,
pub allowed_role: Option<u64>, pub allowed_role: Option<u64>,
} }
@ -109,7 +117,7 @@ INSERT INTO servers (id)
id: guild_id.as_u64().to_owned(), id: guild_id.as_u64().to_owned(),
prefix: String::from("?"), prefix: String::from("?"),
volume: 100, volume: 100,
allow_greets: true, allow_greets: AllowGreet::Enabled,
allowed_role: None, allowed_role: None,
}) })
} }

View File

@ -8,6 +8,7 @@ pub trait JoinSoundCtx {
&self, &self,
user_id: U, user_id: U,
guild_id: Option<G>, guild_id: Option<G>,
guild_only: bool,
) -> Option<u32>; ) -> Option<u32>;
async fn update_join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>( async fn update_join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
&self, &self,
@ -17,12 +18,17 @@ pub trait JoinSoundCtx {
) -> Result<(), sqlx::Error>; ) -> Result<(), sqlx::Error>;
} }
struct JoinSound {
join_sound_id: u32,
}
#[async_trait] #[async_trait]
impl JoinSoundCtx for Data { impl JoinSoundCtx for Data {
async fn join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>( async fn join_sound<U: Into<UserId> + Send + Sync, G: Into<GuildId> + Send + Sync>(
&self, &self,
user_id: U, user_id: U,
guild_id: Option<G>, guild_id: Option<G>,
guild_only: bool,
) -> Option<u32> { ) -> Option<u32> {
let user_id = user_id.into(); let user_id = user_id.into();
let guild_id = guild_id.map(|g| g.into()); let guild_id = guild_id.map(|g| g.into());
@ -37,7 +43,24 @@ impl JoinSoundCtx for Data {
join_sound_id join_sound_id
} else { } else {
let join_sound_id = { 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 SELECT join_sound_id
FROM join_sounds FROM join_sounds
@ -49,7 +72,8 @@ SELECT join_sound_id
guild_id.map(|g| g.0) guild_id.map(|g| g.0)
) )
.fetch_one(&self.database) .fetch_one(&self.database)
.await; .await
};
if let Ok(row) = join_id_res { if let Ok(row) = join_id_res {
Some(row.join_sound_id) Some(row.join_sound_id)