Initial commit
This commit is contained in:
commit
6bcedfd0cd
7 changed files with 420 additions and 0 deletions
179
src/commands.rs
Normal file
179
src/commands.rs
Normal file
|
@ -0,0 +1,179 @@
|
|||
use serenity::client::Context;
|
||||
|
||||
use serenity::model::interactions::application_command::{
|
||||
ApplicationCommandInteraction, ApplicationCommandInteractionDataOptionValue,
|
||||
};
|
||||
use serenity::utils::{EmbedMessageBuilding, MessageBuilder};
|
||||
use songbird::create_player;
|
||||
|
||||
use crate::{CurrentVolume, CurrentlyPlayingTrack};
|
||||
|
||||
pub async fn join(ctx: &Context, command: &ApplicationCommandInteraction) -> String {
|
||||
let guild_id = command.guild_id.unwrap();
|
||||
let guild = guild_id.to_guild_cached(&ctx.cache).await.unwrap();
|
||||
|
||||
let channel_id = guild
|
||||
.voice_states
|
||||
.get(&command.user.id)
|
||||
.and_then(|voice_state| voice_state.channel_id);
|
||||
|
||||
let connect_to = match channel_id {
|
||||
Some(channel) => channel,
|
||||
None => {
|
||||
return "You're not in a voice channel".to_string();
|
||||
}
|
||||
};
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
let _handler = manager.join(guild_id, connect_to).await;
|
||||
|
||||
"Joining your channel".to_string()
|
||||
}
|
||||
|
||||
pub async fn leave(ctx: &Context, command: &ApplicationCommandInteraction) -> String {
|
||||
let guild_id = command.guild_id.unwrap();
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
let has_handler = manager.get(guild_id).is_some();
|
||||
|
||||
if has_handler {
|
||||
if let Err(e) = manager.remove(guild_id).await {
|
||||
return format!("Failed: {:?}", e);
|
||||
}
|
||||
|
||||
"Left voice channel".to_string()
|
||||
} else {
|
||||
"I'm not in a voice channel".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn play(ctx: &mut Context, command: &ApplicationCommandInteraction) -> String {
|
||||
let options = command
|
||||
.data
|
||||
.options
|
||||
.get(0)
|
||||
.expect("Expected url option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected url object");
|
||||
|
||||
let url = match options {
|
||||
ApplicationCommandInteractionDataOptionValue::String(url) => url,
|
||||
_ => {
|
||||
return "Must provide a URL to a video or audio".to_string();
|
||||
}
|
||||
};
|
||||
|
||||
if !url.starts_with("http") {
|
||||
return "Must provide a valid URL".to_string();
|
||||
}
|
||||
|
||||
let guild_id = command.guild_id.unwrap();
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
let source = match songbird::ytdl(&url).await {
|
||||
Ok(source) => source,
|
||||
Err(why) => {
|
||||
println!("Err starting source: {:?}", why);
|
||||
|
||||
return "Error sourcing ffmpeg".to_string();
|
||||
}
|
||||
};
|
||||
|
||||
let message = {
|
||||
if let Some(title) = &source.metadata.title {
|
||||
let mut msg = MessageBuilder::new();
|
||||
msg.push_line("Playing song:");
|
||||
msg.push_named_link(title, url);
|
||||
msg.build()
|
||||
} else {
|
||||
"Playing song".to_string()
|
||||
}
|
||||
};
|
||||
|
||||
let (mut audio, track_handle) = create_player(source);
|
||||
|
||||
let mut data = ctx.data.write().await;
|
||||
|
||||
let current_track = data.get_mut::<CurrentlyPlayingTrack>().unwrap();
|
||||
*current_track = Some(track_handle);
|
||||
|
||||
let volume = data.get::<CurrentVolume>().unwrap();
|
||||
|
||||
audio.set_volume(*volume);
|
||||
handler.play_only(audio);
|
||||
|
||||
message
|
||||
} else {
|
||||
"Not in a voice channel to play in".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn stop(ctx: &Context, command: &ApplicationCommandInteraction) -> String {
|
||||
let guild_id = command.guild_id.unwrap();
|
||||
|
||||
let manager = songbird::get(ctx)
|
||||
.await
|
||||
.expect("Songbird Voice client placed in at initialisation.")
|
||||
.clone();
|
||||
|
||||
if let Some(handler_lock) = manager.get(guild_id) {
|
||||
let mut handler = handler_lock.lock().await;
|
||||
|
||||
handler.stop();
|
||||
|
||||
"Stopping song".to_string()
|
||||
} else {
|
||||
"Not in a voice channel to play in".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_volume(ctx: &mut Context, command: &ApplicationCommandInteraction) -> String {
|
||||
let options = command
|
||||
.data
|
||||
.options
|
||||
.get(0)
|
||||
.expect("Expected volume option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected volume object");
|
||||
|
||||
let volume = match options {
|
||||
ApplicationCommandInteractionDataOptionValue::Number(volume) => *volume,
|
||||
_ => {
|
||||
return "Must provide a volume level".to_string();
|
||||
}
|
||||
};
|
||||
|
||||
if !(0.0..=100.0).contains(&volume) {
|
||||
return "Must provide a value between 0 and 100".to_string();
|
||||
}
|
||||
|
||||
let mut data = ctx.data.write().await;
|
||||
|
||||
let current_volume = data.get_mut::<CurrentVolume>().unwrap();
|
||||
let new_volume = (volume / 100.0) as f32;
|
||||
*current_volume = new_volume;
|
||||
|
||||
let current_track = data.get::<CurrentlyPlayingTrack>().unwrap();
|
||||
if let Some(track) = current_track {
|
||||
track.set_volume(new_volume).unwrap();
|
||||
format!("Setting volume to {}%", volume)
|
||||
} else {
|
||||
"No track is currently playing".to_string()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue