Add command to play at random from server

This commit is contained in:
jude 2023-10-21 19:09:01 +01:00
parent d3e00247bd
commit 6cfdc10a6a
3 changed files with 70 additions and 4 deletions

View File

@ -1,3 +1,5 @@
use std::time::{SystemTime, UNIX_EPOCH};
use poise::serenity_prelude::{ use poise::serenity_prelude::{
builder::CreateActionRow, model::application::component::ButtonStyle, GuildChannel, builder::CreateActionRow, model::application::component::ButtonStyle, GuildChannel,
ReactionType, ReactionType,
@ -6,7 +8,7 @@ use poise::serenity_prelude::{
use crate::{ use crate::{
cmds::autocomplete_sound, cmds::autocomplete_sound,
models::{guild_data::CtxGuildData, sound::SoundCtx}, 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, Context, Error,
}; };
@ -42,6 +44,70 @@ pub async fn play(
Ok(()) 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 /// Play up to 25 sounds on queue
#[poise::command( #[poise::command(
slash_command, slash_command,

View File

@ -85,6 +85,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
cmds::manage::download_file(), cmds::manage::download_file(),
cmds::manage::delete_sound(), cmds::manage::delete_sound(),
cmds::play::play(), cmds::play::play(),
cmds::play::play_random(),
cmds::play::queue_play(), cmds::play::queue_play(),
cmds::play::loop_play(), cmds::play::loop_play(),
cmds::play::soundboard(), cmds::play::soundboard(),
@ -103,7 +104,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
], ],
..cmds::favorite::favorites() ..cmds::favorite::favorites()
}, },
cmds::search::show_random_sounds(),
cmds::search::search_sounds(), cmds::search::search_sounds(),
cmds::stop::stop_playing(), cmds::stop::stop_playing(),
cmds::stop::disconnect(), cmds::stop::disconnect(),

View File

@ -106,7 +106,7 @@ pub async fn play_from_query(
user_id: UserId, user_id: UserId,
channel: Option<ChannelId>, channel: Option<ChannelId>,
query: &str, query: &str,
loop_: bool, r#loop: bool,
) -> String { ) -> String {
let guild_id = guild.id; let guild_id = guild.id;
@ -141,7 +141,7 @@ pub async fn play_from_query(
guild_data.read().await.volume, guild_data.read().await.volume,
&mut lock, &mut lock,
&data.database, &data.database,
loop_, r#loop,
) )
.await .await
.unwrap(); .unwrap();