Skip to content

Commit

Permalink
Merge pull request #49 from jontze/release/v0.2.0-rc.1
Browse files Browse the repository at this point in the history
Release v0.2.0-rc.1
  • Loading branch information
jontze committed Aug 8, 2022
2 parents bad2540 + 8889f4a commit 6031740
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cadency"
version = "0.1.1"
version = "0.2.0-rc.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down Expand Up @@ -29,4 +29,5 @@ version = "1.0.130"
features = ["derive"]

[features]
default = ["audio"]
audio = ["songbird", "songbird/builtin-queue", "serenity/voice", "serenity/cache"]
32 changes: 30 additions & 2 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,40 @@ pub mod fib;
pub mod inspire;
#[cfg(feature = "audio")]
pub mod now;
#[cfg(feature = "audio")]
pub mod pause;
pub mod ping;
#[cfg(feature = "audio")]
pub mod play;
#[cfg(feature = "audio")]
pub mod resume;
#[cfg(feature = "audio")]
pub mod skip;
pub mod slap;
#[cfg(feature = "audio")]
pub mod stop;
#[cfg(feature = "audio")]
pub mod tracks;
pub mod urban;

pub use fib::Fib;
pub use inspire::Inspire;
#[cfg(feature = "audio")]
pub use now::Now;
#[cfg(feature = "audio")]
pub use pause::Pause;
pub use ping::Ping;
#[cfg(feature = "audio")]
pub use play::Play;
#[cfg(feature = "audio")]
pub use resume::Resume;
#[cfg(feature = "audio")]
pub use skip::Skip;
pub use slap::Slap;
#[cfg(feature = "audio")]
pub use stop::Stop;
#[cfg(feature = "audio")]
pub use tracks::Tracks;
pub use urban::Urban;

#[async_trait]
Expand All @@ -41,7 +61,7 @@ pub trait CadencyCommand {

/// Submit global slash commands to the discord api.
/// As global commands are cached for 1 hour, the activation ca take some time.
/// For local testing it is recommended to create commandswith a guild scope.
/// For local testing it is recommended to create commands with a guild scope.
pub async fn setup_commands(ctx: &Context) -> Result<(), serenity::Error> {
tokio::try_join!(
Ping::register(ctx),
Expand All @@ -51,7 +71,15 @@ pub async fn setup_commands(ctx: &Context) -> Result<(), serenity::Error> {
Slap::register(ctx)
)?;
#[cfg(feature = "audio")]
tokio::try_join!(Play::register(ctx), Now::register(ctx))?;
tokio::try_join!(
Play::register(ctx),
Now::register(ctx),
Skip::register(ctx),
Pause::register(ctx),
Resume::register(ctx),
Stop::register(ctx),
Tracks::register(ctx)
)?;
Ok(())
}

Expand Down
74 changes: 74 additions & 0 deletions src/commands/pause.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![cfg(feature = "audio")]
use crate::commands::CadencyCommand;
use crate::error::CadencyError;
use crate::utils;
use serenity::{
async_trait,
client::Context,
model::application::{
command::Command, interaction::application_command::ApplicationCommandInteraction,
},
};

pub struct Pause;

#[async_trait]
impl CadencyCommand for Pause {
async fn register(ctx: &Context) -> Result<Command, serenity::Error> {
Ok(
Command::create_global_application_command(&ctx.http, |command| {
command.name("pause").description("Pause current song")
})
.await?,
)
}

async fn execute<'a>(
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
) -> Result<(), CadencyError> {
debug!("Execute pause command");
if let Some(guild_id) = command.guild_id {
utils::voice::create_deferred_response(ctx, command).await?;
let manager = utils::voice::get_songbird(ctx).await;
if let Some(call) = manager.get(guild_id) {
let handler = call.lock().await;
if handler.queue().is_empty() {
utils::voice::edit_deferred_response(ctx, command, ":x: **Nothing to pause**")
.await?;
} else {
match handler.queue().pause() {
Ok(_) => {
utils::voice::edit_deferred_response(
ctx,
command,
":pause_button: **Paused**",
)
.await?;
}
Err(err) => {
error!("Failed to pause: {err:?}");
utils::voice::edit_deferred_response(
ctx,
command,
":x: **Could not pause**",
)
.await?;
}
};
}
} else {
utils::voice::edit_deferred_response(ctx, command, ":x: **Nothing to pause**")
.await?;
}
} else {
utils::create_response(
ctx,
command,
":x: **This command can only be executed on a server**",
)
.await?;
}
Ok(())
}
}
15 changes: 10 additions & 5 deletions src/commands/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ impl CadencyCommand for Play {
debug!("Execute play command");
let url_option = utils::voice::parse_valid_url(&command.data.options);
if let Some(valid_url) = url_option {
utils::voice::create_deferred_response(ctx, command).await?;
if let Ok((manager, guild_id, _channel_id)) = utils::voice::join(ctx, command).await {
let call = manager.get(guild_id).unwrap();
match utils::voice::add_song(call.clone(), valid_url.to_string()).await {
Ok(added_song) => {
let mut handler = call.lock().await;
handler.remove_all_global_events();
handler.add_global_event(
Event::Periodic(std::time::Duration::from_secs(30), None),
Event::Periodic(std::time::Duration::from_secs(120), None),
InactiveHandler { guild_id, manager },
);
utils::create_response(
utils::voice::edit_deferred_response(
ctx,
command,
&format!(
Expand All @@ -68,7 +69,7 @@ impl CadencyCommand for Play {
}
Err(err) => {
error!("Failed to add song to queue: {}", err);
utils::create_response(
utils::voice::edit_deferred_response(
ctx,
command,
":x: **Could not add audio source to the queue!**",
Expand All @@ -77,8 +78,12 @@ impl CadencyCommand for Play {
}
}
} else {
utils::create_response(ctx, command, ":x: **Could not join your voice channel**")
.await?;
utils::voice::edit_deferred_response(
ctx,
command,
":x: **Could not join your voice channel**",
)
.await?;
}
} else {
utils::create_response(ctx, command, ":x: **This doesn't look lik a valid url**")
Expand Down
76 changes: 76 additions & 0 deletions src/commands/resume.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#![cfg(feature = "audio")]
use crate::commands::CadencyCommand;
use crate::error::CadencyError;
use crate::utils;
use serenity::{
async_trait,
client::Context,
model::application::{
command::Command, interaction::application_command::ApplicationCommandInteraction,
},
};

pub struct Resume;

#[async_trait]
impl CadencyCommand for Resume {
async fn register(ctx: &Context) -> Result<Command, serenity::Error> {
Ok(
Command::create_global_application_command(&ctx.http, |command| {
command
.name("resume")
.description("Resume current song if paused")
})
.await?,
)
}

async fn execute<'a>(
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
) -> Result<(), CadencyError> {
debug!("Execute skip command");
if let Some(guild_id) = command.guild_id {
utils::voice::create_deferred_response(ctx, command).await?;
let manager = utils::voice::get_songbird(ctx).await;
if let Some(call) = manager.get(guild_id) {
let handler = call.lock().await;
if handler.queue().is_empty() {
utils::voice::edit_deferred_response(ctx, command, ":x: **Nothing to resume**")
.await?;
} else {
match handler.queue().resume() {
Ok(_) => {
utils::voice::edit_deferred_response(
ctx,
command,
":play_pause: **Resumed**",
)
.await?;
}
Err(err) => {
error!("Failed to resume: {err:?}");
utils::voice::edit_deferred_response(
ctx,
command,
":x: **Could not resume**",
)
.await?;
}
};
}
} else {
utils::voice::edit_deferred_response(ctx, command, ":x: **Nothing to resume**")
.await?;
}
} else {
utils::create_response(
ctx,
command,
":x: **This command can only be executed on a server**",
)
.await?;
}
Ok(())
}
}
74 changes: 74 additions & 0 deletions src/commands/skip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![cfg(feature = "audio")]
use crate::commands::CadencyCommand;
use crate::error::CadencyError;
use crate::utils;
use serenity::{
async_trait,
client::Context,
model::application::{
command::Command, interaction::application_command::ApplicationCommandInteraction,
},
};

pub struct Skip;

#[async_trait]
impl CadencyCommand for Skip {
async fn register(ctx: &Context) -> Result<Command, serenity::Error> {
Ok(
Command::create_global_application_command(&ctx.http, |command| {
command.name("skip").description("Skip current song")
})
.await?,
)
}

async fn execute<'a>(
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
) -> Result<(), CadencyError> {
debug!("Execute skip command");
if let Some(guild_id) = command.guild_id {
utils::voice::create_deferred_response(ctx, command).await?;
let manager = utils::voice::get_songbird(ctx).await;
if let Some(call) = manager.get(guild_id) {
let handler = call.lock().await;
if handler.queue().is_empty() {
utils::voice::edit_deferred_response(ctx, command, ":x: **Nothing to skip**")
.await?;
} else {
match handler.queue().skip() {
Ok(_) => {
utils::voice::edit_deferred_response(
ctx,
command,
":fast_forward: **Skipped current song**",
)
.await?;
}
Err(err) => {
error!("Failed to skip: {err:?}");
utils::voice::edit_deferred_response(
ctx,
command,
":x: **Could not skip**",
)
.await?;
}
};
}
} else {
utils::voice::edit_deferred_response(ctx, command, ":x: **Nothing to skip**")
.await?;
}
} else {
utils::create_response(
ctx,
command,
":x: **This command can only be executed on a server**",
)
.await?;
}
Ok(())
}
}
Loading

0 comments on commit 6031740

Please sign in to comment.