Add option types for top-level commands
This commit is contained in:
		
							
								
								
									
										87
									
								
								src/utils.rs
									
									
									
									
									
								
							
							
						
						
									
										87
									
								
								src/utils.rs
									
									
									
									
									
								
							@@ -68,3 +68,90 @@ pub fn footer(ctx: Context<'_>) -> CreateEmbedFooter {
 | 
			
		||||
pub trait Extract {
 | 
			
		||||
    fn extract(ctx: ApplicationContext) -> Self;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub use extract_macro::Extract;
 | 
			
		||||
 | 
			
		||||
macro_rules! extract_arg {
 | 
			
		||||
    ($ctx:ident, $name:ident, String) => {
 | 
			
		||||
        $ctx.args
 | 
			
		||||
            .iter()
 | 
			
		||||
            .find(|opt| opt.name == stringify!($name))
 | 
			
		||||
            .map(|opt| &opt.value)
 | 
			
		||||
            .map_or_else(
 | 
			
		||||
                || String::new(),
 | 
			
		||||
                |v| match v {
 | 
			
		||||
                    poise::serenity_prelude::ResolvedValue::String(s) => s.to_string(),
 | 
			
		||||
                    _ => String::new(),
 | 
			
		||||
                },
 | 
			
		||||
            )
 | 
			
		||||
    };
 | 
			
		||||
    ($ctx:ident, $name:ident, Option<String>) => {
 | 
			
		||||
        $ctx.args
 | 
			
		||||
            .iter()
 | 
			
		||||
            .find(|opt| opt.name == stringify!($name))
 | 
			
		||||
            .map(|opt| &opt.value)
 | 
			
		||||
            .map(|v| match v {
 | 
			
		||||
                poise::serenity_prelude::ResolvedValue::String(s) => Some(s.to_string()),
 | 
			
		||||
                _ => None,
 | 
			
		||||
            })
 | 
			
		||||
            .flatten()
 | 
			
		||||
    };
 | 
			
		||||
    ($ctx:ident, $name:ident, bool) => {
 | 
			
		||||
        $ctx.args.iter().find(|opt| opt.name == stringify!($name)).map(|opt| &opt.value).map_or(
 | 
			
		||||
            false,
 | 
			
		||||
            |v| match v {
 | 
			
		||||
                poise::serenity_prelude::ResolvedValue::Boolean(b) => b.to_owned(),
 | 
			
		||||
                _ => false,
 | 
			
		||||
            },
 | 
			
		||||
        )
 | 
			
		||||
    };
 | 
			
		||||
    ($ctx:ident, $name:ident, Option<bool>) => {
 | 
			
		||||
        $ctx.args
 | 
			
		||||
            .iter()
 | 
			
		||||
            .find(|opt| opt.name == stringify!($name))
 | 
			
		||||
            .map(|opt| &opt.value)
 | 
			
		||||
            .map(|v| match v {
 | 
			
		||||
                poise::serenity_prelude::ResolvedValue::Boolean(b) => Some(b.to_owned()),
 | 
			
		||||
                _ => None,
 | 
			
		||||
            })
 | 
			
		||||
            .flatten()
 | 
			
		||||
    };
 | 
			
		||||
    ($ctx:ident, $name:ident, Option<PartialChannel>) => {
 | 
			
		||||
        $ctx.args
 | 
			
		||||
            .iter()
 | 
			
		||||
            .find(|opt| opt.name == stringify!($name))
 | 
			
		||||
            .map(|opt| &opt.value)
 | 
			
		||||
            .map(|v| match v {
 | 
			
		||||
                poise::serenity_prelude::ResolvedValue::Channel(partial) => {
 | 
			
		||||
                    Some(partial.to_owned())
 | 
			
		||||
                }
 | 
			
		||||
                _ => None,
 | 
			
		||||
            })
 | 
			
		||||
            .flatten()
 | 
			
		||||
            .cloned()
 | 
			
		||||
    };
 | 
			
		||||
    ($ctx:ident, $name:ident, i64) => {
 | 
			
		||||
        $ctx.args
 | 
			
		||||
            .iter()
 | 
			
		||||
            .find(|opt| opt.name == stringify!($name))
 | 
			
		||||
            .map(|opt| &opt.value)
 | 
			
		||||
            .map(|v| match v {
 | 
			
		||||
                poise::serenity_prelude::ResolvedValue::Integer(int) => *int,
 | 
			
		||||
                _ => 0,
 | 
			
		||||
            })
 | 
			
		||||
            .flatten()
 | 
			
		||||
    };
 | 
			
		||||
    ($ctx:ident, $name:ident, Option<i64>) => {
 | 
			
		||||
        $ctx.args
 | 
			
		||||
            .iter()
 | 
			
		||||
            .find(|opt| opt.name == stringify!($name))
 | 
			
		||||
            .map(|opt| &opt.value)
 | 
			
		||||
            .map(|v| match v {
 | 
			
		||||
                poise::serenity_prelude::ResolvedValue::Integer(int) => Some(*int),
 | 
			
		||||
                _ => None,
 | 
			
		||||
            })
 | 
			
		||||
            .flatten()
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub(crate) use extract_arg;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user