From 6cfdc10a6a13d0eeccaf2a6249004e41b8addc35 Mon Sep 17 00:00:00 2001 From: jude Date: Sat, 21 Oct 2023 19:09:01 +0100 Subject: [PATCH] Add command to play at random from server --- src/cmds/play.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 2 +- src/utils.rs | 4 +-- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/cmds/play.rs b/src/cmds/play.rs index 64a50d7..fd800bf 100644 --- a/src/cmds/play.rs +++ b/src/cmds/play.rs @@ -1,3 +1,5 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + use poise::serenity_prelude::{ builder::CreateActionRow, model::application::component::ButtonStyle, GuildChannel, ReactionType, @@ -6,7 +8,7 @@ use poise::serenity_prelude::{ use crate::{ cmds::autocomplete_sound, models::{guild_data::CtxGuildData, sound::SoundCtx}, - utils::{join_channel, play_from_query, queue_audio}, + utils::{join_channel, play_audio, play_from_query, queue_audio}, Context, Error, }; @@ -42,6 +44,70 @@ pub async fn play( Ok(()) } +/// Play a random sound from this server +#[poise::command( + slash_command, + rename = "random", + default_member_permissions = "SPEAK", + guild_only = true +)] +pub async fn play_random( + ctx: Context<'_>, + #[description = "Channel to play in (default: your current voice channel)"] + #[channel_types("Voice")] + channel: Option, +) -> Result<(), Error> { + ctx.defer().await?; + + let guild = ctx.guild().unwrap(); + let channel_to_join = channel.map(|c| c.id).or_else(|| { + guild + .voice_states + .get(&ctx.author().id) + .and_then(|voice_state| voice_state.channel_id) + }); + + match channel_to_join { + Some(channel) => { + let (call_handler, _) = + join_channel(ctx.serenity_context(), guild.clone(), channel).await; + + let sounds = ctx.data().guild_sounds(guild.id, None).await?; + + let ts = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + + // This is far cheaper and easier than using an RNG. No reason to use a full RNG here + // anyway. + match sounds.get(ts.subsec_micros() as usize % sounds.len()) { + Some(sound) => { + let guild_data = ctx.data().guild_data(guild.id).await.unwrap(); + let mut lock = call_handler.lock().await; + + play_audio( + sound, + guild_data.read().await.volume, + &mut lock, + &ctx.data().database, + false, + ) + .await + .unwrap(); + } + + None => { + ctx.say("No sounds in this server!").await?; + } + } + } + + None => { + ctx.say("You are not in a voice chat!").await?; + } + } + + Ok(()) +} + /// Play up to 25 sounds on queue #[poise::command( slash_command, diff --git a/src/main.rs b/src/main.rs index 6dc6a25..d341c35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,6 +85,7 @@ async fn main() -> Result<(), Box> { cmds::manage::download_file(), cmds::manage::delete_sound(), cmds::play::play(), + cmds::play::play_random(), cmds::play::queue_play(), cmds::play::loop_play(), cmds::play::soundboard(), @@ -103,7 +104,6 @@ async fn main() -> Result<(), Box> { ], ..cmds::favorite::favorites() }, - cmds::search::show_random_sounds(), cmds::search::search_sounds(), cmds::stop::stop_playing(), cmds::stop::disconnect(), diff --git a/src/utils.rs b/src/utils.rs index ccc7c44..1fdb8a5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -106,7 +106,7 @@ pub async fn play_from_query( user_id: UserId, channel: Option, query: &str, - loop_: bool, + r#loop: bool, ) -> String { let guild_id = guild.id; @@ -141,7 +141,7 @@ pub async fn play_from_query( guild_data.read().await.volume, &mut lock, &data.database, - loop_, + r#loop, ) .await .unwrap();