Split greet sound enabling
This commit is contained in:
		
							
								
								
									
										2
									
								
								.idea/dataSources.local.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								.idea/dataSources.local.xml
									
									
									
										generated
									
									
									
								
							| @@ -1,6 +1,6 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <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"> | ||||
|       <database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="MYSQL" exact-version="0" /> | ||||
|       <secret-storage>master_key</secret-storage> | ||||
|   | ||||
							
								
								
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -2017,7 +2017,7 @@ dependencies = [ | ||||
|  | ||||
| [[package]] | ||||
| name = "soundfx-rs" | ||||
| version = "1.5.6" | ||||
| version = "1.5.7" | ||||
| dependencies = [ | ||||
|  "dashmap", | ||||
|  "env_logger", | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| [package] | ||||
| name = "soundfx-rs" | ||||
| version = "1.5.6" | ||||
| version = "1.5.7" | ||||
| authors = ["jellywx <judesouthworth@pm.me>"] | ||||
| edition = "2018" | ||||
|  | ||||
|   | ||||
							
								
								
									
										1
									
								
								migrations/20230322-disable-user-greets.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/20230322-disable-user-greets.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| ALTER TABLE servers MODIFY COLUMN allow_greets INT NOT NULL DEFAULT 1; | ||||
| @@ -2,7 +2,11 @@ use poise::serenity_prelude::{GuildId, User}; | ||||
|  | ||||
| use crate::{ | ||||
|     cmds::autocomplete_sound, | ||||
|     models::{guild_data::CtxGuildData, join_sound::JoinSoundCtx, sound::SoundCtx}, | ||||
|     models::{ | ||||
|         guild_data::{AllowGreet, CtxGuildData}, | ||||
|         join_sound::JoinSoundCtx, | ||||
|         sound::SoundCtx, | ||||
|     }, | ||||
|     Context, Error, | ||||
| }; | ||||
|  | ||||
| @@ -46,7 +50,7 @@ pub async fn guild_greet_sound(_ctx: Context<'_>) -> Result<(), Error> { | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| /// Set a user's guild-specific join sound | ||||
| /// Set a user's server-specific join sound | ||||
| #[poise::command(slash_command, rename = "set")] | ||||
| pub async fn set_guild_greet_sound( | ||||
|     ctx: Context<'_>, | ||||
| @@ -98,7 +102,7 @@ pub async fn set_guild_greet_sound( | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| /// Unset your global join sound | ||||
| /// Unset a user's server-specific join sound | ||||
| #[poise::command(slash_command, rename = "unset", guild_only = true)] | ||||
| pub async fn unset_guild_greet_sound( | ||||
|     ctx: Context<'_>, | ||||
| @@ -189,7 +193,7 @@ pub async fn unset_user_greet_sound(ctx: Context<'_>) -> Result<(), Error> { | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| /// Disable greet sounds on this server | ||||
| /// Disable all greet sounds on this server | ||||
| #[poise::command( | ||||
|     slash_command, | ||||
|     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; | ||||
|  | ||||
|     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?; | ||||
|     } | ||||
| @@ -211,7 +215,29 @@ pub async fn disable_greet_sound(ctx: Context<'_>) -> Result<(), Error> { | ||||
|     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( | ||||
|     slash_command, | ||||
|     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; | ||||
|  | ||||
|     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?; | ||||
|     } | ||||
|   | ||||
| @@ -11,7 +11,11 @@ use poise::serenity_prelude::{ | ||||
|  | ||||
| use crate::{ | ||||
|     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}, | ||||
|     Data, Error, | ||||
| }; | ||||
| @@ -89,8 +93,14 @@ pub async fn listener(ctx: &Context, event: &poise::Event<'_>, data: &Data) -> R | ||||
|                             allowed_greets = read.allow_greets; | ||||
|                         } | ||||
|  | ||||
|                         if allowed_greets { | ||||
|                             if let Some(join_id) = data.join_sound(new.user_id, new.guild_id).await | ||||
|                         if allowed_greets != AllowGreet::Disabled { | ||||
|                             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!( | ||||
|                                     Sound, | ||||
|   | ||||
| @@ -103,6 +103,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||||
|                         subcommands: vec![ | ||||
|                             cmds::settings::set_guild_greet_sound(), | ||||
|                             cmds::settings::unset_guild_greet_sound(), | ||||
|                             cmds::settings::enable_guild_greet_sound(), | ||||
|                         ], | ||||
|                         ..cmds::settings::guild_greet_sound() | ||||
|                     }, | ||||
|   | ||||
| @@ -1,17 +1,25 @@ | ||||
| use std::sync::Arc; | ||||
|  | ||||
| use poise::serenity_prelude::{async_trait, model::id::GuildId}; | ||||
| use sqlx::Executor; | ||||
| use sqlx::{Executor, Type}; | ||||
| use tokio::sync::RwLock; | ||||
|  | ||||
| use crate::{Context, Data, Database}; | ||||
|  | ||||
| #[derive(Copy, Clone, Type, PartialEq)] | ||||
| #[repr(i32)] | ||||
| pub enum AllowGreet { | ||||
|     Enabled = 1, | ||||
|     GuildOnly = 0, | ||||
|     Disabled = -1, | ||||
| } | ||||
|  | ||||
| #[derive(Clone)] | ||||
| pub struct GuildData { | ||||
|     pub id: u64, | ||||
|     pub prefix: String, | ||||
|     pub volume: u8, | ||||
|     pub allow_greets: bool, | ||||
|     pub allow_greets: AllowGreet, | ||||
|     pub allowed_role: Option<u64>, | ||||
| } | ||||
|  | ||||
| @@ -109,7 +117,7 @@ INSERT INTO servers (id) | ||||
|             id: guild_id.as_u64().to_owned(), | ||||
|             prefix: String::from("?"), | ||||
|             volume: 100, | ||||
|             allow_greets: true, | ||||
|             allow_greets: AllowGreet::Enabled, | ||||
|             allowed_role: None, | ||||
|         }) | ||||
|     } | ||||
|   | ||||
| @@ -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,7 +43,24 @@ 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 | ||||
| @@ -49,7 +72,8 @@ SELECT join_sound_id | ||||
|                         guild_id.map(|g| g.0) | ||||
|                     ) | ||||
|                     .fetch_one(&self.database) | ||||
|                 .await; | ||||
|                     .await | ||||
|                 }; | ||||
|  | ||||
|                 if let Ok(row) = join_id_res { | ||||
|                     Some(row.join_sound_id) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user