added command to allow/disallow greet sounds
This commit is contained in:
		@@ -6,14 +6,15 @@ pub struct GuildData {
 | 
				
			|||||||
    pub name: Option<String>,
 | 
					    pub name: Option<String>,
 | 
				
			||||||
    pub prefix: String,
 | 
					    pub prefix: String,
 | 
				
			||||||
    pub volume: u8,
 | 
					    pub volume: u8,
 | 
				
			||||||
 | 
					    pub allow_greets: bool,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl GuildData {
 | 
					impl GuildData {
 | 
				
			||||||
    pub async fn get_from_id(guild_id: u64, db_pool: MySqlPool) -> Option<GuildData> {
 | 
					    pub async fn get_from_id(guild_id: u64, db_pool: MySqlPool) -> Option<GuildData> {
 | 
				
			||||||
        let guild = sqlx::query_as!(
 | 
					        let guild = sqlx::query_as_unchecked!(
 | 
				
			||||||
            GuildData,
 | 
					            GuildData,
 | 
				
			||||||
            "
 | 
					            "
 | 
				
			||||||
SELECT id, name, prefix, volume
 | 
					SELECT id, name, prefix, volume, allow_greets
 | 
				
			||||||
    FROM servers
 | 
					    FROM servers
 | 
				
			||||||
    WHERE id = ?
 | 
					    WHERE id = ?
 | 
				
			||||||
            ", guild_id
 | 
					            ", guild_id
 | 
				
			||||||
@@ -57,6 +58,7 @@ INSERT INTO servers (id, name)
 | 
				
			|||||||
            name: Some(guild.name.clone()),
 | 
					            name: Some(guild.name.clone()),
 | 
				
			||||||
            prefix: String::from("?"),
 | 
					            prefix: String::from("?"),
 | 
				
			||||||
            volume: 100,
 | 
					            volume: 100,
 | 
				
			||||||
 | 
					            allow_greets: true
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -67,11 +69,12 @@ UPDATE servers
 | 
				
			|||||||
SET
 | 
					SET
 | 
				
			||||||
    name = ?,
 | 
					    name = ?,
 | 
				
			||||||
    prefix = ?,
 | 
					    prefix = ?,
 | 
				
			||||||
    volume = ?
 | 
					    volume = ?,
 | 
				
			||||||
 | 
					    allow_greets = ?
 | 
				
			||||||
WHERE
 | 
					WHERE
 | 
				
			||||||
    id = ?
 | 
					    id = ?
 | 
				
			||||||
            ",
 | 
					            ",
 | 
				
			||||||
            self.name, self.prefix, self.volume, self.id
 | 
					            self.name, self.prefix, self.volume, self.allow_greets, self.id
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
            .execute(&db_pool)
 | 
					            .execute(&db_pool)
 | 
				
			||||||
            .await?;
 | 
					            .await?;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										29
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -120,7 +120,7 @@ struct AllUsers;
 | 
				
			|||||||
struct RoleManagedUsers;
 | 
					struct RoleManagedUsers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[group]
 | 
					#[group]
 | 
				
			||||||
#[commands(change_prefix, set_allowed_roles)]
 | 
					#[commands(change_prefix, set_allowed_roles, allow_greet_sounds)]
 | 
				
			||||||
#[checks(permission_check)]
 | 
					#[checks(permission_check)]
 | 
				
			||||||
struct PermissionManagedUsers;
 | 
					struct PermissionManagedUsers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -225,10 +225,15 @@ struct Handler;
 | 
				
			|||||||
impl EventHandler for Handler {
 | 
					impl EventHandler for Handler {
 | 
				
			||||||
    async fn voice_state_update(&self, ctx: Context, guild_id_opt: Option<GuildId>, old: Option<VoiceState>, new: VoiceState) {
 | 
					    async fn voice_state_update(&self, ctx: Context, guild_id_opt: Option<GuildId>, old: Option<VoiceState>, new: VoiceState) {
 | 
				
			||||||
        if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) {
 | 
					        if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if old.is_none() {
 | 
				
			||||||
                let pool = ctx.data.read().await
 | 
					                let pool = ctx.data.read().await
 | 
				
			||||||
                    .get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
 | 
					                    .get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if old.is_none() {
 | 
					                let guild_data_opt = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if let Some(guild_data) = guild_data_opt {
 | 
				
			||||||
 | 
					                    if guild_data.allow_greets {
 | 
				
			||||||
                        let join_id_res = sqlx::query!(
 | 
					                        let join_id_res = sqlx::query!(
 | 
				
			||||||
                            "
 | 
					                            "
 | 
				
			||||||
        SELECT join_sound_id
 | 
					        SELECT join_sound_id
 | 
				
			||||||
@@ -278,6 +283,8 @@ SELECT name, id, plays, public, server_id, uploader_id
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashMap<GuildId, u8>>, pool: MySqlPool)
 | 
					async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashMap<GuildId, u8>>, pool: MySqlPool)
 | 
				
			||||||
    -> Result<(), Box<dyn std::error::Error>> {
 | 
					    -> Result<(), Box<dyn std::error::Error>> {
 | 
				
			||||||
@@ -1032,3 +1039,21 @@ async fn stop_playing(ctx: &Context, msg: &Message, _args: Args) -> CommandResul
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    Ok(())
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[command("allow_greet")]
 | 
				
			||||||
 | 
					async fn allow_greet_sounds(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
 | 
				
			||||||
 | 
					    let pool = ctx.data.read().await
 | 
				
			||||||
 | 
					        .get::<SQLPool>().cloned().expect("Could not acquire SQL pool from data");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let guild_data_opt = GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool.clone()).await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if let Some(mut guild_data) = guild_data_opt {
 | 
				
			||||||
 | 
					        guild_data.allow_greets = !guild_data.allow_greets;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        guild_data.commit(pool).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        msg.channel_id.say(&ctx, format!("Greet sounds have been {}abled in this server", if guild_data.allow_greets { "en" } else { "dis" })).await?;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ok(())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user