Skip to content

Commit

Permalink
Add support for multiple Discord domains in parse_message_url (sere…
Browse files Browse the repository at this point in the history
  • Loading branch information
darkyeg authored Nov 19, 2023
1 parent c03d36d commit ad85039
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/utils/argument_convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use role::*;
mod emoji;
pub use emoji::*;

use super::DOMAINS;
use crate::model::prelude::*;
use crate::prelude::*;

Expand Down Expand Up @@ -109,13 +110,29 @@ pub fn parse_message_id_pair(s: &str) -> Option<(ChannelId, MessageId)> {
/// MessageId::new(806164913558781963),
/// )),
/// );
/// assert_eq!(
/// parse_message_url(
/// "https://canary.discord.com/channels/381880193251409931/381880193700069377/806164913558781963"
/// ),
/// Some((
/// GuildId::new(381880193251409931),
/// ChannelId::new(381880193700069377),
/// MessageId::new(806164913558781963),
/// )),
/// );
/// assert_eq!(parse_message_url("https://google.com"), None);
/// ```
#[must_use]
pub fn parse_message_url(s: &str) -> Option<(GuildId, ChannelId, MessageId)> {
let mut parts = s.strip_prefix("https://discord.com/channels/")?.splitn(3, '/');
let guild_id = parts.next()?.parse().ok()?;
let channel_id = parts.next()?.parse().ok()?;
let message_id = parts.next()?.parse().ok()?;
Some((guild_id, channel_id, message_id))
for domain in DOMAINS {
if let Some(parts) = s.strip_prefix(&format!("https://{domain}/channels/")) {
let mut parts = parts.splitn(3, '/');

let guild_id = parts.next()?.parse().ok()?;
let channel_id = parts.next()?.parse().ok()?;
let message_id = parts.next()?.parse().ok()?;
return Some((guild_id, channel_id, message_id));
}
}
None
}

0 comments on commit ad85039

Please sign in to comment.