Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
member parsing by mention and id (part of #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
AEnterprise authored and BlackHoleFox committed May 4, 2020
1 parent d4568e5 commit 8b68c5c
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::sync::Arc;

use log::debug;
use twilight::model::gateway::payload::MessageCreate;
use twilight::cache::twilight_cache_inmemory::model::CachedMember;
use twilight::model::{
id::{GuildId, UserId},
user::User,
gateway::payload::MessageCreate,
};

use crate::commands;
use crate::commands::meta::nodes::CommandNode;
use crate::core::Context;
use crate::utils::{matchers, Error, ParseError};
use twilight::model::{
id::{GuildId, UserId},
user::User,
};

#[derive(Clone)]
pub struct Parser {
Expand Down Expand Up @@ -138,6 +139,33 @@ impl Parser {
}
}

pub async fn get_member(&mut self, gid: GuildId) -> Result<Arc<CachedMember>, Error> {
let input = self.get_next()?;
let mention = matchers::get_mention(input);
match mention {
// we got a mention
Some(uid) => match self.ctx.cache.member(gid, UserId(uid)).await? {
Some(member) => Ok(member),
None => Err(Error::ParseError(ParseError::MemberNotFoundById(uid))),
},
None => {
// is it a userid?
match input.parse::<u64>() {
Ok(uid) => match self.ctx.cache.member(gid, UserId(uid)).await? {
Some(member) => Ok(member),
None => Err(Error::ParseError(ParseError::MemberNotFoundById(uid))),
},
Err(_) => {
//nope, must be a partial name
Err(Error::ParseError(ParseError::MemberNotFoundByName(
"not implemented yet".to_string(),
)))
}
}
}
}
}

pub async fn get_user_or(&mut self, alternative: User) -> Result<Arc<User>, Error> {
if self.has_next() {
Ok(self.get_user().await?)
Expand Down

0 comments on commit 8b68c5c

Please sign in to comment.