added command to allow/disallow greet sounds

This commit is contained in:
jude-lafitteIII 2020-05-29 14:35:24 +01:00
parent 086ed93a51
commit d6d825e86d
2 changed files with 66 additions and 38 deletions

View File

@ -6,14 +6,15 @@ pub struct GuildData {
pub name: Option<String>,
pub prefix: String,
pub volume: u8,
pub allow_greets: bool,
}
impl 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,
"
SELECT id, name, prefix, volume
SELECT id, name, prefix, volume, allow_greets
FROM servers
WHERE id = ?
", guild_id
@ -57,6 +58,7 @@ INSERT INTO servers (id, name)
name: Some(guild.name.clone()),
prefix: String::from("?"),
volume: 100,
allow_greets: true
})
}
@ -67,11 +69,12 @@ UPDATE servers
SET
name = ?,
prefix = ?,
volume = ?
volume = ?,
allow_greets = ?
WHERE
id = ?
",
self.name, self.prefix, self.volume, self.id
self.name, self.prefix, self.volume, self.allow_greets, self.id
)
.execute(&db_pool)
.await?;

View File

@ -120,7 +120,7 @@ struct AllUsers;
struct RoleManagedUsers;
#[group]
#[commands(change_prefix, set_allowed_roles)]
#[commands(change_prefix, set_allowed_roles, allow_greet_sounds)]
#[checks(permission_check)]
struct PermissionManagedUsers;
@ -225,10 +225,15 @@ struct Handler;
impl EventHandler for Handler {
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 old.is_none() {
let pool = ctx.data.read().await
.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!(
"
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)
-> Result<(), Box<dyn std::error::Error>> {
@ -1032,3 +1039,21 @@ async fn stop_playing(ctx: &Context, msg: &Message, _args: Args) -> CommandResul
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(())
}