Add shutdown and pause command

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/2/head
trivernis 3 years ago
parent e9c70bc754
commit 6c9df7e044
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,12 +1,14 @@
use serenity::framework::standard::macros::group;
use ping::PING_COMMAND;
use shutdown::SHUTDOWN_COMMAND;
use stats::STATS_COMMAND;
pub(crate) mod help;
mod ping;
mod shutdown;
mod stats;
#[group]
#[commands(ping, stats)]
#[commands(ping, stats, shutdown)]
pub struct Misc;

@ -0,0 +1,18 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::channel::Message;
use serenity::prelude::*;
use std::process;
#[command]
#[description("Shutdown")]
#[usage("shutdown")]
#[example("shutdown")]
#[owners_only]
async fn shutdown(ctx: &Context, msg: &Message) -> CommandResult {
log::info!("Shutting down...");
msg.channel_id
.say(ctx, ":night_with_stars: Good night ....")
.await?;
process::exit(0);
}

@ -12,6 +12,7 @@ use sysinfo::{ProcessExt, SystemExt};
#[usage("stats")]
#[example("stats")]
async fn stats(ctx: &Context, msg: &Message) -> CommandResult {
log::debug!("Reading system stats");
let mut system = sysinfo::System::new_all();
system.refresh_all();
let kernel_version = system.get_kernel_version().unwrap_or("n/a".to_string());
@ -35,6 +36,8 @@ async fn stats(ctx: &Context, msg: &Message) -> CommandResult {
bot_info.owner.id, guild_count
);
log::trace!("Discord info {}", discord_info);
let system_info = format!(
r#"
Kernel Version: {}
@ -51,6 +54,7 @@ async fn stats(ctx: &Context, msg: &Message) -> CommandResult {
uptime.num_hours() % 24,
uptime.num_minutes() % 60
);
log::trace!("System info {}", system_info);
msg.channel_id
.send_message(ctx, |m| {

@ -16,6 +16,7 @@ use clear::CLEAR_COMMAND;
use current::CURRENT_COMMAND;
use join::JOIN_COMMAND;
use leave::LEAVE_COMMAND;
use pause::PAUSE_COMMAND;
use play::PLAY_COMMAND;
use play_next::PLAY_NEXT_COMMAND;
use queue::QUEUE_COMMAND;
@ -35,6 +36,7 @@ mod clear;
mod current;
mod join;
mod leave;
mod pause;
mod play;
mod play_next;
mod queue;
@ -42,7 +44,9 @@ mod shuffle;
mod skip;
#[group]
#[commands(join, leave, play, queue, skip, shuffle, current, play_next, clear)]
#[commands(
join, leave, play, queue, skip, shuffle, current, play_next, clear, pause
)]
#[prefix("m")]
pub struct Music;

@ -0,0 +1,33 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::channel::Message;
use serenity::prelude::*;
use crate::commands::music::get_queue_for_guild;
#[command]
#[only_in(guilds)]
#[description("Pauses playback")]
#[usage("pause")]
#[allowed_roles("DJ")]
async fn pause(ctx: &Context, msg: &Message) -> CommandResult {
let guild = msg.guild(&ctx.cache).await.unwrap();
log::debug!("Pausing playback for guild {}", guild.id);
let queue = get_queue_for_guild(ctx, &guild.id).await?;
let mut queue_lock = queue.lock().await;
if let Some(_) = queue_lock.current() {
queue_lock.pause();
if queue_lock.paused() {
log::debug!("Paused");
msg.channel_id.say(ctx, "Paused playback").await?;
} else {
log::debug!("Resumed");
msg.channel_id.say(ctx, "Resumed playback").await?;
}
} else {
msg.channel_id.say(ctx, "Nothing to pause").await?;
}
Ok(())
}

@ -11,6 +11,7 @@ use aspotify::{Track, TrackSimplified};
pub struct MusicQueue {
inner: VecDeque<Song>,
current: Option<TrackHandle>,
paused: bool,
}
impl MusicQueue {
@ -18,6 +19,7 @@ impl MusicQueue {
Self {
inner: VecDeque::new(),
current: None,
paused: false,
}
}
@ -65,6 +67,25 @@ impl MusicQueue {
pub fn clear(&mut self) {
self.inner.clear();
}
/// Toggles pause
pub fn pause(&mut self) {
if let Some(current) = &self.current {
if self.paused {
let _ = current.play();
} else {
let _ = current.pause();
}
self.paused = !self.paused;
} else {
self.paused = false;
}
}
pub fn paused(&self) -> bool {
self.paused
}
}
#[derive(Clone, Debug)]

Loading…
Cancel
Save