Skip to content

Commit

Permalink
refactor: Update code to latest songbird and serenity version
Browse files Browse the repository at this point in the history
  • Loading branch information
jontze committed Nov 30, 2023
1 parent 4116aa9 commit f031e10
Show file tree
Hide file tree
Showing 24 changed files with 290 additions and 273 deletions.
2 changes: 1 addition & 1 deletion cadency_codegen/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub(crate) fn impl_command_baseline(derive_input: DeriveInput) -> TokenStream {
let deferred = command.deferred;
quote! {
use cadency_core::{CadencyCommandBaseline as __CadencyCommandBaseline, CadencyCommandOption as __CadencyCommandOption};
use serenity::model::application::command::CommandOptionType as __CommandOptionType;
use serenity::model::application::CommandOptionType as __CommandOptionType;
impl __CadencyCommandBaseline for #struct_name {
fn name(&self) -> String {
String::from(#command_name)
Expand Down
6 changes: 2 additions & 4 deletions cadency_commands/src/fib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use cadency_core::{
use serenity::{
async_trait,
client::Context,
model::application::interaction::application_command::{
ApplicationCommandInteraction, CommandDataOptionValue,
},
model::application::{CommandDataOptionValue, CommandInteraction},
};

#[derive(CommandBaseline, Default)]
Expand All @@ -34,7 +32,7 @@ impl CadencyCommand for Fib {
async fn execute<'a>(
&self,
_ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let number = utils::get_option_value_at_position(command.data.options.as_ref(), 0)
Expand Down
7 changes: 2 additions & 5 deletions cadency_commands/src/inspire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use cadency_core::{
response::{Response, ResponseBuilder},
CadencyCommand, CadencyError,
};
use serenity::{
async_trait, client::Context,
model::application::interaction::application_command::ApplicationCommandInteraction,
};
use serenity::{async_trait, client::Context, model::application::CommandInteraction};

#[derive(CommandBaseline, Default)]
#[description = "Say something really inspiring!"]
Expand All @@ -26,7 +23,7 @@ impl CadencyCommand for Inspire {
async fn execute<'a>(
&self,
_ctx: &Context,
_command: &'a mut ApplicationCommandInteraction,
_command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let inspire_url = Self::request_inspire_image_url().await.map_err(|err| {
Expand Down
12 changes: 5 additions & 7 deletions cadency_commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ mod test {
fn impl_commandbaseline_trait_with_macro() {
#[derive(cadency_codegen::CommandBaseline)]
struct Test {}
assert!(true)
}

#[test]
Expand Down Expand Up @@ -97,9 +96,8 @@ mod test {
#[description = "123"]
struct Test {}
let test = Test {};
assert_eq!(
test.deferred(),
false,
assert!(
!test.deferred(),
"Test command should not be deferred by default"
)
}
Expand Down Expand Up @@ -128,7 +126,7 @@ mod test {

#[test]
fn return_derived_option() {
use serenity::model::application::command::CommandOptionType;
use serenity::model::application::CommandOptionType;
#[derive(cadency_codegen::CommandBaseline)]
#[argument(
name = "say",
Expand All @@ -144,7 +142,7 @@ mod test {
assert_eq!(argument.name, "say");
assert_eq!(argument.description, "Word to say");
assert_eq!(argument.kind, CommandOptionType::String);
assert_eq!(argument.required, false);
assert!(!argument.required);
}

#[test]
Expand All @@ -166,7 +164,7 @@ mod test {

#[test]
fn return_multiple_options() {
use serenity::model::application::command::CommandOptionType;
use serenity::model::application::CommandOptionType;

#[derive(cadency_codegen::CommandBaseline)]
#[argument(name = "say", description = "Word to say", kind = "String")]
Expand Down
27 changes: 17 additions & 10 deletions cadency_commands/src/now.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use cadency_core::{
response::{Response, ResponseBuilder},
utils, CadencyCommand, CadencyError,
};
use serenity::{
async_trait, client::Context,
model::application::interaction::application_command::ApplicationCommandInteraction,
utils::{self, voice::TrackMetaKey},
CadencyCommand, CadencyError,
};
use serenity::{async_trait, client::Context, model::application::CommandInteraction};

#[derive(CommandBaseline, Default)]
#[description = "Shows current song"]
Expand All @@ -16,7 +14,7 @@ impl CadencyCommand for Now {
async fn execute<'a>(
&self,
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let guild_id = command.guild_id.ok_or(CadencyError::Command {
Expand All @@ -30,11 +28,20 @@ impl CadencyCommand for Now {
let track = handler.queue().current().ok_or(CadencyError::Command {
message: ":x: **No song is playing**".to_string(),
})?;
Ok(response_builder
.message(Some(track.metadata().title.as_ref().map_or(

// Create message from track metadata. This is scoped to drop the read lock on the
// trackmeta as soon as possible.
let message = {
let track_map = track.typemap().read().await;
let metadata = track_map
.get::<TrackMetaKey>()
.expect("Metadata to be present in track map");
metadata.title.as_ref().map_or(
String::from(":x: **Could not add audio source to the queue!**"),
|title| format!(":newspaper: `{title}`"),
)))
.build()?)
)
};

Ok(response_builder.message(Some(message)).build()?)
}
}
7 changes: 2 additions & 5 deletions cadency_commands/src/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use cadency_core::{
response::{Response, ResponseBuilder},
utils, CadencyCommand, CadencyError,
};
use serenity::{
async_trait, client::Context,
model::application::interaction::application_command::ApplicationCommandInteraction,
};
use serenity::{async_trait, client::Context, model::application::CommandInteraction};

#[derive(CommandBaseline, Default)]
#[description = "Pause the current song"]
Expand All @@ -17,7 +14,7 @@ impl CadencyCommand for Pause {
async fn execute<'a>(
&self,
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let guild_id = command.guild_id.ok_or(CadencyError::Command {
Expand Down
7 changes: 2 additions & 5 deletions cadency_commands/src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use cadency_core::{
response::{Response, ResponseBuilder},
CadencyCommand, CadencyError,
};
use serenity::{
async_trait, client::Context,
model::application::interaction::application_command::ApplicationCommandInteraction,
};
use serenity::{async_trait, client::Context, model::application::CommandInteraction};

#[derive(CommandBaseline, Default)]
#[description = "Play Ping-Pong"]
Expand All @@ -16,7 +13,7 @@ impl CadencyCommand for Ping {
async fn execute<'a>(
&self,
_ctx: &Context,
_command: &'a mut ApplicationCommandInteraction,
_command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
Ok(response_builder
Expand Down
51 changes: 17 additions & 34 deletions cadency_commands/src/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use reqwest::Url;
use serenity::{
async_trait,
client::Context,
model::application::interaction::application_command::{
ApplicationCommandInteraction, CommandDataOptionValue,
},
model::application::{CommandDataOptionValue, CommandInteraction},
};
use songbird::events::Event;

Expand Down Expand Up @@ -42,7 +40,7 @@ impl CadencyCommand for Play {
async fn execute<'a>(
&self,
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let (search_payload, is_url, is_playlist) =
Expand All @@ -67,10 +65,7 @@ impl CadencyCommand for Play {
message: ":x: **No search string provided**".to_string(),
})?;
let (manager, call, guild_id) = utils::voice::join(ctx, command).await?;
let mut is_queue_empty = {
let call_handler = call.lock().await;
call_handler.queue().is_empty()
};

let response_builder = if is_playlist {
let playlist_items =
cadency_yt_playlist::fetch_playlist_songs(search_payload.clone()).unwrap();
Expand All @@ -87,19 +82,11 @@ impl CadencyCommand for Play {
if amount_added_playlist_songs <= self.playlist_song_limit
&& song.duration <= self.song_length_limit
{
match utils::voice::add_song(
call.clone(),
song.url,
true,
!is_queue_empty, // Don't add first song lazy to the queue
)
.await
{
Ok(added_song) => {
match utils::voice::add_song(ctx, call.clone(), song.url, true).await {
Ok((added_song_meta, _)) => {
amount_added_playlist_songs += 1;
amount_total_added_playlist_duration += song.duration;
is_queue_empty = false;
debug!("➕ Added song '{:?}' from playlist", added_song.title);
debug!("➕ Added song '{:?}' from playlist", added_song_meta.title);
}
Err(err) => {
error!("❌ Failed to add song: {err}");
Expand All @@ -119,19 +106,15 @@ impl CadencyCommand for Play {
":white_check_mark: **Added ___{amount_added_playlist_songs}___ songs to the queue with a duration of ___{amount_total_added_playlist_duration:.2}___ mins** \n**Playing** :notes: `{search_payload}`",
)))
} else {
let added_song = utils::voice::add_song(
call.clone(),
search_payload.clone(),
is_url,
!is_queue_empty, // Don't add first song lazy to the queue
)
.await
.map_err(|err| {
error!("❌ Failed to add song to queue: {}", err);
CadencyError::Command {
message: ":x: **Couldn't add audio source to the queue!**".to_string(),
}
})?;
let (added_song_meta, _) =
utils::voice::add_song(ctx, call.clone(), search_payload.clone(), is_url)
.await
.map_err(|err| {
error!("❌ Failed to add song to queue: {}", err);
CadencyError::Command {
message: ":x: **Couldn't add audio source to the queue!**".to_string(),
}
})?;
let mut handler = call.lock().await;
handler.remove_all_global_events();
handler.add_global_event(
Expand All @@ -141,15 +124,15 @@ impl CadencyCommand for Play {
let song_url = if is_url {
search_payload
} else {
added_song
added_song_meta
.source_url
.as_ref()
.map_or("unknown url", |url| url)
};
response_builder.message(Some(format!(
":white_check_mark: **Added song to the queue and started playing:** \n:notes: `{}` \n:link: `{}`",
song_url,
added_song
added_song_meta
.title
.as_ref()
.map_or(":x: **Unknown title**", |title| title)
Expand Down
7 changes: 2 additions & 5 deletions cadency_commands/src/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use cadency_core::{
response::{Response, ResponseBuilder},
utils, CadencyCommand, CadencyError,
};
use serenity::{
async_trait, client::Context,
model::application::interaction::application_command::ApplicationCommandInteraction,
};
use serenity::{async_trait, client::Context, model::application::CommandInteraction};

#[derive(CommandBaseline, Default)]
#[description = "Resume current song if paused"]
Expand All @@ -17,7 +14,7 @@ impl CadencyCommand for Resume {
async fn execute<'a>(
&self,
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let guild_id = command.guild_id.ok_or(CadencyError::Command {
Expand Down
7 changes: 2 additions & 5 deletions cadency_commands/src/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use cadency_core::{
response::{Response, ResponseBuilder},
utils, CadencyCommand, CadencyError,
};
use serenity::{
async_trait, client::Context,
model::application::interaction::application_command::ApplicationCommandInteraction,
};
use serenity::{async_trait, client::Context, model::application::CommandInteraction};

#[derive(CommandBaseline, Default)]
#[description = "Skip current song"]
Expand All @@ -17,7 +14,7 @@ impl CadencyCommand for Skip {
async fn execute<'a>(
&self,
ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let guild_id = command.guild_id.ok_or(CadencyError::Command {
Expand Down
21 changes: 10 additions & 11 deletions cadency_commands/src/slap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use cadency_core::{
use serenity::{
async_trait,
client::Context,
model::application::interaction::application_command::{
ApplicationCommandInteraction, CommandDataOptionValue,
},
model::application::{CommandDataOptionValue, CommandInteraction},
};
use std::num::NonZeroU64;

#[derive(CommandBaseline, Default)]
#[description = "Slap someone with a large trout!"]
Expand All @@ -24,13 +23,13 @@ impl CadencyCommand for Slap {
async fn execute<'a>(
&self,
_ctx: &Context,
command: &'a mut ApplicationCommandInteraction,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let user = utils::get_option_value_at_position(command.data.options.as_ref(), 0)
let user_id = utils::get_option_value_at_position(command.data.options.as_ref(), 0)
.and_then(|option_value| {
if let CommandDataOptionValue::User(user, _) = option_value {
Some(user)
if let CommandDataOptionValue::User(user_id) = option_value {
Some(user_id)
} else {
error!("Command option is not a user");
None
Expand All @@ -40,20 +39,20 @@ impl CadencyCommand for Slap {
message: ":x: *Invalid user provided*".to_string(),
})?;

let response_builder = if user.id == command.user.id {
let response_builder = if user_id == &command.user.id {
response_builder.message(Some(format!(
"**Why do you want to slap yourself, {}?**",
command.user
)))
} else if user.id.0 == command.application_id.0 {
} else if NonZeroU64::from(*user_id) == NonZeroU64::from(command.application_id) {
response_builder.message(Some(format!(
"**Nope!\n{} slaps {} around a bit with a large trout!**",
user, command.user
user_id, command.user
)))
} else {
response_builder.message(Some(format!(
"**{} slaps {} around a bit with a large trout!**",
command.user, user
command.user, user_id
)))
};
Ok(response_builder.build()?)
Expand Down
Loading

0 comments on commit f031e10

Please sign in to comment.