Cleanup commands
This commit is contained in:
@@ -17,101 +17,68 @@ pub mod skip;
|
|||||||
pub mod stop;
|
pub mod stop;
|
||||||
pub mod volume;
|
pub mod volume;
|
||||||
|
|
||||||
|
pub async fn get_songbird(ctx: &Context) -> Arc<Songbird> {
|
||||||
|
songbird::get(ctx)
|
||||||
|
.await
|
||||||
|
.expect("Songbird Voice client placed in at initialization")
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds a voice channel that the user is currently in, and attempts to join it.
|
* Finds a voice channel that the user is currently in, and attempts to join it.
|
||||||
*/
|
*/
|
||||||
pub async fn join_by_user(
|
pub async fn join_voice_channel(
|
||||||
cache: &Arc<Cache>,
|
cache: &Arc<Cache>,
|
||||||
manager: &Arc<Songbird>,
|
manager: &Arc<Songbird>,
|
||||||
guild_id: &GuildId,
|
guild_id: &GuildId,
|
||||||
user: &User,
|
user: &User,
|
||||||
) -> SirenResult<ChannelId> {
|
) -> SirenResult<ChannelId> {
|
||||||
let channel_id = match find_voice_channel(cache, guild_id, user) {
|
let channel_id = find_voice_channel(cache, guild_id, user)?;
|
||||||
Ok(channel) => channel,
|
|
||||||
Err(err) => return Err(SirenError::new(500, err.to_string()))
|
|
||||||
};
|
|
||||||
|
|
||||||
join_voice_channel(manager, guild_id, &channel_id).await?;
|
|
||||||
Ok(channel_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Joins a voice channel.
|
|
||||||
*/
|
|
||||||
async fn join_voice_channel(
|
|
||||||
manager: &Arc<Songbird>,
|
|
||||||
guild_id: &GuildId,
|
|
||||||
channel_id: &ChannelId,
|
|
||||||
) -> SirenResult<()> {
|
|
||||||
log::debug!("<{}> Joining channel {}", guild_id.get(), channel_id.get());
|
log::debug!("<{}> Joining channel {}", guild_id.get(), channel_id.get());
|
||||||
manager.join(guild_id.to_owned(), channel_id.to_owned()).await?;
|
manager.join(guild_id.to_owned(), channel_id.to_owned()).await?;
|
||||||
Ok(())
|
Ok(channel_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Leaves a voice channel.
|
* Leaves a voice channel.
|
||||||
*/
|
*/
|
||||||
pub async fn leave_voice_channel(
|
pub async fn leave_voice_channel(
|
||||||
manager: Arc<Songbird>,
|
manager: &Arc<Songbird>,
|
||||||
guild_id_option: &Option<GuildId>,
|
guild_id: &GuildId,
|
||||||
) -> Result<(), String> {
|
) -> SirenResult<()> {
|
||||||
let guild_id = match guild_id_option {
|
if manager.get(guild_id.to_owned()).is_some() {
|
||||||
Some(g) => g,
|
|
||||||
None => {
|
|
||||||
return Err(format!("{}", "No guild ID set"));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if manager.get(*guild_id).is_some() {
|
|
||||||
log::debug!("<{}> Disconnecting from channel", guild_id.get());
|
log::debug!("<{}> Disconnecting from channel", guild_id.get());
|
||||||
if let Err(e) = manager.remove(*guild_id).await {
|
manager.remove(*guild_id).await?;
|
||||||
return Err(format!("{}", e));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds a voice channel that the user is currently in.
|
|
||||||
*/
|
|
||||||
fn find_voice_channel(
|
|
||||||
cache: &Arc<Cache>,
|
|
||||||
guild_id: &GuildId,
|
|
||||||
user: &User,
|
|
||||||
) -> Result<ChannelId, String> {
|
|
||||||
let guild = match guild_id.to_guild_cached(cache) {
|
|
||||||
Some(g) => g,
|
|
||||||
None => return Err(format!("Guild not found")),
|
|
||||||
};
|
|
||||||
|
|
||||||
match guild
|
|
||||||
.voice_states
|
|
||||||
.get(&user.id)
|
|
||||||
.and_then(|voice_state| voice_state.channel_id)
|
|
||||||
{
|
|
||||||
Some(channel) => Ok(channel),
|
|
||||||
None => return Err(format!("User is not in a voice channel")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn create_response(
|
pub async fn create_response(
|
||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
command: &CommandInteraction,
|
command: &CommandInteraction,
|
||||||
content: String,
|
content: String,
|
||||||
) -> Result<(), SerenityError> {
|
) {
|
||||||
let data = CreateInteractionResponseMessage::new().content(content);
|
let data = CreateInteractionResponseMessage::new().content(content.to_owned());
|
||||||
let builder = CreateInteractionResponse::Message(data);
|
let builder = CreateInteractionResponse::Message(data);
|
||||||
command.create_response(&ctx.http, builder).await?;
|
match command.create_response(&ctx.http, builder).await {
|
||||||
Ok(())
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to create response for {content}\n{err}");
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn edit_response(
|
pub async fn edit_response(
|
||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
command: &CommandInteraction,
|
command: &CommandInteraction,
|
||||||
content: String,
|
content: String,
|
||||||
) -> Result<serenity::model::channel::Message, SerenityError> {
|
) {
|
||||||
let builder = EditInteractionResponse::new().content(content);
|
let builder = EditInteractionResponse::new().content(content.to_owned());
|
||||||
command.edit_response(&ctx.http, builder).await
|
match command.edit_response(&ctx.http, builder).await {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to create response for {content}\n{err}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,8 +96,25 @@ fn is_valid_url(url: &str) -> (bool, bool) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_songbird(ctx: &Context) -> Arc<Songbird> {
|
/**
|
||||||
songbird::get(ctx)
|
* Finds a voice channel that the user is currently in.
|
||||||
.await
|
*/
|
||||||
.expect("Songbird Voice client placed in at initialization")
|
fn find_voice_channel(
|
||||||
|
cache: &Arc<Cache>,
|
||||||
|
guild_id: &GuildId,
|
||||||
|
user: &User,
|
||||||
|
) -> SirenResult<ChannelId> {
|
||||||
|
let guild = match guild_id.to_guild_cached(cache) {
|
||||||
|
Some(g) => g,
|
||||||
|
None => return Err(SirenError::new(404, "Guild not found".to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
match guild
|
||||||
|
.voice_states
|
||||||
|
.get(&user.id)
|
||||||
|
.and_then(|voice_state| voice_state.channel_id)
|
||||||
|
{
|
||||||
|
Some(channel) => Ok(channel),
|
||||||
|
None => return Err(SirenError::new(401, "User is not in a voice channel".to_string())),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,33 @@
|
|||||||
use log::{debug, error};
|
|
||||||
|
|
||||||
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
||||||
|
|
||||||
use super::{get_songbird, create_response, edit_response};
|
use super::{get_songbird, create_response, edit_response};
|
||||||
|
|
||||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||||
// Create the initial response
|
// Create the initial response
|
||||||
if let Err(why) = create_response(&ctx, &command, "Processing command...".to_string()).await {
|
create_response(&ctx, &command, format!(".....")).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let guild_id = match command.guild_id {
|
// Get the songbird manager
|
||||||
Some(g) => g,
|
let manager = get_songbird(ctx).await;
|
||||||
|
|
||||||
|
// Extract the guild ID
|
||||||
|
let guild_id = match &command.guild_id {
|
||||||
|
Some(guild_id) => guild_id,
|
||||||
None => {
|
None => {
|
||||||
if let Err(why) =
|
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
|
||||||
edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await
|
|
||||||
{
|
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let manager = get_songbird(ctx).await;
|
|
||||||
if let Some(handler_lock) = manager.get(guild_id) {
|
// Pause the track
|
||||||
|
if let Some(handler_lock) = manager.get(guild_id.to_owned()) {
|
||||||
let handler = handler_lock.lock().await;
|
let handler = handler_lock.lock().await;
|
||||||
if let Err(err) = handler.queue().pause() {
|
match handler.queue().pause() {
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to pause: {}", err)).await {
|
Ok(_) => {
|
||||||
error!("Failed to edit response message: {}", why);
|
log::debug!("Paused the track");
|
||||||
|
edit_response(&ctx, &command, format!("Pausing the track")).await;
|
||||||
}
|
}
|
||||||
} else {
|
Err(err) => {
|
||||||
debug!("Paused the track");
|
edit_response(&ctx, &command, format!("Failed to pause: {}", err)).await;
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Pausing the track")).await {
|
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,63 +5,45 @@ use serenity::model::prelude::GuildId;
|
|||||||
use serenity::{prelude::*, async_trait};
|
use serenity::{prelude::*, async_trait};
|
||||||
use songbird::input::{AuxMetadata, Input, YoutubeDl};
|
use songbird::input::{AuxMetadata, Input, YoutubeDl};
|
||||||
use songbird::tracks::TrackHandle;
|
use songbird::tracks::TrackHandle;
|
||||||
use songbird::{Call, EventHandler, Songbird};
|
use songbird::{Call, Event, EventHandler, Songbird, TrackEvent};
|
||||||
|
|
||||||
use crate::bot::guilds::GuildCache;
|
use crate::bot::guilds::GuildCache;
|
||||||
use crate::bot::ytdlp::{PlaylistItem, YtDlp};
|
use crate::bot::ytdlp::{PlaylistItem, YtDlp};
|
||||||
use crate::bot::commands::audio::{create_response, edit_response, leave_voice_channel};
|
|
||||||
use crate::error::{SirenResult, Error as SirenError};
|
use crate::error::{SirenResult, Error as SirenError};
|
||||||
use crate::HttpKey;
|
use crate::HttpKey;
|
||||||
|
|
||||||
use super::{get_songbird, is_valid_url, join_by_user};
|
use super::{create_response, edit_response, get_songbird, is_valid_url, join_voice_channel, leave_voice_channel};
|
||||||
|
|
||||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||||
// Get the track url
|
// Process the command options
|
||||||
let track_url = match command.data.options.get(0) {
|
let track_url = match command.data.options.first() {
|
||||||
Some(o) => match &o.value.as_str() {
|
Some(o) => &o.value.as_str().unwrap(),
|
||||||
Some(s) => s.to_owned(),
|
|
||||||
None => {
|
None => {
|
||||||
log::warn!("Missing track option");
|
log::warn!("{} attempted to play a track without a track option", command.user.id.get());
|
||||||
if let Err(why) =
|
create_response(&ctx, &command, format!("Track option is missing")).await;
|
||||||
create_response(&ctx, &command, format!("Track option is missing")).await
|
|
||||||
{
|
|
||||||
log::error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
log::warn!("Missing track option");
|
|
||||||
if let Err(why) = create_response(&ctx, &command, format!("Track option is missing")).await {
|
|
||||||
log::error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the initial response
|
// Create the initial response
|
||||||
if let Err(why) = create_response(&ctx, &command, format!("Processing command...")).await {
|
create_response(&ctx, &command, format!(".....")).await;
|
||||||
log::error!("Failed to create response message: {}", why);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Get the songbird manager
|
||||||
let manager = get_songbird(ctx).await;
|
let manager = get_songbird(ctx).await;
|
||||||
|
|
||||||
// Extract the guild ID
|
// Extract the guild ID
|
||||||
let guild_id = match &command.guild_id {
|
let guild_id = match &command.guild_id {
|
||||||
Some(guild_id) => guild_id,
|
Some(guild_id) => guild_id,
|
||||||
None => {
|
None => {
|
||||||
if let Err(why) =
|
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
|
||||||
edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await
|
|
||||||
{
|
|
||||||
log::error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Join the user's voice channel
|
// Join the user's voice channel
|
||||||
match join_by_user(&ctx.cache, &manager, guild_id, &command.user).await {
|
match join_voice_channel(&ctx.cache, &manager, guild_id, &command.user).await {
|
||||||
Ok(_) => {
|
Ok(channel_id) => {
|
||||||
log::debug!("Play command executed with track: {:?}", track_url);
|
log::debug!("<{guild_id}> Play command executed on {channel_id} with track: {track_url:?}");
|
||||||
// Handle the track url
|
// Handle the track url
|
||||||
match play_track(ctx, manager, guild_id.to_owned(), track_url).await {
|
match play_track(ctx, manager, guild_id.to_owned(), track_url).await {
|
||||||
Ok(count) => {
|
Ok(count) => {
|
||||||
@@ -71,25 +53,17 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
|||||||
} else if count == 1 {
|
} else if count == 1 {
|
||||||
message = "Playing 1 track".to_string();
|
message = "Playing 1 track".to_string();
|
||||||
}
|
}
|
||||||
if let Err(why) = edit_response(&ctx, &command, message).await {
|
edit_response(&ctx, &command, message).await;
|
||||||
log::error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::warn!("Failed to play track: {}", err);
|
log::warn!("Failed to play track: {}", err);
|
||||||
if let Err(why) =
|
edit_response(&ctx, &command, format!("Failed to play track: {}", err)).await;
|
||||||
edit_response(&ctx, &command, format!("Failed to play track: {}", err)).await
|
|
||||||
{
|
|
||||||
log::error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::warn!("{}", err);
|
log::warn!("{}", err);
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("{}", err)).await {
|
edit_response(&ctx, &command, format!("{}", err)).await;
|
||||||
log::error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,17 +114,17 @@ pub async fn play_track(
|
|||||||
handler_lock.clone(),
|
handler_lock.clone(),
|
||||||
&item.url,
|
&item.url,
|
||||||
is_queue_empty,
|
is_queue_empty,
|
||||||
Some(guild.volume as f32 / 100.0),
|
guild.volume as f32 / 100.0,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(added_song) => {
|
Ok(metadata) => {
|
||||||
let track_title = added_song.title.unwrap();
|
let track_title = metadata.title.unwrap();
|
||||||
log::debug!("Added track: {}", track_title);
|
log::debug!("Added track: {}", track_title);
|
||||||
let mut handler = handler_lock.lock().await;
|
let mut handler = handler_lock.lock().await;
|
||||||
handler.remove_all_global_events();
|
// handler.remove_all_global_events();
|
||||||
handler.add_global_event(
|
handler.add_global_event(
|
||||||
songbird::Event::Track(songbird::TrackEvent::End),
|
Event::Track(TrackEvent::End),
|
||||||
TrackEndNotifier {
|
TrackEndNotifier {
|
||||||
guild_id,
|
guild_id,
|
||||||
call: manager.clone(),
|
call: manager.clone(),
|
||||||
@@ -160,7 +134,7 @@ pub async fn play_track(
|
|||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::warn!("Failed to add song: {}", err);
|
log::warn!("Failed to add song: {}", err);
|
||||||
if let Err(why) = leave_voice_channel(manager, &Some(guild_id)).await {
|
if let Err(why) = leave_voice_channel(&manager, &guild_id).await {
|
||||||
log::error!("Failed to leave voice channel: {}", why);
|
log::error!("Failed to leave voice channel: {}", why);
|
||||||
}
|
}
|
||||||
return Err(SirenError::new(422, err.to_string()));
|
return Err(SirenError::new(422, err.to_string()));
|
||||||
@@ -176,9 +150,8 @@ async fn add_song(
|
|||||||
call: Arc<Mutex<Call>>,
|
call: Arc<Mutex<Call>>,
|
||||||
url: &str,
|
url: &str,
|
||||||
lazy: bool,
|
lazy: bool,
|
||||||
volume: Option<f32>,
|
volume: f32,
|
||||||
) -> SirenResult<AuxMetadata> {
|
) -> SirenResult<AuxMetadata> {
|
||||||
// let source = Restartable::ytdl(url.to_owned(), lazy).await?;
|
|
||||||
let http_client = {
|
let http_client = {
|
||||||
let data = ctx.data.read().await;
|
let data = ctx.data.read().await;
|
||||||
data.get::<HttpKey>()
|
data.get::<HttpKey>()
|
||||||
@@ -187,17 +160,16 @@ async fn add_song(
|
|||||||
};
|
};
|
||||||
let source = YoutubeDl::new(http_client, url.to_owned());
|
let source = YoutubeDl::new(http_client, url.to_owned());
|
||||||
let mut handler = call.lock().await;
|
let mut handler = call.lock().await;
|
||||||
let mut track: Input = source.into();
|
let mut input: Input = source.into();
|
||||||
let metadata = track.aux_metadata().await.unwrap();
|
let metadata = input.aux_metadata().await.unwrap();
|
||||||
let track_handle: TrackHandle;
|
let track_handle: TrackHandle;
|
||||||
if lazy {
|
if lazy {
|
||||||
track_handle = handler.play_input(track);
|
track_handle = handler.play_input(input);
|
||||||
} else {
|
} else {
|
||||||
track_handle = handler.enqueue_input(track).await;
|
track_handle = handler.enqueue_input(input).await;
|
||||||
}
|
}
|
||||||
if let Some(volume) = volume {
|
// Set the volume
|
||||||
let _ = track_handle.set_volume(volume);
|
let _ = track_handle.set_volume(volume);
|
||||||
}
|
|
||||||
Ok(metadata)
|
Ok(metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,8 +208,8 @@ pub fn register() -> CreateCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct TrackEndNotifier {
|
struct TrackEndNotifier {
|
||||||
pub call: std::sync::Arc<songbird::Songbird>,
|
pub call: Arc<Songbird>,
|
||||||
pub guild_id: serenity::model::id::GuildId,
|
pub guild_id: GuildId,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
|
|||||||
@@ -1,38 +1,33 @@
|
|||||||
use log::{debug, error};
|
|
||||||
|
|
||||||
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
||||||
|
|
||||||
use super::{get_songbird, create_response, edit_response};
|
use super::{get_songbird, create_response, edit_response};
|
||||||
|
|
||||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||||
// Create the initial response
|
// Create the initial response
|
||||||
if let Err(why) = create_response(&ctx, &command, "Processing command...".to_string()).await {
|
create_response(&ctx, &command, format!(".....")).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let guild_id = match command.guild_id {
|
// Get the songbird manager
|
||||||
Some(g) => g,
|
let manager = get_songbird(ctx).await;
|
||||||
|
|
||||||
|
// Extract the guild ID
|
||||||
|
let guild_id = match &command.guild_id {
|
||||||
|
Some(guild_id) => guild_id,
|
||||||
None => {
|
None => {
|
||||||
if let Err(why) =
|
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
|
||||||
edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await
|
|
||||||
{
|
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let manager = get_songbird(ctx).await;
|
|
||||||
if let Some(handler_lock) = manager.get(guild_id) {
|
// Resume the track
|
||||||
|
if let Some(handler_lock) = manager.get(guild_id.to_owned()) {
|
||||||
let handler = handler_lock.lock().await;
|
let handler = handler_lock.lock().await;
|
||||||
if let Err(err) = handler.queue().resume() {
|
match handler.queue().resume() {
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to resume: {}", err)).await {
|
Ok(_) => {
|
||||||
error!("Failed to edit response message: {}", why);
|
log::debug!("Resumed the track");
|
||||||
}
|
edit_response(&ctx, &command, format!("Resuming the track")).await;
|
||||||
} else {
|
},
|
||||||
debug!("Resumed the track");
|
Err(err) => {
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Resuming the track")).await {
|
edit_response(&ctx, &command, format!("Failed to resume: {}", err)).await;
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,33 @@
|
|||||||
use log::{debug, error};
|
|
||||||
|
|
||||||
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
||||||
|
|
||||||
use super::{get_songbird, create_response, edit_response};
|
use super::{get_songbird, create_response, edit_response};
|
||||||
|
|
||||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||||
// Create the initial response
|
// Create the initial response
|
||||||
if let Err(why) = create_response(&ctx, &command, "Processing command...".to_string()).await {
|
create_response(&ctx, &command, format!(".....")).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let guild_id = match command.guild_id {
|
// Get the songbird manager
|
||||||
Some(g) => g,
|
let manager = get_songbird(ctx).await;
|
||||||
|
|
||||||
|
// Extract the guild ID
|
||||||
|
let guild_id = match &command.guild_id {
|
||||||
|
Some(guild_id) => guild_id,
|
||||||
None => {
|
None => {
|
||||||
if let Err(why) =
|
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
|
||||||
edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await
|
|
||||||
{
|
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let manager = get_songbird(ctx).await;
|
|
||||||
if let Some(handler_lock) = manager.get(guild_id) {
|
// Skip the track
|
||||||
|
if let Some(handler_lock) = manager.get(guild_id.to_owned()) {
|
||||||
let handler = handler_lock.lock().await;
|
let handler = handler_lock.lock().await;
|
||||||
if let Err(err) = handler.queue().skip() {
|
match handler.queue().skip() {
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Failed to skip: {}", err)).await {
|
Ok(_) => {
|
||||||
error!("Failed to edit response message: {}", why);
|
log::debug!("Skipped the track");
|
||||||
}
|
edit_response(&ctx, &command, format!("Skipping the track")).await;
|
||||||
} else {
|
},
|
||||||
debug!("Skipped the track");
|
Err(err) => {
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Skipping the track")).await {
|
edit_response(&ctx, &command, format!("Failed to skip: {}", err)).await;
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,29 @@
|
|||||||
use log::{debug, error};
|
|
||||||
|
|
||||||
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
use serenity::{all::{CommandInteraction, CreateCommand}, prelude::*};
|
||||||
|
|
||||||
use super::{get_songbird, create_response, edit_response};
|
use super::{get_songbird, create_response, edit_response};
|
||||||
|
|
||||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||||
// Create the initial response
|
// Create the initial response
|
||||||
if let Err(why) = create_response(&ctx, &command, "Processing command...".to_string()).await {
|
create_response(&ctx, &command, format!(".....")).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Get the songbird manager
|
||||||
|
let manager = get_songbird(ctx).await;
|
||||||
|
|
||||||
|
// Extract the guild ID
|
||||||
let guild_id = match command.guild_id {
|
let guild_id = match command.guild_id {
|
||||||
Some(g) => g,
|
Some(g) => g,
|
||||||
None => {
|
None => {
|
||||||
if let Err(why) =
|
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
|
||||||
edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await
|
|
||||||
{
|
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let manager = get_songbird(ctx).await;
|
|
||||||
|
// Stop the track and clear the queue
|
||||||
if let Some(handler_lock) = manager.get(guild_id) {
|
if let Some(handler_lock) = manager.get(guild_id) {
|
||||||
let handler = handler_lock.lock().await;
|
let handler = handler_lock.lock().await;
|
||||||
handler.queue().stop();
|
handler.queue().stop();
|
||||||
debug!("Stopped the track");
|
log::debug!("Stopped the track");
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Stopping the tracks")).await {
|
edit_response(&ctx, &command, format!("Stopping the tracks")).await;
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use log::{error, warn};
|
|
||||||
|
|
||||||
use serenity::{all::{CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption}, model::prelude::GuildId, prelude::*};
|
use serenity::{all::{CommandInteraction, CommandOptionType, CreateCommand, CreateCommandOption}, model::prelude::GuildId, prelude::*};
|
||||||
use songbird::Songbird;
|
use songbird::Songbird;
|
||||||
|
|
||||||
@@ -10,66 +8,53 @@ use crate::bot::guilds::GuildCache;
|
|||||||
use super::{get_songbird, create_response, edit_response};
|
use super::{get_songbird, create_response, edit_response};
|
||||||
|
|
||||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||||
// Get the volume
|
// Process the command options
|
||||||
let volume = match command.data.options.get(0) {
|
let volume = match command.data.options.first() {
|
||||||
Some(o) => match o.value.as_i64() {
|
Some(o) => o.value.as_i64().unwrap() as i32,
|
||||||
Some(p) => p as i32,
|
|
||||||
None => {
|
None => {
|
||||||
warn!("Unable to get volume option as a string");
|
log::warn!("Unable to get volume option as a string");
|
||||||
if let Err(why) =
|
create_response(&ctx, &command, format!("Volume option is missing")).await;
|
||||||
create_response(&ctx, &command, format!("Volume option is missing")).await
|
|
||||||
{
|
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
warn!("Missing volume option");
|
|
||||||
if let Err(why) = create_response(&ctx, &command, format!("Volume option is missing")).await {
|
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the initial response
|
// Create the initial response
|
||||||
if let Err(why) = create_response(&ctx, &command, "Processing command...".to_string()).await {
|
create_response(&ctx, &command, ".....".to_string()).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let guild_id = match command.guild_id {
|
// Get the songbird manager
|
||||||
Some(g) => g,
|
let manager = get_songbird(ctx).await;
|
||||||
|
|
||||||
|
// Extract the guild ID
|
||||||
|
let guild_id = match &command.guild_id {
|
||||||
|
Some(guild_id) => guild_id,
|
||||||
None => {
|
None => {
|
||||||
if let Err(why) =
|
edit_response(&ctx, &command, "Unable to find the current server ID".to_string()).await;
|
||||||
edit_response(&ctx, &command, "Unable to join voice channel".to_string()).await
|
|
||||||
{
|
|
||||||
error!("Failed to edit response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let manager = get_songbird(ctx).await;
|
|
||||||
set_volume(manager, guild_id, volume).await;
|
// Set the volume
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Setting the volume to {}", volume)).await
|
set_volume(&manager, guild_id, volume).await;
|
||||||
{
|
edit_response(&ctx, &command, format!("Setting the volume to {}", volume)).await;
|
||||||
error!("Failed to set the volume: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_volume(manager: Arc<Songbird>, guild_id: GuildId, volume: i32) {
|
pub async fn set_volume(manager: &Arc<Songbird>, guild_id: &GuildId, volume: i32) {
|
||||||
// Format volume to f32 bound between 0.0 and 1.0
|
// Format volume to f32 bound between 0.0 and 1.0
|
||||||
let volume = std::cmp::min(100, std::cmp::max(0, volume));
|
let volume = std::cmp::min(100, std::cmp::max(0, volume));
|
||||||
let bound_volume = volume as f32 / 100.0;
|
let bound_volume = volume as f32 / 100.0;
|
||||||
|
|
||||||
|
// Update the guild cache
|
||||||
let mut guild_cache = GuildCache::get_by_id(guild_id.get() as i64).await.unwrap().unwrap();
|
let mut guild_cache = GuildCache::get_by_id(guild_id.get() as i64).await.unwrap().unwrap();
|
||||||
guild_cache.volume = volume;
|
guild_cache.volume = volume;
|
||||||
guild_cache.update().await.unwrap();
|
guild_cache.update().await.unwrap();
|
||||||
|
|
||||||
if let Some(handler_lock) = manager.get(guild_id) {
|
// Update the volume of the songbird handler
|
||||||
|
if let Some(handler_lock) = manager.get(guild_id.to_owned()) {
|
||||||
let handler = handler_lock.lock().await;
|
let handler = handler_lock.lock().await;
|
||||||
for (_, track_handle) in handler.queue().current_queue().iter().enumerate() {
|
for (_, track_handle) in handler.queue().current_queue().iter().enumerate() {
|
||||||
let _ = track_handle.set_volume(bound_volume);
|
if let Err(err) = track_handle.set_volume(bound_volume) {
|
||||||
|
log::error!("Unable to set volume: {err}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use log::{error, warn};
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serenity::all::{CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption};
|
use serenity::all::{CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption};
|
||||||
|
|
||||||
@@ -7,28 +6,21 @@ use crate::bot::commands::audio::edit_response;
|
|||||||
use super::audio::create_response;
|
use super::audio::create_response;
|
||||||
|
|
||||||
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
||||||
if let Err(why) = create_response(&ctx, &command, format!("Processing command...")).await {
|
create_response(&ctx, &command, format!("Processing command...")).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let dice_string = match command.data.options.get(0) {
|
let dice_string = match command.data.options.get(0) {
|
||||||
Some(o) => {
|
Some(o) => {
|
||||||
match o.value.as_str() {
|
match o.value.as_str() {
|
||||||
Some(s) => s.split_whitespace().collect::<String>(),
|
Some(s) => s.split_whitespace().collect::<String>(),
|
||||||
None => {
|
None => {
|
||||||
warn!("Missing dice option");
|
log::warn!("Missing dice option");
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Dice option is missing")).await {
|
edit_response(&ctx, &command, format!("Dice option is missing")).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
warn!("Missing dice option");
|
log::warn!("Missing dice option");
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Dice option is missing")).await {
|
edit_response(&ctx, &command, format!("Dice option is missing")).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -55,15 +47,10 @@ pub async fn run(ctx: &Context, command: &CommandInteraction) {
|
|||||||
},
|
},
|
||||||
total + (modifier as u32)
|
total + (modifier as u32)
|
||||||
);
|
);
|
||||||
if let Err(why) = edit_response(&ctx, &command, response).await {
|
edit_response(&ctx, &command, response).await;
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
if let Err(why) = edit_response(&ctx, &command, format!("Invalid dice string: {}", why)).await
|
edit_response(&ctx, &command, format!("Invalid dice string: {}", why)).await;
|
||||||
{
|
|
||||||
error!("Failed to create response message: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,10 +66,7 @@ impl EventHandler for Handler {
|
|||||||
"ping" => commands::ping::run(&command.data.options),
|
"ping" => commands::ping::run(&command.data.options),
|
||||||
_ => "Unknown command".to_string(),
|
_ => "Unknown command".to_string(),
|
||||||
};
|
};
|
||||||
|
create_response(&ctx, &command, content).await;
|
||||||
if let Err(why) = create_response(&ctx, &command, content).await {
|
|
||||||
warn!("Cannot respond to slash command: {}", why);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user