replaced allow_slash with a method to disallow text commands for soundboard. made string argument selector stricter

This commit is contained in:
2021-06-25 23:13:11 +01:00
parent 14ef6385a0
commit a38e4c808e
9 changed files with 164 additions and 47 deletions

View File

@ -5,7 +5,7 @@ use syn::parse::{Error, Result};
use syn::spanned::Spanned;
use syn::{Attribute, Ident, Lit, LitStr, Meta, NestedMeta, Path};
use crate::structures::{ApplicationCommandOptionType, Arg, PermissionLevel};
use crate::structures::{ApplicationCommandOptionType, Arg, CommandKind, PermissionLevel};
use crate::util::{AsOption, LitExt};
#[derive(Debug, Clone, Copy, PartialEq)]
@ -321,6 +321,18 @@ impl AttributeOption for PermissionLevel {
}
}
impl AttributeOption for CommandKind {
fn parse(values: Values) -> Result<Self> {
validate(&values, &[ValueKind::SingleList])?;
Ok(values
.literals
.get(0)
.map(|(_, l)| CommandKind::from_str(&*l.to_str()).unwrap())
.unwrap())
}
}
impl AttributeOption for Arg {
fn parse(values: Values) -> Result<Self> {
validate(&values, &[ValueKind::EqualsList])?;

View File

@ -70,7 +70,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
aliases;
group;
required_permissions;
allow_slash
kind
]);
}
}
@ -82,7 +82,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
group,
examples,
required_permissions,
allow_slash,
kind,
mut cmd_args,
} = options;
@ -152,7 +152,7 @@ pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
group: #group,
examples: &[#(#examples),*],
required_permissions: #required_permissions,
allow_slash: #allow_slash,
kind: #kind,
args: &[#(&#arg_idents),*],
};

View File

@ -214,6 +214,55 @@ impl ToTokens for PermissionLevel {
}
}
#[derive(Debug)]
pub enum CommandKind {
Slash,
Both,
Text,
}
impl Default for CommandKind {
fn default() -> Self {
Self::Both
}
}
impl CommandKind {
pub fn from_str(s: &str) -> Option<Self> {
Some(match s.to_uppercase().as_str() {
"SLASH" => Self::Slash,
"BOTH" => Self::Both,
"TEXT" => Self::Text,
_ => return None,
})
}
}
impl ToTokens for CommandKind {
fn to_tokens(&self, stream: &mut TokenStream2) {
let path = quote!(crate::framework::CommandKind);
let variant;
match self {
Self::Slash => {
variant = quote!(Slash);
}
Self::Both => {
variant = quote!(Both);
}
Self::Text => {
variant = quote!(Text);
}
}
stream.extend(quote! {
#path::#variant
});
}
}
#[derive(Debug)]
pub(crate) enum ApplicationCommandOptionType {
SubCommand,
@ -293,7 +342,7 @@ pub(crate) struct Options {
pub group: String,
pub examples: Vec<String>,
pub required_permissions: PermissionLevel,
pub allow_slash: bool,
pub kind: CommandKind,
pub cmd_args: Vec<Arg>,
}
@ -301,7 +350,6 @@ impl Options {
#[inline]
pub fn new() -> Self {
Self {
allow_slash: true,
group: "Other".to_string(),
..Default::default()
}