fix message edit commands, parse DM message links for ids

This commit is contained in:
dusk 2025-10-13 10:25:55 +00:00
parent 134855f8f8
commit a307dd37c4
No known key found for this signature in database
6 changed files with 74 additions and 47 deletions

View file

@ -50,13 +50,13 @@ pub fn parse_command(
let mut matched_tokens: Vec<(Tree, (Token, TokenMatchResult, usize))> = Vec::new();
let mut filtered_tokens: Vec<Token> = Vec::new();
loop {
println!(
"possible: {:?}",
local_tree
.possible_tokens()
.filter(|t| filtered_tokens.contains(t))
.collect::<Vec<_>>()
);
// println!(
// "possible: {:?}",
// local_tree
// .possible_tokens()
// .filter(|t| filtered_tokens.contains(t))
// .collect::<Vec<_>>()
// );
let next = next_token(
local_tree
.possible_tokens()
@ -64,7 +64,7 @@ pub fn parse_command(
&input,
current_pos,
);
println!("next: {:?}", next);
// println!("next: {:?}", next);
match &next {
Some((found_token, result, new_pos)) => {
match &result {

View file

@ -135,32 +135,41 @@ impl Parameter {
return Ok(ParameterValue::MessageRef(None, None, message_id));
}
static RE: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
static SERVER_RE: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(
|| {
regex::Regex::new(
r"https://(?:\w+\.)?discord(?:app)?\.com/channels/(?P<guild>\d+)/(?P<channel>\d+)/(?P<message>\d+)",
)
.unwrap()
},
);
static DM_RE: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
regex::Regex::new(
r"https://(?:\w+\.)?discord(?:app)?\.com/channels/(\d+)/(\d+)/(\d+)",
r"https://(?:\w+\.)?discord(?:app)?\.com/channels/@me/(?P<channel>\d+)/(?P<message>\d+)",
)
.unwrap()
});
if let Some(captures) = RE.captures(input) {
let guild_id = captures
.get(1)
.and_then(|m| m.as_str().parse::<u64>().ok())
.ok_or_else(|| SmolStr::new("invalid guild ID in message link"))?;
let channel_id = captures
.get(2)
.and_then(|m| m.as_str().parse::<u64>().ok())
.ok_or_else(|| SmolStr::new("invalid channel ID in message link"))?;
let message_id = captures
.get(3)
.and_then(|m| m.as_str().parse::<u64>().ok())
.ok_or_else(|| SmolStr::new("invalid message ID in message link"))?;
if let Some(captures) = SERVER_RE.captures(input) {
let guild_id = captures.parse_id("guild")?;
let channel_id = captures.parse_id("channel")?;
let message_id = captures.parse_id("message")?;
Ok(ParameterValue::MessageRef(
Some(guild_id),
Some(channel_id),
message_id,
))
} else if let Some(captures) = DM_RE.captures(input) {
let channel_id = captures.parse_id("channel")?;
let message_id = captures.parse_id("message")?;
Ok(ParameterValue::MessageRef(
None,
Some(channel_id),
message_id,
))
} else {
Err(SmolStr::new("invalid message reference"))
}
@ -560,3 +569,15 @@ impl FromStr for ProxySwitchAction {
.ok_or_else(|| SmolStr::new("invalid proxy switch action, must be new/add/off"))
}
}
trait ParseMessageLink {
fn parse_id(&self, name: &str) -> Result<u64, SmolStr>;
}
impl ParseMessageLink for regex::Captures<'_> {
fn parse_id(&self, name: &str) -> Result<u64, SmolStr> {
self.name(name)
.and_then(|m| m.as_str().parse::<u64>().ok())
.ok_or_else(|| SmolStr::new(format!("invalid {} in message link", name)))
}
}