Add command to play at random from server

This commit is contained in:
claude Bot committed 2023-10-21 19:09:01 +01:00
1 parent d3e00247bd
commit 6cfdc10a6a
3 files changed
+70 -4

No files matched your search

+67 -1
View File
@@ -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<GuildChannel>,
) -> 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,