Skip to content

Commit

Permalink
Improved .expect(...) and panic!(...) messages & removed dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
fdnt7 committed Apr 10, 2024
1 parent 0bf18af commit cd7568d
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 144 deletions.
12 changes: 6 additions & 6 deletions lyra/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lyra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dotenvy = "0.15.7"
dotenvy_macro = "0.15.7"
thiserror = "1.0.58"
color-eyre = "0.6.3"
rstest = "0.18.2"
rstest = "0.19.0"
futures = "0.3.30"
tokio = { version = "1.37.0", features = [
"sync",
Expand Down
2 changes: 1 addition & 1 deletion lyra/lyra_proc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ unwrap_used = "deny"

[dependencies]
syn = "2.0.58"
quote = "1.0.35"
quote = "1.0.36"
itertools = "0.12.1"
heck = "0.5.0"
8 changes: 3 additions & 5 deletions lyra/src/bot/command/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ pub async fn user_allowed_in(ctx: &Ctx<impl CtxKind>) -> Result<(), check::UserA
ChannelType::PublicThread
| ChannelType::PrivateThread
| ChannelType::AnnouncementThread => {
let parent_id = channel
.parent_id
.expect("threads must have a parent channel");
let parent_id = channel.parent_id.expect("threads always have a parent id");
access_calculator_builder = access_calculator_builder
.thread(channel.id)
.text_channel(parent_id);
Expand Down Expand Up @@ -201,7 +199,7 @@ fn someone_else_in(channel_id: Id<ChannelMarker>, ctx: &Ctx<impl CtxKind>) -> Op
states.any(|v| {
!ctx.cache()
.user(v.user_id())
.expect("user of `v.user_id()` must exist in the cache")
.expect("user should be in the cache")
.bot
&& v.user_id() != ctx.author_id()
})
Expand Down Expand Up @@ -336,7 +334,7 @@ async fn currently_playing(ctx: &Ctx<impl CtxKind>) -> Result<CurrentlyPlaying,
let (current, index) = queue.current_and_index().ok_or(NotPlayingError)?;

let requester = current.requester();
let position = NonZeroUsize::new(index + 1).expect("`index + 1` must be nonzero");
let position = NonZeroUsize::new(index + 1).expect("index + 1 is non-zero");
let title = current.track().info.corrected_title().into();
let channel_id = ctx.lavalink().connection(guild_id).channel_id;

Expand Down
16 changes: 8 additions & 8 deletions lyra/src/bot/command/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ macro_rules! declare_slash_commands {
fn name() -> &'static str {
let cmd_name = stringify!($raw_cmd);
&SLASH_COMMANDS_MAP.get(cmd_name)
.unwrap_or_else(|| panic!("command {} must exist", cmd_name))
.unwrap_or_else(|| panic!("command not found: {}", cmd_name))
.name
}
}
)*

impl SlashCtx {
pub async fn execute(mut self, data: CommandData) -> Result<(), CommandExecuteError> {
pub async fn execute(self, data: CommandData) -> Result<(), CommandExecuteError> {
check::user_allowed_in(&self).await?;

match data.name {
Expand All @@ -58,7 +58,7 @@ macro_rules! declare_slash_commands {
}
)*
_ => {
let cmd_data = self.take_partial_command_data().expect("`self.data` must exist");
let cmd_data = self.into_partial_command_data();
return Err(CommandExecuteError::UnknownCommand(cmd_data))
}
}
Expand All @@ -84,14 +84,14 @@ macro_rules! declare_message_commands {
fn name() -> &'static str {
let cmd_name = stringify!($raw_cmd);
&MESSAGE_COMMANDS_MAP.get(cmd_name)
.unwrap_or_else(|| panic!("command {} must exist", cmd_name))
.unwrap_or_else(|| panic!("command not found: {}", cmd_name))
.name
}
}
)*

impl MessageCtx {
pub async fn execute(mut self, data: CommandData) -> Result<(), CommandExecuteError> {
pub async fn execute(self, data: CommandData) -> Result<(), CommandExecuteError> {
check::user_allowed_in(&self).await?;

match data.name {
Expand All @@ -101,7 +101,7 @@ macro_rules! declare_message_commands {
}
)*
_ => {
let cmd_data = self.take_partial_command_data().expect("`self.data` must exist");
let cmd_data = self.into_partial_command_data();
return Err(CommandExecuteError::UnknownCommand(cmd_data))
}
}
Expand All @@ -114,15 +114,15 @@ macro_rules! declare_message_commands {
macro_rules! declare_autocomplete {
($ ($raw_cmd: ident => $raw_autocomplete: ident) ,* $(,)? ) => {
impl AutocompleteCtx {
pub async fn execute(mut self, data: CommandData) -> Result<(), AutocompleteExecuteError> {
pub async fn execute(self, data: CommandData) -> Result<(), AutocompleteExecuteError> {
match data.name {
$(
ref n if n == <$raw_cmd>::name() => {
return Ok(<$raw_autocomplete>::from_interaction(data.into())?.execute(self).await?);
}
)*
_ => {
let cmd_data = self.take_partial_command_data().expect("`self.data` must exist");
let cmd_data = self.into_partial_command_data();
return Err(AutocompleteExecuteError::UnknownAutocomplete(cmd_data))
}
}
Expand Down
29 changes: 11 additions & 18 deletions lyra/src/bot/command/model/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<T: CtxKind> Ctx<T> {
pub fn bot_member(&self) -> CachedBotMember {
self.cache()
.member(self.guild_id(), self.bot().user_id())
.expect("bot's member object must exist")
.expect("bot's member should be in cache")
}

#[inline]
Expand All @@ -122,20 +122,18 @@ impl<T: CtxKind> Ctx<T> {
self.inner
.channel
.as_ref()
.expect("`self.inner.channel` must not be `None`")
.expect("interaction type is not ping")
}

pub fn author(&self) -> &User {
self.inner
.author()
.expect("`self.inner.author()` must not be `None`")
self.inner.author().expect("interaction type is not ping")
}

pub fn member(&self) -> &PartialMember {
self.inner
.member
.as_ref()
.expect("`self.inner.member` must not be `None`")
.expect("interaction invoked in a guild")
}

#[inline]
Expand All @@ -151,18 +149,14 @@ impl<T: CtxKind> Ctx<T> {
&self.inner.token
}

fn interaction_data(&self) -> &PartialInteractionData {
self.data.as_ref().expect("`self.data` must exist")
}

pub async fn interface(&self) -> Result<InteractionInterface, DeserializeBodyFromHttpError> {
Ok(self.bot.interaction().await?.interfaces(&self.inner))
}

pub fn bot_permissions(&self) -> Permissions {
self.inner
.app_permissions
.expect("this interaction must be executed in guilds")
.expect("interaction invoked in a guild")
}

pub fn bot_permissions_for(&self, channel_id: Id<ChannelMarker>) -> Permissions {
Expand All @@ -175,7 +169,7 @@ impl<T: CtxKind> Ctx<T> {
let everyone_role = self
.cache()
.role(guild_id.cast())
.expect("`@everyone` role must exist");
.expect("@everyone role should be in cache");
let member_roles = self
.bot_member()
.roles()
Expand All @@ -191,11 +185,11 @@ impl<T: CtxKind> Ctx<T> {
let channel = self
.cache()
.channel(channel_id)
.expect("channel must exist");
.expect("channel should be in cache");
let channel_overwrites = channel
.permission_overwrites
.as_ref()
.expect("permission overrwrites must exist");
.expect("channel is in a guild");

twilight_util::permission_calculator::PermissionCalculator::new(
guild_id,
Expand Down Expand Up @@ -256,8 +250,7 @@ impl<T: CtxKind> GuildIdAware for Ctx<T> {

impl<T: CtxKind> ExpectedGuildIdAware for Ctx<T> {
fn guild_id(&self) -> Id<GuildMarker> {
self.get_guild_id()
.expect("this interaction must be executed in guilds")
self.get_guild_id().expect("interaction invoked in a guild")
}
}

Expand All @@ -266,8 +259,8 @@ impl<T: CtxKind> AuthorPermissionsAware for Ctx<T> {
self.inner
.member
.as_ref()
.expect("this interaction must be executed in guilds")
.expect("interaction invoked in a guild")
.permissions
.expect("this field should exist")
.expect("member from an interaction")
}
}
32 changes: 23 additions & 9 deletions lyra/src/bot/command/model/ctx/command_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,27 @@ impl<T: CommandDataAware> Ctx<T> {
}
}

pub fn command_data(&self) -> &PartialCommandData {
fn interaction_data(&self) -> &PartialInteractionData {
self.data.as_ref().expect("T: CommandDataAware")
}

fn into_interaction_data(self) -> PartialInteractionData {
self.data.expect("T: CommandDataAware")
}

pub fn partial_command_data(&self) -> &PartialCommandData {
let PartialInteractionData::Command(data) = self.interaction_data() else {
unreachable!()
};
data
}

pub fn take_partial_command_data(&mut self) -> Option<PartialCommandData> {
self.data.take().and_then(|d| match d {
PartialInteractionData::Command(data) => Some(data),
_ => None,
})
pub fn into_partial_command_data(self) -> PartialCommandData {
let data = self.into_interaction_data();
let PartialInteractionData::Command(command_data) = data else {
unreachable!()
};
command_data
}

pub fn command_name_full(&self) -> Box<str> {
Expand All @@ -74,14 +83,19 @@ impl<T: CommandDataAware> Ctx<T> {
}

recurse_through_names(
vec![self.command_data().name.clone()],
&self.command_data().options,
vec![self.partial_command_data().name.clone()],
&self.partial_command_data().options,
)
.join(" ")
.into()
}

pub fn command_mention_full(&self) -> Box<str> {
format!("</{}:{}>", self.command_name_full(), self.command_data().id).into()
format!(
"</{}:{}>",
self.command_name_full(),
self.partial_command_data().id
)
.into()
}
}
16 changes: 8 additions & 8 deletions lyra/src/bot/command/model/ctx/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl TargetIdAware for MessageAppMarker {}

impl<T: TargetIdAware + AppCtxKind> Ctx<AppCtxMarker<T>> {
pub fn target_id(&self) -> Id<GenericMarker> {
self.command_data()
self.partial_command_data()
.target_id
.expect("`self.command_data().target_id` must exist")
.expect("T: TargetIdAware")
}
}

Expand All @@ -40,13 +40,13 @@ impl UserCtx {
}

pub fn target_user(&self) -> &User {
self.command_data()
self.partial_command_data()
.resolved
.as_ref()
.expect("`self.command_data().resolved` must exist")
.expect("interaction type is application command")
.users
.get(&self.target_user_id())
.expect("user must exist")
.expect("user should be resolved")
}
}

Expand All @@ -57,12 +57,12 @@ impl MessageCtx {
}

pub fn target_message(&self) -> &Message {
self.command_data()
self.partial_command_data()
.resolved
.as_ref()
.expect("`self.command_data().resolved` must exist")
.expect("interaction type is application command")
.messages
.get(&self.target_message_id())
.expect("message must exist")
.expect("message should be resolved")
}
}
8 changes: 4 additions & 4 deletions lyra/src/bot/command/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ struct WaitForPollActionsContext<'a> {
}

fn handle_interactions(inter: Interaction, upvote_button_id: &String) -> PollAction {
let user_id = inter.author_id().expect("author id must exist");
let user_id = inter.author_id().expect("interaction from a guild");

let Some(InteractionData::MessageComponent(ref component)) = inter.data else {
unreachable!()
Expand All @@ -205,9 +205,9 @@ fn handle_interactions(inter: Interaction, upvote_button_id: &String) -> PollAct
let voter_permissions = inter
.member
.as_ref()
.expect("member must exist")
.expect("interaction from a guild")
.permissions
.expect("permissions must exist");
.expect("member from an interaction");

match (
super::check::is_user_dj(&Voter::new(voter_permissions)),
Expand Down Expand Up @@ -320,7 +320,7 @@ fn get_users_in_voice(
let users_in_voice = ctx
.cache()
.voice_channel_states(ctx.lavalink().connection(guild_id).channel_id)
.expect("bot must be in voice")
.expect("bot is in voice")
.map(|v| ctx.cache().user(v.user_id()).ok_or(CacheError))
.filter_map_ok(|u| (!u.bot).then_some(u.id))
.collect::<Result<HashSet<_>, _>>()?;
Expand Down
2 changes: 1 addition & 1 deletion lyra/src/bot/command/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub trait GuildAvatarUrlAware {

impl GuildAvatarUrlAware for PartialMember {
fn id(&self) -> Id<UserMarker> {
self.user.as_ref().expect("user must exist").id
self.user.as_ref().expect("member is not partial").id
}
fn avatar(&self) -> Option<ImageHash> {
self.avatar
Expand Down
Loading

0 comments on commit cd7568d

Please sign in to comment.