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

Parse id arguments as their mentions where appropriate #315

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion src/prefix_argument/argument_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//! the auto-deref specialization emulation code to e.g. support more strings for bool parameters
//! instead of the `FromStr` ones

use super::{pop_string, InvalidBool, MissingAttachment, TooFewArguments};
use super::{
pop_string, InvalidBool, InvalidChannelId, InvalidRoleId, InvalidUserId, MissingAttachment,
TooFewArguments,
};
use crate::serenity_prelude as serenity;
use std::marker::PhantomData;

Expand Down Expand Up @@ -135,3 +138,84 @@ impl<'a> PopArgumentHack<'a, serenity::Attachment> for &PhantomData<serenity::At
Ok((args, attachment_index + 1, attachment))
}
}

#[async_trait::async_trait]
impl<'a> PopArgumentHack<'a, serenity::UserId> for &PhantomData<serenity::UserId> {
async fn pop_from(
self,
args: &'a str,
attachment_index: usize,
ctx: &serenity::Context,
msg: &serenity::Message,
) -> Result<
(&'a str, usize, serenity::UserId),
(Box<dyn std::error::Error + Send + Sync>, Option<String>),
> {
let (args, string) =
pop_string(args).map_err(|_| (TooFewArguments::default().into(), None))?;

if let Some(user_id) = string
.parse()
.ok()
.or_else(|| serenity::utils::parse_user_mention(&string))
{
Ok((args.trim_start(), attachment_index, user_id))
} else {
Err((InvalidUserId::default().into(), Some(string)))
}
}
}

#[async_trait::async_trait]
impl<'a> PopArgumentHack<'a, serenity::RoleId> for &PhantomData<serenity::RoleId> {
async fn pop_from(
self,
args: &'a str,
attachment_index: usize,
ctx: &serenity::Context,
msg: &serenity::Message,
) -> Result<
(&'a str, usize, serenity::RoleId),
(Box<dyn std::error::Error + Send + Sync>, Option<String>),
> {
let (args, string) =
pop_string(args).map_err(|_| (TooFewArguments::default().into(), None))?;

if let Some(user_id) = string
.parse()
.ok()
.or_else(|| serenity::utils::parse_role_mention(&string))
{
Ok((args.trim_start(), attachment_index, user_id))
} else {
Err((InvalidRoleId::default().into(), Some(string)))
}
}
}

#[async_trait::async_trait]
impl<'a> PopArgumentHack<'a, serenity::ChannelId> for &PhantomData<serenity::ChannelId> {
async fn pop_from(
self,
args: &'a str,
attachment_index: usize,
ctx: &serenity::Context,
msg: &serenity::Message,
) -> Result<
(&'a str, usize, serenity::ChannelId),
(Box<dyn std::error::Error + Send + Sync>, Option<String>),
> {
let (args, string) =
pop_string(args).map_err(|_| (TooFewArguments::default().into(), None))?;

if let Some(user_id) = string
.parse()
.ok()
.or_else(|| serenity::utils::parse_channel_mention(&string))
{
Ok((args.trim_start(), attachment_index, user_id))
} else {
Err((InvalidChannelId::default().into(), Some(string)))
}
}
}
jamesbt365 marked this conversation as resolved.
Show resolved Hide resolved
39 changes: 39 additions & 0 deletions src/prefix_argument/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,45 @@ impl std::fmt::Display for InvalidBool {
}
impl std::error::Error for InvalidBool {}

/// Error thrown when the user enters a string that cannot be parsed as a UserId.
#[derive(Default, Debug)]
pub struct InvalidUserId {
#[doc(hidden)]
pub __non_exhaustive: (),
}
impl std::fmt::Display for InvalidUserId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Enter a valid user's ID or a mention.")
}
}
impl std::error::Error for InvalidUserId {}

/// Error thrown when the user enters a string that cannot be parsed as a RoleId.
#[derive(Default, Debug)]
pub struct InvalidRoleId {
#[doc(hidden)]
pub __non_exhaustive: (),
}
impl std::fmt::Display for InvalidRoleId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Enter a valid role's ID or a mention.")
}
}
impl std::error::Error for InvalidRoleId {}

/// Error thrown when the user enters a string that cannot be parsed as a RoleId.
#[derive(Default, Debug)]
pub struct InvalidChannelId {
#[doc(hidden)]
pub __non_exhaustive: (),
}
impl std::fmt::Display for InvalidChannelId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Enter a valid channel's ID or a mention.")
}
}
impl std::error::Error for InvalidChannelId {}

#[cfg(test)]
#[test]
fn test_pop_string() {
Expand Down
Loading