mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-20 01:08:55 +00:00
Implement Flags parser and handle choose --everyone
- A generic Flags parser that could handle any `--flag` argument. - `choose --everyone` overrides the Offline/DnD check.
This commit is contained in:
parent
2449b09cb2
commit
2c2092eb91
3 changed files with 46 additions and 0 deletions
|
@ -51,6 +51,7 @@ Note that only online/idle users in the channel are chosen from."]
|
||||||
#[bucket = "community"]
|
#[bucket = "community"]
|
||||||
#[max_args(2)]
|
#[max_args(2)]
|
||||||
pub async fn choose(ctx: &Context, m: &Message, mut args: Args) -> CommandResult {
|
pub async fn choose(ctx: &Context, m: &Message, mut args: Args) -> CommandResult {
|
||||||
|
let flags = Flags::collect_from(&mut args);
|
||||||
let role = args.find::<RoleId>().ok();
|
let role = args.find::<RoleId>().ok();
|
||||||
let title = if args.is_empty() {
|
let title = if args.is_empty() {
|
||||||
"the chosen one".to_owned()
|
"the chosen one".to_owned()
|
||||||
|
@ -58,6 +59,8 @@ pub async fn choose(ctx: &Context, m: &Message, mut args: Args) -> CommandResult
|
||||||
args.single::<String>()?
|
args.single::<String>()?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let online_only = !flags.contains("everyone");
|
||||||
|
|
||||||
let users: Result<Vec<_>, Error> = {
|
let users: Result<Vec<_>, Error> = {
|
||||||
let guild = m.guild(&ctx).await.unwrap();
|
let guild = m.guild(&ctx).await.unwrap();
|
||||||
let presences = &guild.presences;
|
let presences = &guild.presences;
|
||||||
|
@ -69,6 +72,9 @@ pub async fn choose(ctx: &Context, m: &Message, mut args: Args) -> CommandResult
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|v| !v.user.bot) // Filter out bots
|
.filter(|v| !v.user.bot) // Filter out bots
|
||||||
.filter(|v| {
|
.filter(|v| {
|
||||||
|
if !online_only {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
// Filter out only online people
|
// Filter out only online people
|
||||||
presences
|
presences
|
||||||
.get(&v.user.id)
|
.get(&v.user.id)
|
||||||
|
|
38
youmubot-prelude/src/flags.rs
Normal file
38
youmubot-prelude/src/flags.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use serenity::prelude::Args;
|
||||||
|
use std::collections::HashSet as Set;
|
||||||
|
|
||||||
|
/// Handle flags parsing.
|
||||||
|
pub struct Flags(Set<String>);
|
||||||
|
|
||||||
|
struct Flag(pub String);
|
||||||
|
|
||||||
|
impl std::str::FromStr for Flag {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
if s.starts_with("--") {
|
||||||
|
Ok(Flag(s.trim_start_matches("--").to_owned()))
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Flags {
|
||||||
|
/// Parses the set of flags from a given `Args` structure.
|
||||||
|
pub fn collect_from(args: &mut Args) -> Flags {
|
||||||
|
let mut set = Set::new();
|
||||||
|
loop {
|
||||||
|
if let Some(Flag(s)) = args.find().ok() {
|
||||||
|
set.insert(s);
|
||||||
|
} else {
|
||||||
|
break Flags(set);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks whether `flag` exists in the flags set.
|
||||||
|
pub fn contains(&self, flag: impl AsRef<str>) -> bool {
|
||||||
|
self.0.contains(flag.as_ref())
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
pub mod announcer;
|
pub mod announcer;
|
||||||
pub mod args;
|
pub mod args;
|
||||||
|
pub mod flags;
|
||||||
pub mod hook;
|
pub mod hook;
|
||||||
pub mod member_cache;
|
pub mod member_cache;
|
||||||
pub mod pagination;
|
pub mod pagination;
|
||||||
|
@ -13,6 +14,7 @@ pub mod setup;
|
||||||
|
|
||||||
pub use announcer::{Announcer, AnnouncerHandler};
|
pub use announcer::{Announcer, AnnouncerHandler};
|
||||||
pub use args::{Duration, UsernameArg};
|
pub use args::{Duration, UsernameArg};
|
||||||
|
pub use flags::Flags;
|
||||||
pub use hook::Hook;
|
pub use hook::Hook;
|
||||||
pub use member_cache::MemberCache;
|
pub use member_cache::MemberCache;
|
||||||
pub use pagination::{paginate, paginate_fn, paginate_reply, paginate_reply_fn, Paginate};
|
pub use pagination::{paginate, paginate_fn, paginate_reply, paginate_reply_fn, Paginate};
|
||||||
|
|
Loading…
Add table
Reference in a new issue