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 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?;

View File

@ -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,54 +225,61 @@ 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) {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
if old.is_none() { if old.is_none() {
let join_id_res = sqlx::query!( let pool = ctx.data.read().await
" .get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
SELECT join_sound_id
FROM users
WHERE user = ? AND join_sound_id IS NOT NULL
",
new.user_id.as_u64()
)
.fetch_one(&pool)
.await;
match join_id_res { let guild_data_opt = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await;
Ok(join_id_record) => {
let join_id = join_id_record.join_sound_id; if let Some(guild_data) = guild_data_opt {
let mut sound = sqlx::query_as_unchecked!( if guild_data.allow_greets {
Sound, let join_id_res = sqlx::query!(
" "
SELECT name, id, plays, public, server_id, uploader_id SELECT join_sound_id
FROM sounds FROM users
WHERE id = ? WHERE user = ? AND join_sound_id IS NOT NULL
", ",
join_id new.user_id.as_u64()
) )
.fetch_one(&pool) .fetch_one(&pool)
.await.unwrap(); .await;
let voice_manager_lock = ctx.data.read().await match join_id_res {
.get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data"); Ok(join_id_record) => {
let join_id = join_id_record.join_sound_id;
let mut sound = sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, plays, public, server_id, uploader_id
FROM sounds
WHERE id = ?
",
join_id
)
.fetch_one(&pool)
.await.unwrap();
let mut voice_manager = voice_manager_lock.lock().await; let voice_manager_lock = ctx.data.read().await
.get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data");
let voice_guilds_lock = ctx.data.read().await let mut voice_manager = voice_manager_lock.lock().await;
.get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data");
let voice_guilds = voice_guilds_lock.lock().await; let voice_guilds_lock = ctx.data.read().await
.get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data");
let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap(); let voice_guilds = voice_guilds_lock.lock().await;
if let Some(handler) = voice_manager.join(guild_id, user_channel) { let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap();
let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await;
if let Some(handler) = voice_manager.join(guild_id, user_channel) {
let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await;
}
}
Err(_) => {}
} }
} }
Err(_) => {}
} }
} }
} }
@ -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(())
}