Add move_song command

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/11/head
trivernis 3 years ago
parent a98f401fbe
commit bba12ff763
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -22,6 +22,7 @@ use current::CURRENT_COMMAND;
use join::JOIN_COMMAND;
use leave::LEAVE_COMMAND;
use lyrics::LYRICS_COMMAND;
use move_song::MOVE_SONG_COMMAND;
use pause::PAUSE_COMMAND;
use play::PLAY_COMMAND;
use play_next::PLAY_NEXT_COMMAND;
@ -43,6 +44,7 @@ mod current;
mod join;
mod leave;
mod lyrics;
mod move_song;
mod pause;
mod play;
mod play_next;
@ -66,7 +68,8 @@ mod skip;
pause,
save_playlist,
playlists,
lyrics
lyrics,
move_song
)]
pub struct Music;

@ -0,0 +1,38 @@
use crate::commands::music::{get_queue_for_guild, is_dj};
use serenity::client::Context;
use serenity::framework::standard::macros::command;
use serenity::framework::standard::{Args, CommandResult};
use serenity::model::channel::Message;
#[command]
#[description("Moves a song in the queue from one position to a new one")]
#[usage("<old-pos> <new-pos>")]
#[example("102 2")]
#[min_args(2)]
#[max_args(2)]
#[bucket("general")]
#[only_in(guilds)]
#[aliases("mvs", "movesong", "move-song")]
async fn move_song(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
log::debug!("Moving song for guild {}", guild.id);
let pos1 = args.single::<usize>()?;
let pos2 = args.single::<usize>()?;
if !is_dj(ctx, guild.id, &msg.author).await? {
msg.channel_id.say(ctx, "Requires DJ permissions").await?;
return Ok(());
}
let queue = get_queue_for_guild(ctx, &guild.id).await?;
let mut queue_lock = queue.lock().await;
queue_lock.move_position(pos1, pos2);
msg.channel_id
.say(
ctx,
format!("Moved Song `{}` to new position `{}`", pos1, pos2),
)
.await?;
Ok(())
}

@ -76,6 +76,18 @@ impl MusicQueue {
self.inner.clear();
}
/// Moves a song to a new position
pub fn move_position(&mut self, index: usize, new_index: usize) {
if let Some(song) = self.inner.remove(index) {
self.inner.insert(new_index, song);
}
}
/// Removes a song from the queue
pub fn remove(&mut self, index: usize) {
self.inner.remove(index);
}
/// Toggles pause
pub fn pause(&mut self) {
if let Some(current) = &self.current {

Loading…
Cancel
Save