Add command to play at random from server
This commit is contained in:
parent
d3e00247bd
commit
6cfdc10a6a
@ -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,
|
||||
|
@ -85,6 +85,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
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<dyn std::error::Error + Send + Sync>> {
|
||||
],
|
||||
..cmds::favorite::favorites()
|
||||
},
|
||||
cmds::search::show_random_sounds(),
|
||||
cmds::search::search_sounds(),
|
||||
cmds::stop::stop_playing(),
|
||||
cmds::stop::disconnect(),
|
||||
|
@ -106,7 +106,7 @@ pub async fn play_from_query(
|
||||
user_id: UserId,
|
||||
channel: Option<ChannelId>,
|
||||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user