Skip to content

Commit

Permalink
fix custombinds parser bug, pull tower changes, print command name on…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
AsianIntel committed Nov 4, 2024
1 parent 0e10898 commit 34709bf
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 46 additions & 13 deletions rowifi_core/src/custombinds/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use nom::{
character::complete::{alpha1, alphanumeric1, char, digit1, multispace0, multispace1},
combinator::{all_consuming, map, map_res},
error::VerboseError,
multi::separated_list0,
sequence::{delimited, pair, preceded, tuple},
Err, IResult, Parser,
};
Expand Down Expand Up @@ -79,13 +80,19 @@ fn parse_constant(i: &str) -> IResult<&str, Expression, VerboseError<&str>> {
map(parse_atom, Expression::Constant).parse(i)
}

fn parse_function_args(i: &str) -> IResult<&str, Vec<Expression>, VerboseError<&str>> {
separated_list0(
preceded(multispace0, char(',')),
preceded(multispace0, map(parse_atom, Expression::Constant)),
)(i)
}

fn parse_function(i: &str) -> IResult<&str, Expression, VerboseError<&str>> {
let (i, (name, args)) = pair(
alphanumeric1,
delimited(
char('('),
map(parse_atom, |atom| vec![Expression::Constant(atom)]),
char(')'),
preceded(
multispace0,
delimited(char('('), parse_function_args, char(')')),
),
)
.parse(i)?;
Expand Down Expand Up @@ -128,13 +135,7 @@ fn parse_comparison(i: &str) -> IResult<&str, Expression, VerboseError<&str>> {
}

fn parse_term(i: &str) -> IResult<&str, Expression, VerboseError<&str>> {
alt((
parse_negation,
parse_brackets,
parse_function,
parse_constant,
))
.parse(i)
alt((parse_function, parse_constant)).parse(i)
}

fn parse_negation(i: &str) -> IResult<&str, Expression, VerboseError<&str>> {
Expand Down Expand Up @@ -165,8 +166,8 @@ fn parse_operation(i: &str) -> IResult<&str, Expression, VerboseError<&str>> {

fn parse_expression(i: &str) -> IResult<&str, Expression, VerboseError<&str>> {
alt((
parse_comparison,
parse_negation,
parse_comparison,
parse_brackets,
parse_function,
parse_constant,
Expand Down Expand Up @@ -298,6 +299,38 @@ mod tests {
vec![Expression::Constant(Atom::Num(2))]
)))
))
)
);
}

#[test]
fn full_test_2() {
assert_eq!(
parser("not HasRank(1000,25) and not HasRank (2000,25)"),
Ok(Expression::Operation(
Operator::And,
Box::new(Expression::Operation(
Operator::Not,
Box::new(Expression::Function(
"HasRank".to_string(),
vec![
Expression::Constant(Atom::Num(1000)),
Expression::Constant(Atom::Num(25))
]
)),
None
)),
Some(Box::new(Expression::Operation(
Operator::Not,
Box::new(Expression::Function(
"HasRank".to_string(),
vec![
Expression::Constant(Atom::Num(2000)),
Expression::Constant(Atom::Num(25))
]
)),
None
)))
))
);
}
}
11 changes: 6 additions & 5 deletions rowifi_core/src/user/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ impl UpdateUser<'_> {
}
}
} else {
self.guild.default_template.as_ref().unwrap().nickname(
&roblox_user,
self.user.user_id,
&self.member.username,
)
self.guild
.default_template
.as_ref()
.cloned()
.unwrap_or_default()
.nickname(&roblox_user, self.user.user_id, &self.member.username)
};

if !has_nickname_bypass && (original_nickname != new_nickname) {
Expand Down
1 change: 1 addition & 0 deletions rowifi_framework/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct BotContextInner {
pub struct BotContext(Arc<BotContextInner>);

pub struct CommandContext {
pub name: String,
pub guild_id: GuildId,
pub channel_id: ChannelId,
pub author_id: UserId,
Expand Down
5 changes: 3 additions & 2 deletions rowifi_framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
type Rejection = Response;

async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
let (_parts, body) = req.into_parts();
let (parts, body) = req.into_parts();
let bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();

let interaction = serde_json::from_slice::<Interaction>(&bytes)
Expand All @@ -51,6 +51,7 @@ where
unreachable!()
};
let ctx = CommandContext {
name: parts.uri.path_and_query().map(|s| s.to_string()).unwrap_or_default(),
guild_id: GuildId(interaction.guild_id.unwrap()),
channel_id: ChannelId(interaction.channel.as_ref().unwrap().id),
author_id: UserId(interaction.author_id().unwrap()),
Expand Down Expand Up @@ -83,6 +84,6 @@ fn recurse_skip_subcommands(data: &[CommandDataOption]) -> &[CommandDataOption]
}

pub async fn handle_error(bot: BotContext, ctx: CommandContext, err: RoError) {
tracing::error!(err = ?err);
tracing::error!(name =? ctx.name, err = ?err);
let _ = ctx.respond(&bot).content("Something went wrong. Please try again. If the issue persists, please contact the RoWifi support server.").unwrap().await;
}

0 comments on commit 34709bf

Please sign in to comment.