Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: parrot message types #198

Merged
merged 11 commits into from
May 29, 2022
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: -- -D clippy::all
args: -- -D clippy::all -D warnings

rustfmt:
name: Format
Expand Down
13 changes: 5 additions & 8 deletions src/commands/autopause.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
errors::ParrotError,
guild::settings::GuildSettingsMap,
strings::{AUTOPAUSE_OFF, AUTOPAUSE_ON},
errors::ParrotError, guild::settings::GuildSettingsMap, messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand All @@ -19,10 +17,9 @@ pub async fn autopause(
let guild_settings = settings.entry(guild_id).or_default();
guild_settings.autopause = !guild_settings.autopause;

let message = if guild_settings.autopause {
AUTOPAUSE_ON.to_string()
if guild_settings.autopause {
create_response(&ctx.http, interaction, ParrotMessage::AutopauseOn).await
} else {
AUTOPAUSE_OFF.to_string()
};
create_response(&ctx.http, interaction, &message).await
create_response(&ctx.http, interaction, ParrotMessage::AutopauseOff).await
}
}
4 changes: 2 additions & 2 deletions src/commands/clear.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
errors::{verify, ParrotError},
handlers::track_end::update_queue_messages,
strings::CLEARED,
messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand Down Expand Up @@ -29,7 +29,7 @@ pub async fn clear(
let queue = handler.queue().current_queue();
drop(handler);

create_response(&ctx.http, interaction, CLEARED).await?;
create_response(&ctx.http, interaction, ParrotMessage::Clear).await?;
update_queue_messages(&ctx.http, &ctx.data, &queue, guild_id).await;
Ok(())
}
4 changes: 2 additions & 2 deletions src/commands/leave.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{errors::ParrotError, strings::LEAVING, utils::create_response};
use crate::{errors::ParrotError, messaging::message::ParrotMessage, utils::create_response};
use serenity::{
client::Context, model::interactions::application_command::ApplicationCommandInteraction,
};
Expand All @@ -11,5 +11,5 @@ pub async fn leave(
let manager = songbird::get(ctx).await.unwrap();
manager.remove(guild_id).await.unwrap();

create_response(&ctx.http, interaction, LEAVING).await
create_response(&ctx.http, interaction, ParrotMessage::Leaving).await
}
4 changes: 2 additions & 2 deletions src/commands/pause.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
errors::{verify, ParrotError},
strings::PAUSED,
messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand All @@ -21,5 +21,5 @@ pub async fn pause(
verify(!queue.is_empty(), ParrotError::NothingPlaying)?;
verify(queue.pause(), ParrotError::Other("Failed to pause"))?;

create_response(&ctx.http, interaction, PAUSED).await
create_response(&ctx.http, interaction, ParrotMessage::Pause).await
}
14 changes: 7 additions & 7 deletions src/commands/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::{
commands::{skip::force_skip_top_track, summon::summon},
errors::{verify, ParrotError},
handlers::track_end::update_queue_messages,
messaging::message::ParrotMessage,
messaging::messages::{
PLAY_QUEUE, PLAY_TOP, SPOTIFY_AUTH_FAILED, TRACK_DURATION, TRACK_TIME_TO_PLAY,
},
sources::{
spotify::{Spotify, SPOTIFY},
youtube::{YouTube, YouTubeRestartable},
},
strings::{
PLAY_ALL_FAILED, PLAY_PLAYLIST, PLAY_QUEUE, PLAY_TOP, SEARCHING, SPOTIFY_AUTH_FAILED,
TRACK_DURATION, TRACK_TIME_TO_PLAY,
},
utils::{
create_now_playing_embed, create_response, edit_embed_response, edit_response,
get_human_readable_timestamp,
Expand Down Expand Up @@ -95,7 +95,7 @@ pub async fn play(

// reply with a temporary message while we fetch the source
// needed because interactions must be replied within 3s and queueing takes longer
create_response(&ctx.http, interaction, SEARCHING).await?;
create_response(&ctx.http, interaction, ParrotMessage::Search).await?;

let handler = call.lock().await;
let queue_was_empty = handler.queue().is_empty();
Expand Down Expand Up @@ -215,7 +215,7 @@ pub async fn play(
}
}
_ => {
edit_response(&ctx.http, interaction, PLAY_ALL_FAILED).await?;
edit_response(&ctx.http, interaction, ParrotMessage::PlayAllFailed).await?;
return Ok(());
}
},
Expand Down Expand Up @@ -245,7 +245,7 @@ pub async fn play(
edit_embed_response(&ctx.http, interaction, embed).await?;
}
(QueryType::PlaylistLink(_) | QueryType::KeywordList(_), _) => {
edit_response(&ctx.http, interaction, PLAY_PLAYLIST).await?;
edit_response(&ctx.http, interaction, ParrotMessage::PlaylistQueued).await?;
}
(_, _) => {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
errors::ParrotError,
guild::cache::GuildCacheMap,
handlers::track_end::ModifyQueueHandler,
strings::{
messaging::messages::{
QUEUE_EXPIRED, QUEUE_NOTHING_IS_PLAYING, QUEUE_NOW_PLAYING, QUEUE_NO_SONGS, QUEUE_PAGE,
QUEUE_PAGE_OF, QUEUE_UP_NEXT,
},
Expand Down
5 changes: 3 additions & 2 deletions src/commands/remove.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
errors::{verify, ParrotError},
handlers::track_end::update_queue_messages,
strings::{REMOVED_QUEUE, REMOVED_QUEUE_MULTIPLE},
messaging::message::ParrotMessage,
messaging::messages::REMOVED_QUEUE,
utils::create_embed_response,
utils::create_response,
};
Expand Down Expand Up @@ -71,7 +72,7 @@ pub async fn remove(
let embed = create_remove_enqueued_embed(track).await;
create_embed_response(&ctx.http, interaction, embed).await?;
} else {
create_response(&ctx.http, interaction, REMOVED_QUEUE_MULTIPLE).await?;
create_response(&ctx.http, interaction, ParrotMessage::RemoveMultiple).await?;
}

update_queue_messages(&ctx.http, &ctx.data, &queue, guild_id).await;
Expand Down
11 changes: 7 additions & 4 deletions src/commands/repeat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
errors::ParrotError,
strings::{FAIL_LOOP, LOOP_DISABLED, LOOP_ENABLED},
errors::ParrotError, messaging::message::ParrotMessage, messaging::messages::FAIL_LOOP,
utils::create_response,
};
use serenity::{
Expand All @@ -27,8 +26,12 @@ pub async fn repeat(
};

match toggler(&track) {
Ok(_) if was_looping => create_response(&ctx.http, interaction, LOOP_DISABLED).await,
Ok(_) if !was_looping => create_response(&ctx.http, interaction, LOOP_ENABLED).await,
Ok(_) if was_looping => {
create_response(&ctx.http, interaction, ParrotMessage::LoopDisable).await
}
Ok(_) if !was_looping => {
create_response(&ctx.http, interaction, ParrotMessage::LoopEnable).await
}
_ => Err(ParrotError::Other(FAIL_LOOP)),
}
}
4 changes: 2 additions & 2 deletions src/commands/resume.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
errors::{verify, ParrotError},
strings::RESUMED,
messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand All @@ -21,5 +21,5 @@ pub async fn resume(
verify(!queue.is_empty(), ParrotError::NothingPlaying)?;
verify(queue.resume(), ParrotError::Other("Failed resuming track"))?;

create_response(&ctx.http, interaction, RESUMED).await
create_response(&ctx.http, interaction, ParrotMessage::Resume).await
}
11 changes: 7 additions & 4 deletions src/commands/seek.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
errors::{verify, ParrotError},
strings::{FAIL_MINUTES_PARSING, FAIL_SECONDS_PARSING, SEEKED},
messaging::message::ParrotMessage,
messaging::messages::{FAIL_MINUTES_PARSING, FAIL_SECONDS_PARSING},
utils::create_response,
};
use serenity::{
Expand All @@ -19,8 +20,8 @@ pub async fn seek(
let args = interaction.data.options.clone();
let seek_time = args.first().unwrap().value.as_ref().unwrap();

let timestamp = seek_time.as_str().unwrap();
let mut units_iter = timestamp.split(':');
let timestamp_str = seek_time.as_str().unwrap();
let mut units_iter = timestamp_str.split(':');

let minutes = units_iter.next().and_then(|c| c.parse::<u64>().ok());
let minutes = verify(minutes, ParrotError::Other(FAIL_MINUTES_PARSING))?;
Expand All @@ -42,7 +43,9 @@ pub async fn seek(
create_response(
&ctx.http,
interaction,
&format!("{} **{}**!", SEEKED, seek_time),
ParrotMessage::Seek {
timestamp: timestamp_str.to_owned(),
},
)
.await
}
6 changes: 3 additions & 3 deletions src/commands/shuffle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
errors::ParrotError, handlers::track_end::update_queue_messages, strings::SHUFFLED_SUCCESS,
utils::create_response,
errors::ParrotError, handlers::track_end::update_queue_messages,
messaging::message::ParrotMessage, utils::create_response,
};
use rand::Rng;
use serenity::{
Expand Down Expand Up @@ -28,7 +28,7 @@ pub async fn shuffle(
let queue = handler.queue().current_queue();
drop(handler);

create_response(&ctx.http, interaction, SHUFFLED_SUCCESS).await?;
create_response(&ctx.http, interaction, ParrotMessage::Shuffle).await?;
update_queue_messages(&ctx.http, &ctx.data, &queue, guild_id).await;
Ok(())
}
Expand Down
16 changes: 7 additions & 9 deletions src/commands/skip.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
errors::{verify, ParrotError},
strings::{SKIPPED, SKIPPED_ALL, SKIPPED_TO},
messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand Down Expand Up @@ -50,20 +50,18 @@ pub async fn create_skip_response(
create_response(
&ctx.http,
interaction,
&format!(
"{} [**{}**]({})!",
SKIPPED_TO,
track.metadata().title.as_ref().unwrap(),
track.metadata().source_url.as_ref().unwrap()
),
ParrotMessage::SkipTo {
title: track.metadata().title.as_ref().unwrap().to_owned(),
url: track.metadata().source_url.as_ref().unwrap().to_owned(),
},
)
.await
}
None => {
if tracks_to_skip > 1 {
create_response(&ctx.http, interaction, SKIPPED_ALL).await
create_response(&ctx.http, interaction, ParrotMessage::SkipAll).await
} else {
create_response(&ctx.http, interaction, SKIPPED).await
create_response(&ctx.http, interaction, ParrotMessage::Skip).await
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/stop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
errors::{verify, ParrotError},
handlers::track_end::update_queue_messages,
strings::STOPPED,
messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand All @@ -26,7 +26,7 @@ pub async fn stop(
let queue = handler.queue().current_queue();
drop(handler);

create_response(&ctx.http, interaction, STOPPED).await?;
create_response(&ctx.http, interaction, ParrotMessage::Stop).await?;
update_queue_messages(&ctx.http, &ctx.data, &queue, guild_id).await;
Ok(())
}
12 changes: 9 additions & 3 deletions src/commands/summon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
connection::get_voice_channel_for_user,
errors::ParrotError,
handlers::{IdleHandler, TrackEndHandler},
strings::JOINING,
messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand Down Expand Up @@ -67,8 +67,14 @@ pub async fn summon(
}

if send_reply {
let content = format!("{} **{}**!", JOINING, channel_id.mention());
return create_response(&ctx.http, interaction, &content).await;
return create_response(
&ctx.http,
interaction,
ParrotMessage::Summon {
mention: channel_id.mention(),
},
)
.await;
}

Ok(())
Expand Down
24 changes: 9 additions & 15 deletions src/commands/version.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
use crate::{
errors::ParrotError,
strings::{VERSION, VERSION_LATEST},
utils::create_response,
};
use crate::{errors::ParrotError, messaging::message::ParrotMessage, utils::create_response};
use serenity::{
client::Context, model::interactions::application_command::ApplicationCommandInteraction,
};

const RELEASES_LINK: &str = "https://github.com/aquelemiguel/parrot/releases";

pub async fn version(
ctx: &Context,
interaction: &mut ApplicationCommandInteraction,
) -> Result<(), ParrotError> {
let current = option_env!("CARGO_PKG_VERSION").unwrap_or_else(|| "Unknown");
let current = format!(
"{} [{}]({}/tag/v{})",
VERSION, current, RELEASES_LINK, current
);
let latest = format!("{}({}/latest)", VERSION_LATEST, RELEASES_LINK);
let content = format!("{}\n{}", current, latest);

create_response(&ctx.http, interaction, &content).await
create_response(
&ctx.http,
interaction,
ParrotMessage::Version {
current: current.to_owned(),
},
)
.await
}
14 changes: 5 additions & 9 deletions src/commands/voteskip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
connection::get_voice_channel_for_user,
errors::{verify, ParrotError},
guild::cache::GuildCacheMap,
strings::{SKIP_VOTE_EMOJI, SKIP_VOTE_MISSING, SKIP_VOTE_USER},
messaging::message::ParrotMessage,
utils::create_response,
};
use serenity::{
Expand Down Expand Up @@ -50,14 +50,10 @@ pub async fn voteskip(
create_response(
&ctx.http,
interaction,
&format!(
"{}{} {} {} {}",
SKIP_VOTE_EMOJI,
interaction.user.id.mention(),
SKIP_VOTE_USER,
skip_threshold - cache.current_skip_votes.len(),
SKIP_VOTE_MISSING
),
ParrotMessage::VoteSkip {
mention: interaction.user.id.mention(),
missing: skip_threshold - cache.current_skip_votes.len(),
},
)
.await
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::strings::{
use crate::messaging::messages::{
FAIL_ANOTHER_CHANNEL, FAIL_AUTHOR_DISCONNECTED, FAIL_AUTHOR_NOT_FOUND,
FAIL_NO_VOICE_CONNECTION, FAIL_WRONG_CHANNEL, NOTHING_IS_PLAYING, QUEUE_IS_EMPTY,
TRACK_INAPPROPRIATE, TRACK_NOT_FOUND,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::{
Arc,
};

use crate::strings::IDLE_ALERT;
use crate::messaging::messages::IDLE_ALERT;

pub struct IdleHandler {
pub http: Arc<Http>,
Expand Down
Loading