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

@ -7,7 +7,7 @@ pub fn cmds() -> impl Iterator<Item = Command> {
let delete = ("delete", ["del", "d"]);
let reproxy = ("reproxy", ["rp", "crimes", "crime"]);
let edit = tokens!(("edit", ["e"]), ("new_content", OpaqueStringRemainder));
let edit = ("edit", ["e"]);
let apply_edit = |cmd: Command| {
cmd.flag(("append", ["a"]))
.flag(("prepend", ["p"]))
@ -25,8 +25,9 @@ pub fn cmds() -> impl Iterator<Item = Command> {
.help("Shows information about a proxied message"),
command!(message, author => "message_author").help("Shows the author of a proxied message"),
command!(message, delete => "message_delete").help("Deletes a proxied message"),
apply_edit(command!(message, edit => "message_edit")),
apply_edit(command!(edit => "message_edit")),
apply_edit(command!(message, edit, ("new_content", OpaqueStringRemainder) => "message_edit_specified")),
apply_edit(command!(edit, Skip(MessageRef), ("new_content", OpaqueStringRemainder) => "message_edit_specified")),
apply_edit(command!(edit, ("new_content", OpaqueStringRemainder) => "message_edit")),
command!(reproxy, ("member", MemberRef) => "message_reproxy")
.help("Reproxies a message with a different member"),
command!(reproxy, ("msg", MessageRef), ("member", MemberRef) => "message_reproxy_specified")

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)))
}
}

View file

@ -51,8 +51,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
extract_fn_name = get_param_param_ty(param.kind()),
throw_null = param
.is_optional()
.then_some("")
.unwrap_or(" ?? throw new PKError(\"this is a bug\")"),
.then(String::new)
.unwrap_or(format!(" ?? throw new PKError(\"parameter {} not found but was required, this is a bug in the command parser, for command: {}!\")", param.name(), command.cb)),
)?;
}
let mut command_flags_init = String::new();